datagrid视图按钮列的问题

时间:2011-08-31 19:44:43

标签: c# .net winforms datagridview

有人可以帮忙吗?

我有一个包含三列和一个按钮列的DataGridView。

如果我点击按钮列,那么该行中的所有值都应转移到另一种形式。

注意:我已经实现了以下点击事件处理程序:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
}

除了使用单元格点击事件之外,还有其他选择吗?

非常感谢.....

2 个答案:

答案 0 :(得分:1)

尝试CellContentClick事件。

  

单击单元格内容时会发生此事件。当按钮单元格或复选框单元格具有焦点时,用户按下并释放空格键时也会发生这种情况,如果在按空格键时单击单元格内容,则会对这些单元格类型发生两次。

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick.aspx

编辑:CellContentClick事件提供DataGridViewCellEventArgs参数,其中包含单击的确切列和行。从我提供的那个链接,我可以把一个简短的例子放在一起。我假设您要传递的值是字符串,并且位于第2,3和4列(分别为索引1,2和3)。 注意,这未经过测试!

if (DataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn && cellEvent.RowIndex != -1)
{
    DataRow currRow = DataGridView1.Rows[e.RowIndex];
    SendValuesSomewhereElse(Convert.ToString(currRow[1]), Convert.ToString(currRow[2]), Convert.ToString(currRow[3]));
}

答案 1 :(得分:1)

假设前三列包含数据,然后第四列包含按钮。

提取数据的快捷方式是:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    //If not fourth column then return (the button)
    if (e.ColumnIndex != 3)
        return;
    object col1 = dataGridView1.Rows[e.RowIndex].Cells[0].Value;
    object col2 = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
    object col3 = dataGridView1.Rows[e.RowIndex].Cells[2].Value;
}

您点击的行包含e.RowIndex,并点击了e.ColumnIndex列。 0是第一行/列。

相关问题