1

我的问题是:当我单击某个复选框时,如何从 DataGridView 获取当前复选框控件的索引这是我的 snick 代码

dataGridView2.RowCount = 5;
    dataGridView2.ColumnCount = 4;

    for (int i = 0; i < dataGridView2.ColumnCount; i++)
    {
        for (int j = 0; j < dataGridView2.RowCount; j++)
        {
            box = new CheckBox();
            box.Text = "MyDate";
            //box.Size = new System.Drawing.Size(15, 15);
            dataGridView2.Controls.Add(box);
            Rectangle rec = dataGridView2.GetCellDisplayRectangle(i, j, true);
            box.Left = rec.Left;
            box.Top = rec.Top;
        }
    }
}
4

3 回答 3

0

检查此链接。这为您提供了有关网格视图的详细信息。

http://msdn.microsoft.com/en-us/library/ms972814.aspx

http://msdn.microsoft.com/en-us/library/aa479344.aspx

于 2013-08-05T08:47:29.750 回答
0

如果您使用的是CellContentClick事件或您获得的任何其他事件,DataGridViewCellEventArgs那么您拥有的单元格的列ColumnIndexRowIndex行的属性已更改

于 2013-08-05T08:41:22.877 回答
0

看起来您尝试在不使用 a的情况下添加纯CheckBoxes ,这种方法的解决方案很简单,如下所示:DataGridViewDataGridViewCheckBoxColumn

for (int i = 0; i < dataGridView2.ColumnCount; i++)
    {
        for (int j = 0; j < dataGridView2.RowCount; j++)
        {
            box = new CheckBox();
            box.Text = "MyDate";
            //box.Size = new System.Drawing.Size(15, 15);
            dataGridView2.Controls.Add(box);
            Rectangle rec = dataGridView2.GetCellDisplayRectangle(i, j, true);
            box.Left = rec.Left;
            box.Top = rec.Top;
            //Added code
            box.Tag = new Point(i,j);
            box.Click += CheckBoxesClicked;
        }
    }
private void CheckBoxesClicked(object sender, EventArgs e){
   CheckBox chb = sender as CheckBox;
   if(chb.Tag != null) {
      Point coord = (Point)chb.Tag;
      MessageBox.Show(string.Format("Row index: {0}\nColumn index: {1}", coord.Y, coord.X);
   }
}     

您应该使用 aDataGridViewCheckBoxColumn相反,通过这种方法,您可以处理事件CellContentClick......

于 2013-08-05T09:40:44.067 回答