【问题标题】:Changing DataGridViewButtonColumn's Button Text Per Row更改每行的 DataGridViewButtonColumn 按钮文本
【发布时间】:2011-07-25 23:35:39
【问题描述】:

我有一个DataGridView,其中一列类型为DataGridViewButtonColumn。

我想在按钮上放一个文本。我尝试在“编辑列”选项中将文本放入“文本”属性中。我什至尝试过 swtteing UseColumnTextForButtonValue = true。

我找不到让它工作的方法。

【问题讨论】:

    标签: c# datagridview datagridviewcolumn


    【解决方案1】:

    假设你的 DataGridView 名称是 DataGridView1

     foreach (DataGridViewRow row in dataGridView1.Rows)
     {
         DataGridViewCell cell = row.Cells[0]; //Column Index for the dataGridViewButtonColumn
         cell.Value = "Your Text";
     }
    

    【讨论】:

      【解决方案2】:

      是否要将 DataGridViewButtonColumn 文本绑定到 DataGridView 的数据源?如果是这样,则通过“编辑列”选项为该列设置 DataPropertyName。

      否则你可以尝试使用 DataGridView 的 CellFormatting 事件:

      private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
      {
          if (e.ColumnIndex == 0)
          {
              e.Value = "some value";
          }
      }
      

      【讨论】:

      • 这应该被接受。注意:e.Value 只是显示的,与cell.Value 无关,它仍然可以是我们需要的任何东西,包括真实 Button: ` 按钮 btn = DGV[2, e.RowIndex].Value 作为按钮; if (btn != null) e.Value = btn.Text;`
      猜你喜欢
      • 2018-07-03
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多