这是一段编历数据库充填treeview的程序, 以首字母做父结点. 我能保证数据库库信息的正常的.
我对读入的字符串进行substring处理获取首字符, 编译器老是报我出错, 为什么, 不可能有错呀.
编译器大哥放过我吧.string nch = rd["CName"].ToString();
nch = nch.Substring(0,1); // 索引和长度必须引用该字符串内的位置。或是 string nch = rd["CName"].Substring(0,1); private void Ref()
{// SELECT CID, CName FROM Customer ORDER BY CName
this.tvCust.Nodes.Clear();
string ch = "";
int i = 0; this.cmdList.Connection.Open();
System.Data.SqlClient.SqlDataReader rd = this.cmdList.ExecuteReader();
while( rd.Read() )
{
string nch = rd["CName"].ToString();
nch = nch.Substring(0,1); // 索引和长度必须引用该字符串内的位置。
if( nch != ch )
{
ch = rd["CName"].ToString().Substring(0,1);
TreeNode fnode = new TreeNode(ch);
this.tvCust.Nodes.Add(fnode);
i++;
}
//if( rd["CompanyName"].ToString().Substring(0,1) == ch )
if( rd["CName"].ToString().Substring(0,1) == ch)
{
TreeNode snode = new TreeNode( rd["CName"].ToString() );
snode.Tag = rd["CID"].ToString();
this.tvCust.Nodes[i-1].Nodes.Add(snode);
}
}
rd.Close();
this.cmdList.Connection.Close();
}
我对读入的字符串进行substring处理获取首字符, 编译器老是报我出错, 为什么, 不可能有错呀.
编译器大哥放过我吧.string nch = rd["CName"].ToString();
nch = nch.Substring(0,1); // 索引和长度必须引用该字符串内的位置。或是 string nch = rd["CName"].Substring(0,1); private void Ref()
{// SELECT CID, CName FROM Customer ORDER BY CName
this.tvCust.Nodes.Clear();
string ch = "";
int i = 0; this.cmdList.Connection.Open();
System.Data.SqlClient.SqlDataReader rd = this.cmdList.ExecuteReader();
while( rd.Read() )
{
string nch = rd["CName"].ToString();
nch = nch.Substring(0,1); // 索引和长度必须引用该字符串内的位置。
if( nch != ch )
{
ch = rd["CName"].ToString().Substring(0,1);
TreeNode fnode = new TreeNode(ch);
this.tvCust.Nodes.Add(fnode);
i++;
}
//if( rd["CompanyName"].ToString().Substring(0,1) == ch )
if( rd["CName"].ToString().Substring(0,1) == ch)
{
TreeNode snode = new TreeNode( rd["CName"].ToString() );
snode.Tag = rd["CID"].ToString();
this.tvCust.Nodes[i-1].Nodes.Add(snode);
}
}
rd.Close();
this.cmdList.Connection.Close();
}
解决方案 »
- 用c#模拟邮箱登陆 如何获得邮箱是否成功登陆(如sina,gmail,126)
- nextdate 问题,来看看啦!
- 向各位高手求救: 急,在sql 2005中存了各种文件的路径名,怎么查询这些文件中有没有一些词语,
- C#中使用水晶报表如和实现图表统计
- 大家帮我看看这个过程调用的代码片段有没有错,感觉是没错,但是报错连不上库。
- 正则表达式替换一个英文单词,要求不区别大小写。
- 大家帮忙看看,本来正常的,不难的
- 字符数组比较如何效率最高呢?
- 关于网站架构的问题!在线请教大神!
- (在线等)怎样把DataGrid中的一列取出来?并赋值给一个数组?
- 关于一个C#datagrid 的问题!请各位大虾帮忙!在线等!分不够再给加!
- C#是不是不能动态分配内存
我的数据表中就这列有值, 其他的都为空.
string s = rd["CName"].ToString();
this.txtID.Text = s;
this.txtName.Text = rd[0].ToString();
this.txtOCount.Text = s.Substring(0,1);最后一句注释掉, 运行OK. this.txtID.Text显示一个完整字符串.
不注释 this.txtOCount.Text = s.Substring(0,1); // 索引和长度必须引用该字符串内的位置。我他妈找谁惹谁了.