【问题标题】:change the button text in datagridview when clicking单击时更改datagridview中的按钮文本
【发布时间】:2019-06-05 19:07:29
【问题描述】:

我有一个未绑定的DataGridViewButtonColumn,我想在用户执行操作后更改每个按钮(和每一行)上的文本(以将进程标记为正在进行或准备就绪)。如何在不影响每一行的情况下更改一行中的按钮文本?因为我尝试使用单元格的名称,但随后所有行都受到影响

我试过了

dataGridView1[e.RowIndex, e.ColumnIndex].Value = "new button text";
dataGridView1.CurrentCell.Value = "ABCD";
ColumnButton.Text = "in progress";

【问题讨论】:

  • 您的按钮文本是否绑定到底层数据源行?
  • 我的按钮是使用datagridview助手创建的

标签: c# button text


【解决方案1】:

您说这是一个未绑定的DataGridView,但我不确定您是如何添加行的。我假设您正在创建DataGridViewRow 的实例,然后将DataGridViewButtonCell 添加到按钮列的Cells 属性?

如果设置列的文本 (ColumnButton.Text = "in progress") 会更改所有单元格的文本,那么听起来您在 DataGridViewButtonCell 上将 UseColumnTextForButtonValue 设置为 true。如果该属性设置为 true,则更改单元格上的 Value 不会更新按钮的文本。

添加行时,只需将DataGridViewButtonCell 上的Value 设置为ColumnButton.Text,即可将其默认为列的文本。那么dataGridView1[e.RowIndex, e.ColumnIndex].Value = "new button text"; 应该可以工作了。

private void PopulateDataGridView()
{
   // Assuming I have a DataGridView named dataGridView1 with only one column
   // which is a DataGridViewButtonColumn named ButtonColumn that I set up in
   // the "Add Columns" or "Edit Columns" window...
   var row = new DataGridViewRow();
   var btnCell = new DataGridViewButtonCell();

   // Instead of btnCell.UseColumnTextForButtonValue = true, do this to set the
   // text of the button to the text of the column by default.
   btnCell.Value = ButtonColumn.Text;

   row.Cells.Add(btnCell);   

   dataGridView1.Rows.Add(row);
}

private void dataGridView1_CellContentClick(
   object sender, 
   DataGridViewCellEventArgs e)
{
   dataGridView1[e.ColumnIndex, e.RowIndex].Value = "Loading...";
}

【讨论】:

  • 是的,我创建了 daratgridview,然后我使用助手添加了列,还创建了单元格按钮并将此 UseColumnTextForButtonValue 设置为 true,否则将不会显示文本。但我试图将它编辑为假 bt 仍然没有工作我从早上开始就在尝试所有他能找到的代码但它不起作用
  • 创建DataGridViewButtonCell 时,不要将UseColumnTextForButtonValue 设置为true,只需将Value 属性设置为ButtonCell.Text。如果它仍然不起作用,您正在处理哪个事件?您需要处理 CellContentClick 而不是 CellClick
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-08
  • 2022-01-08
  • 2021-01-20
相关资源
最近更新 更多