DatagridViewButtonColumn with different Text and different Functionality for different Rows

我只是一个虾纸丫 提交于 2019-12-13 02:22:51

问题


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 and e.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 to 0

  • 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!