问题
I need to create a datagidview in winforms. It has a Datagridviewbuttoncolumn and few other columns. The Datagridviewbuttoncolumn must intially display text "Upload" and clicking on it should open a location to store files. After file is uploaded the Datagridviewbuttoncolumn text should become "View File" and clicking on it should open the file. A brief idea how it could be done will be helpful.
回答1:
Different Text for Buttons
- Handle CellFormatting event and set
e.Value
based on value of other columns or any other logic you need.
Different Functionality for Buttons
- Handle CellContentClick and decide about the task that you want to run based on value of other columns or any other logic you need.
In both events
First check if the event is fired for a data cell, not a header cell, check
e.ColumnIndex
ande.RowIndex
should be greater than-1
Then check if the event is fired for your desired column, for example to check if the event is for column at index
0
,e.ColumnIndex
should be equals to0
To get value of another column in the same row, for example column at index 2, simply use
dataGridView1.Rows(e.RowIndex).Cells(2).Value
. So you can decide about the text or about the code you want to run, based on value of other columns.
来源:https://stackoverflow.com/questions/36354700/datagridviewbuttoncolumn-with-different-text-and-different-functionality-for-dif