首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

代码很简单,但是乃是不知道出错的原因在哪里!囧

2013-08-27 
代码很简单,但是就是不知道出错的原因在哪里!囧~功能:遍历datagridview控件中“关键词”所在列的值,如果存在

代码很简单,但是就是不知道出错的原因在哪里!囧~
功能:遍历datagridview控件中“关键词”所在列的值,如果存在相同的值,则将重复的值标记为空!
代码如下



DataGridView 控件 遍历
[解决办法]
for(int k=0;k<dataGridView.RowCount;k++)
            {
                for (int m = k+1; m<dataGridView.RowCount;m++ )
                {
                    if ((dataGridView.Rows[k].Cells[0].Text == dataGridView.Rows[m].Cells[0].Text))
                    {


                        dataGridView.Rows[m].Cells[0].Text= string.Empty;
                    }
                }
            }


根据索引号去查看看呢
[解决办法]
点没反应?加个MessageBox.Show调试下不就知道了
[解决办法]
其实问题在这里 
 for (int m = k+1;m<dataGridView.RowCount;++m )
                {
                    if (dataGridView.Rows[k].Cells[1].Value == dataGridView.Rows[m].Cells[1].Value)
                    {
                        dataGridView.Rows[m].Cells[1].Value = null;
                    }
                }
你把这里改成下面这样:
 for (int m = k+1;m<dataGridView.RowCount;++m )
                {
                    if (dataGridView.Rows[k].Cells[1].Value.ToString() == dataGridView.Rows[m].Cells[1].Value.ToString())
                    {
                        dataGridView.Rows[m].Cells[1].Value = null;
                    }
                }

因为dataGridView.Rows[k].Cells[1].Values是个Object的类型,所以得到的值虽然是一样,但是没法相等,你转换成string类型就可以了,不过你的m=k+1,有可能到最后一行的时候dataGridView.Rows[m].Cells[1].Value=null,这样转换就会报错,所以在转换成string之前你最好先判断一下dataGridView.Rows[m].Cells[1].Value是否为null
------解决方案--------------------




如果不行 那就真给它一个值 dataGridView.Rows[m].Cells[1].Value="&nbsp;" 不要跟我说不识别空格

热点排行