具有不同文本和不同功能的DatagridViewButtonColumn用于不同的行 [英] DatagridViewButtonColumn with different Text and different Functionality for different Rows

查看:124
本文介绍了具有不同文本和不同功能的DatagridViewButtonColumn用于不同的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在winforms中创建一个datagidview。它具有一个Datagridviewbuttoncolumn和其他一些列。 Datagridviewbutton列必须首先显示文本 Upload,然后单击它应打开一个存储文件的位置。文件上传后,Datagridviewbuttoncolumn文本应变为 View File,然后单击它应打开文件。简要说明如何完成将很有帮助。

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.

推荐答案

按钮的不同文本


  • 处理 CellFormatting 事件,并根据其他列的值或您需要的任何其他逻辑来设置 e.Value

  • Handle CellFormatting event and set e.Value based on value of other columns or any other logic you need.

按钮的不同功能


  • 处理 CellContentClick 并根据其他列的值或任何其他逻辑y确定要运行的任务

在两种情况下


  • 首先检查是否为数据单元格(而非标题单元格)触发了事件,请检查 e.ColumnIndex e.RowIndex 应该大于 -1

然后检查是否为所需的列触发了事件,例如,检查事件是否针对索引为 0 e.ColumnIndex 应该等于 0

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

要获取同一行中另一列的值,例如索引2处的列,只需使用 dataGridView1.Rows(e.RowIndex).Cells(2).Value 。因此,您可以根据其他列的值来确定文本或要运行的代码。

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.

这篇关于具有不同文本和不同功能的DatagridViewButtonColumn用于不同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆