1 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
2 {
3 if (e.RowIndex == -1)
4 return;
5 //string s = string.Empty;
6 //foreach (DataGridViewCell c in dataGridView1.Rows[e.RowIndex].Cells)
7 //{
8 // s += c.ColumnIndex.ToString() + "," + c.Value.ToString() + " || ";
9 //}
10 int index = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
11 CustomerControlCellEventArgs args = new CustomerControlCellEventArgs(index);
12 CellClick(sender, args);
13 }
2 {
3 if (e.RowIndex == -1)
4 return;
5 //string s = string.Empty;
6 //foreach (DataGridViewCell c in dataGridView1.Rows[e.RowIndex].Cells)
7 //{
8 // s += c.ColumnIndex.ToString() + "," + c.Value.ToString() + " || ";
9 //}
10 int index = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
11 CustomerControlCellEventArgs args = new CustomerControlCellEventArgs(index);
12 CellClick(sender, args);
13 }
这是我为一个DataGridView添加的Cell单击事件,问题出在第10行。
在.Net 2.0 DataGridView里的Cell真实排列顺序竟然和显示的不一样。
就是说显示的第一列是编号,但是我只能用Cells[10]来访问它,而不是Cells[0]
这是我使用被注释的代码获得的S来看到的,这相当的郁闷。
在我把这个程序拿到公司装了.Net 3.5的机器上跑时候,发现这个问题然被修正了。
就是现在显示的和真实排列顺序是一致的,程序代码我未作任何修改。
看样子,如果要让DataGridView在这2个版本都能正常运行,只能使用Cells[string ColumnName]来访问了。
我Google了一下,发现这个问题可能是由DataGridView设计时候手动更改过显示的顺序造成的。
也就是显示的排列顺序,和Column添加到DataGridView的顺序不一致。