关于DataGridView的一个问题 本帖最后由 svview 于 2010-12-14 14:15:55 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在CellContentClick中,遍历Column1,如果为选中状态,取得Column2中字符串,与当前选中的Column2中字符串比较,如果相同出提示代码自己写一下吧,懒得写 javascript 可以控制這樣的操作.... ........还没节帖啊........写个参考,控件:dataGridView1private class CityModel { private string _CityName; public string CityName { get { return _CityName; } set { _CityName = value; } } } private void Form2_Load(object sender, EventArgs e) { List<CityModel> lst = new List<CityModel>(); CityModel cm1 = new CityModel(); cm1.CityName = "中国"; CityModel cm2 = new CityModel(); cm2.CityName = "美国"; CityModel cm3 = new CityModel(); cm3.CityName = "中国"; lst.Add(cm1); lst.Add(cm2); lst.Add(cm3); dataGridView1.DataSource = lst; } int _isEdit = 0; private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1 && e.ColumnIndex == 0) { _isEdit = 1; } } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { _isEdit = 0; } private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { if (_isEdit == 0) return; if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null && (bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value == false) return; for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (e.RowIndex != i && dataGridView1.Rows[i].Cells[1].Value.ToString() == dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()) { if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null && (bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value == true) { MessageBox.Show("已选择"); _isEdit = 0; return; } } } } public class Class1 : INotifyPropertyChanged{ public event MyEventHandler Checked; public event PropertyChangedEventHandler PropertyChanged; private bool column1; public bool Column1 { get { return column1; } set { column1 = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Column1")); if(value == true) // 无论是通过界面还是代码,只要是true就触发这个事件。 { if (Checked != null) Checked(this, new MyEventArgs { Col2 = Column2 }); } } } private string column2; public string Column2 { get { return column2; } set { column2 = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Column2")); } } } public delegate void MyEventHandler(object sender, MyEventArgs e); public class MyEventArgs : EventArgs { public string Col2 { get; set; } }} public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Init(); } private List<Class1> list; public void Init() { list = new List<Class1>(); Class1 cls = new Class1 { Column1 = false, Column2 = "中国人" }; cls.Checked += new MyEventHandler(cls_Checked); list.Add(cls); cls = new Class1 { Column1 = false, Column2 = "日本人" }; cls.Checked += new MyEventHandler(cls_Checked); list.Add(cls); cls = new Class1 { Column1 = false, Column2 = "中国人" }; cls.Checked += new MyEventHandler(cls_Checked); list.Add(cls); cls = new Class1 { Column1 = false, Column2 = "美国人" }; cls.Checked += new MyEventHandler(cls_Checked); list.Add(cls); cls = new Class1 { Column1 = false, Column2 = "日本人" }; cls.Checked += new MyEventHandler(cls_Checked); list.Add(cls); dataGridView1.DataSource = list; } void cls_Checked(object sender, MyEventArgs e) { if (list.FindAll(p => p.Column1 == true && p.Column2 == e.Col2).Count > 1) { MessageBox.Show("不行,有重复"); Class1 cls = sender as Class1; cls.Column1 = false; } } }这样做的好处是,你修改那些文字部分,比如日本人改成中国人或美国人,照样起着防止重得的作用。 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if ((e.RowIndex > -1) && (e.ColumnIndex > -1)) { if (this.dataGridView1.CurrentCell is DataGridViewCheckBoxCell) { try { if ((bool)(this.dataGridView1.CurrentCell.Value) == true)//取消选中时返回 return; } catch { } try { if (IsChecked(this.dataGridView1.CurrentRow.Cells[1].Value.ToString())) { MessageBox.Show("你已经选择了"); } } catch { } } } } //已经选中过 bool IsChecked(string value) { foreach (DataGridViewRow dr in this.dataGridView1.Rows) { try { if ((bool)(dr.Cells[0].Value) == true) { if (dr.Cells[1].Value.ToString() == value) return true; } } catch { return false; } } return false; } 如果DataGrid绑定的是DataTable之类会更方便,直接datatable.Select("字段1=true,字段2='中国人'").Count>0类似的代码就可以判断出已经有复选择了 看着不好,重发一下: private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if ((e.RowIndex > -1) && (e.ColumnIndex > -1)) { if (this.dataGridView1.CurrentCell is DataGridViewCheckBoxCell) { try { if ((bool)(this.dataGridView1.CurrentCell.Value) == true)//取消选中时返回 return; } catch { } try { if (IsChecked(this.dataGridView1.CurrentRow.Cells[1].Value.ToString())) { MessageBox.Show("你已经选择了"); } } catch { } } } } //已经选中过 bool IsChecked(string value) { foreach (DataGridViewRow dr in this.dataGridView1.Rows) { try { if ((bool)(dr.Cells[0].Value) == true) { if (dr.Cells[1].Value.ToString() == value) return true; } } catch { return false; } } return false; } 你真是我亲哥! 哈哈我就想要这样的!我是和DataTable绑定了,可是不能用Select,因为这些属性都是能过别的画面用list传过来的·! 在每次checkBox打勾的时候用一个HashTable保存打勾的数据,key是行号,value是选择的值或者对应的ID,同样打勾去掉的时候就找到把值给删掉。这样打勾的时候直接从这个HashTable里面去查找有没有选中同样值的数据,不就可以判断了,而且循环次数也少。 谢谢你给我的回答,不过现在有个问题,你给我的代码是在点击后判断出提示框,之后这个重复的checkbox就被选上了,怎么能让它不选上呢? wpf窗口如何创建和编辑窗口的“通知区域” C# Ftp FtpWebResponse类不响应的原因 oracle数据库blob读入内存,再用word打开 如何把两个int数组中差最小的两个元素拿出来,组成一个二维数组,在线等!!!!! 找不到一个引用,高手请进来指点一下,送分啦! 请教aspx页面中如何实现repeater的多行表头? 如何自动在今天更新的新闻前加小图片??求教各为高手!! 问:如何实现WINDOWS服务拒绝启动的代码?抛异常可以吗? 初学者问题!感谢大家帮助! 资源嵌入的问题 textbox 长度 c# 点击按钮打开一个form后,textbox自动获得焦点。
代码自己写一下吧,懒得写
写个参考,控件:dataGridView1private class CityModel
{
private string _CityName;
public string CityName
{
get { return _CityName; }
set { _CityName = value; }
}
}
private void Form2_Load(object sender, EventArgs e)
{
List<CityModel> lst = new List<CityModel>();
CityModel cm1 = new CityModel();
cm1.CityName = "中国";
CityModel cm2 = new CityModel();
cm2.CityName = "美国";
CityModel cm3 = new CityModel();
cm3.CityName = "中国";
lst.Add(cm1);
lst.Add(cm2);
lst.Add(cm3);
dataGridView1.DataSource = lst;
}
int _isEdit = 0;
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex == 0)
{
_isEdit = 1;
}
} private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
_isEdit = 0;
} private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
if (_isEdit == 0) return;
if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null
&& (bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value == false) return;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (e.RowIndex != i &&
dataGridView1.Rows[i].Cells[1].Value.ToString() == dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString())
{
if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null && (bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value == true)
{
MessageBox.Show("已选择");
_isEdit = 0;
return;
}
}
}
}
public class Class1 : INotifyPropertyChanged
{
public event MyEventHandler Checked;
public event PropertyChangedEventHandler PropertyChanged; private bool column1;
public bool Column1
{
get { return column1; }
set
{
column1 = value; if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Column1")); if(value == true) // 无论是通过界面还是代码,只要是true就触发这个事件。
{
if (Checked != null)
Checked(this, new MyEventArgs { Col2 = Column2 });
}
}
} private string column2;
public string Column2
{
get { return column2; }
set
{
column2 = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Column2")); }
}
} public delegate void MyEventHandler(object sender, MyEventArgs e); public class MyEventArgs : EventArgs
{
public string Col2 { get; set; }
}
} public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Init();
} private List<Class1> list; public void Init()
{
list = new List<Class1>(); Class1 cls = new Class1 { Column1 = false, Column2 = "中国人" };
cls.Checked += new MyEventHandler(cls_Checked);
list.Add(cls); cls = new Class1 { Column1 = false, Column2 = "日本人" };
cls.Checked += new MyEventHandler(cls_Checked);
list.Add(cls); cls = new Class1 { Column1 = false, Column2 = "中国人" };
cls.Checked += new MyEventHandler(cls_Checked);
list.Add(cls); cls = new Class1 { Column1 = false, Column2 = "美国人" };
cls.Checked += new MyEventHandler(cls_Checked);
list.Add(cls); cls = new Class1 { Column1 = false, Column2 = "日本人" };
cls.Checked += new MyEventHandler(cls_Checked);
list.Add(cls); dataGridView1.DataSource = list; } void cls_Checked(object sender, MyEventArgs e)
{ if (list.FindAll(p => p.Column1 == true && p.Column2 == e.Col2).Count > 1)
{
MessageBox.Show("不行,有重复");
Class1 cls = sender as Class1;
cls.Column1 = false;
}
} }
这样做的好处是,你修改那些文字部分,比如日本人改成中国人或美国人,照样起着防止重得的作用。
{
if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
{
if (this.dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
try
{
if ((bool)(this.dataGridView1.CurrentCell.Value) == true)//取消选中时返回
return;
}
catch { } try
{
if (IsChecked(this.dataGridView1.CurrentRow.Cells[1].Value.ToString()))
{
MessageBox.Show("你已经选择了");
}
}
catch { }
}
} } //已经选中过
bool IsChecked(string value)
{
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
try
{
if ((bool)(dr.Cells[0].Value) == true)
{
if (dr.Cells[1].Value.ToString() == value)
return true;
}
}
catch { return false; }
}
return false;
}
{
if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
{
if (this.dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
try
{
if ((bool)(this.dataGridView1.CurrentCell.Value) == true)//取消选中时返回
return;
}
catch { } try
{
if (IsChecked(this.dataGridView1.CurrentRow.Cells[1].Value.ToString()))
{
MessageBox.Show("你已经选择了");
}
}
catch { }
}
} } //已经选中过
bool IsChecked(string value)
{
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
try
{
if ((bool)(dr.Cells[0].Value) == true)
{
if (dr.Cells[1].Value.ToString() == value)
return true;
}
}
catch { return false; }
}
return false;
}
你真是我亲哥! 哈哈我就想要这样的!我是和DataTable绑定了,可是不能用Select,因为这些属性都是能过别的画面用list传过来的·!
谢谢你给我的回答,不过现在有个问题,你给我的代码是在点击后判断出提示框,之后这个重复的checkbox就被选上了,怎么能让它不选上呢?