开发者

Implementing two cells click functions in same row using single cell click event

I have a DataGridView 开发者_StackOverflow中文版with three columns.

I have implemented a function in the DataGridView cell click event for one cell in one row. If I click on that cell then the corresponding row values will be transferred to another form. That works.

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

My problem is that I want to implement another function for another cell being clicked in same row. Can I implement this function in same click event (as I have mentioned above) or is there another process I need to follow?


The DataGridViewCellEventArgs can be used to determine what the position of that cell in the grid is:

DataGridViewCell cell = (DataGridViewCell) dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

if (cell.ColumnIndex == this.dataGridView1.Columns["YourColumn"].Index)
{
    // Do something when a "YourColumn" cell is clicked
}
else if (cell.ColumnIndex == this.dataGridView1.Columns["AnotherColumn"].Index)
{
    // Do something when an "AnotherColumn" cell is clicked
}

This way you can provide different behaviour, according to what cell is clicked.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜