功能:遍历datagridview控件中“关键词”所在列的值,如果存在相同的值,则将重复的值标记为空!
代码如下
int k,m;
for(k=0;k<dataGridView.RowCount;++k)
{
for (m = 1; m<dataGridView.RowCount;++m )
{
if ((m>k)&&(dataGridView.Rows[k].Cells["keyword"].Value == dataGridView.Rows[m].Cells["keyword"].Value))
{
dataGridView.Rows[m].Cells["keyword"].Value = null;
}
}
}
程序的结果是,点击按钮,没有任何的反映,求教各位高手如何解决!DataGridView控件遍历
代码如下
int k,m;
for(k=0;k<dataGridView.RowCount;++k)
{
for (m = 1; m<dataGridView.RowCount;++m )
{
if ((m>k)&&(dataGridView.Rows[k].Cells["keyword"].Value == dataGridView.Rows[m].Cells["keyword"].Value))
{
dataGridView.Rows[m].Cells["keyword"].Value = null;
}
}
}
程序的结果是,点击按钮,没有任何的反映,求教各位高手如何解决!DataGridView控件遍历
解决方案 »
- 一个较长字符串,有中文英文或数字,怎样将其前38个字节赋给另一个字符串
- 在gridview控件里删除最后条记录时出现错误,不是最后条记录没问题
- keydown 事件 有很多怎么选择。WINFORM
- 急救,关于C#实现ADSL自动拨号类的问题
- 发现一个酷站,散点分
- 请问如何用XmlDocument对象把两个同构的xml文档合并??
- 奇怪问题 程序只在某个虚拟机里面有问题。
- 求助!C#后台静默下载
- The type name or alias IColorInfo could not be resolved. Please check your confi
- 用oledb读取excel文件时,excel版本是多少?求告知!感谢!
- 你不知道的 double 和float类型的值比较问题
- 关于上午的一个C#问题的总结!你肯定学到东西!(涉及两个关于DataGridView控件的关键点!)重要关键)
{
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;
}
}
}
根据索引号去查看看呢
这也是一样啊 excel的数据是数据源 点击确定只是你把Excel的数据处理完以后重新绑定! 不需要在dataGridView中处理 明白
private void button2_Click(object sender, EventArgs e)
{
for(int k=0;k<dataGridView.RowCount;++k)
{
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;
}
}
}
}这个就是去重按钮的事件!求帮忙呀!
private void button2_Click(object sender, EventArgs e)
{
for(int k=0;k<dataGridView.RowCount;++k)
{
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;
}
}
}
MessageBox.Show(dataGridView.Rows[0].Cells[1].Value.ToString());
}
message可以正常显示!
INotifyPropertyChanged 看看这个
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
你这样写: for (int m = k+1;m<dataGridView.RowCount;++m )
{if(dataGridView.Rows[m].Cells[1].Value==null)
{
dataGridView.Rows[m].Cells[1].Value="";
}
if (dataGridView.Rows[k].Cells[1].Value.ToString() == dataGridView.Rows[m].Cells[1].Value.ToString())
{
dataGridView.Rows[m].Cells[1].Value = null;
}
}
{
object str=dataGridView.Rows[k].Cells[1].Value;
object str2=dataGridView.Rows[m].Cells[1].Value;
if(str2==null)
{
str2="";
}
if (str.ToString() == str2.ToString() )
{
dataGridView.Rows[m].Cells[1].Value = null;
}
}
可尝试修改dataGridView.DataSource 然后重新指定数据源例如 dataGridView.DataSource = list1;
你只需要修改list1就可以 然后重新dataGridView.DataSource = list1;
换成dataGridView.Rows[m].Cells[1].text="";试试
如果不行 那就真给它一个值 dataGridView.Rows[m].Cells[1].Value=" " 不要跟我说不识别空格
如果不行 那就真给它一个值 dataGridView.Rows[m].Cells[1].Value=" " 不要跟我说不识别空格问题解决了,如果想知道,可以看我的下一篇帖子!
以下是例子:(仅供参考,并不严谨)
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;
}
}
}