开发者

Problem with datagrid view button column

开发者 https://www.devze.com 2023-04-01 19:27 出处:网络
Could anyone please help with this? I have a DataGridView with three columns and one button column. If I click on the button column, then all values in that row should be transferred to another form

Could anyone please help with this?

I have a DataGridView with three columns and one button column.

If I click on the button column, then all values in that row should be transferred to another form.

Note : I have already implemented the following click event handler:

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

Is there an alternative other than using the开发者_开发百科 cell click event?

Many Thanks.....


Try the CellContentClick event.

This event occurs when the cell content is clicked. It also occurs when the user presses and releases the SPACEBAR while a button cell or check box cell has focus, and will occur twice for these cell types if the cell content is clicked while pressing the SPACEBAR.

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

Edit: The CellContentClick event provides a DataGridViewCellEventArgs parameters, which contains the exact column and row clicked. From that link I provided, I can put together a short example. I'm assuming the values you want to pass are strings and are in columns 2, 3 and 4 (indexes 1, 2 and 3, respectively). Note, this isn't tested!

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]));
}


Assuming you have first three columns with data and then the fourth column with the button.

A quick and dirty way to extract the data is:

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;
}

You get the clicked row with e.RowIndex and clicked column with e.ColumnIndex. 0 being first row/column.

0

精彩评论

暂无评论...
验证码 换一张
取 消