【问题标题】:C# DataGridViewButtonCell set buttons textC# DataGridViewButtonCell 设置按钮文本
【发布时间】:2013-10-03 12:56:04
【问题描述】:

我需要将我的DataGridViewButtonCell 添加到Column,并且我需要用不同的名字互相命名。

但我没有找到任何文本属性。

有人可以帮帮我吗?

我做那些事

DataGridViewButtonCell b = new DataGridViewButtonCell(); 
b.Value = "name"; 
MainTable.Rows.Add(b);

它不起作用

【问题讨论】:

  • 如果您按照大多数答案的建议设置 DataGridViewCell.Value 属性,它将更改实际值。往往不能接受。使用 CellFormatting 事件并在那里设置 e.Value!

标签: c# winforms datagridview datagridviewcolumn datagridviewbuttoncolumn


【解决方案1】:

如果您希望所有按钮具有相同的文本,请使用 UseColumnTextForButtonValue 属性。

如果您希望每个按钮使用不同的文本,请使用 DataGridViewCell.Value 属性

【讨论】:

  • UseColumnTextForButtonValue 正是我想要的。将其设置为 true 并将列的 Text 属性设置为您想要显示的内容。
【解决方案2】:

如果您想为所有按钮设置相同的文本,您可以:

  1. GridView 属性中点击编辑列
  2. 点击你的按钮栏
  3. Text 设置为您希望在每个按钮上看到的文字
  4. UserColumnTextForButtonValue 设置为True

见下文:

【讨论】:

    【解决方案3】:

    我承认我不明白为什么你的方法不起作用,但我能够得到以下解决方法来做你想做的事情:

            DataGridViewButtonCell b = new DataGridViewButtonCell();
            int rowIndex = MainTable.Rows.Add(b);
            MainTable.Rows[rowIndex].Cells[0].Value = "name";
    

    在我的示例中,我假设您只有一列(DataGridViewButtonColumn)。只要您在添加行之后设置值,您应该可以随意修改它。再说一次,不知道为什么会这样......

    【讨论】:

      【解决方案4】:

      我知道您的回答已被接受,但我在constructor 中添加了这些内容,它对我有用。我唯一缺少的是UseColumnTextForButtonValue = true

      public Form1() 
          {
              InitializeComponent();
              var editColumn = new DataGridViewButtonColumn
              {
                  Text = "Edit",
                  UseColumnTextForButtonValue = true,
                  Name = "Edit",
                  DataPropertyName = "Edit"
              };
              dataGridView1.Columns.Add(editColumn);
          }
      

      【讨论】:

      • 这个答案很好,我很久以前就解决了,但如果它真的有效,那就最好了,不能自己检查,但任何寻找答案的人最好试试这个
      【解决方案5】:

      首先使用它在所有带有默认按钮的按钮上显示相同的文本

      DataGridView.Columns["Buttons_Index"].DefaultCellStyle.NullValue = "name";
      

      【讨论】:

        【解决方案6】:
        【解决方案7】:

        试试

        yourDataGridView.Rows[rowIndex].Cells[columnIndex].Value
        

        【讨论】:

        • 我做了那些东西DataGridViewButtonCell b = new DataGridViewButtonCell(); b.Value = "name"; MainTable.Rows.Add(b);,但它不起作用
        • 只是按钮列,仅此而已
        • 您还需要在表中添加DataGridViewButtonColumn DataGridViewButtonColumn column = new DataGridViewButtonColumn (); column.Name = "按钮"; MainTable.Columns.Add(column);
        【解决方案8】:

        【讨论】:

        • 我做那些东西 DataGridViewButtonCell b = new DataGridViewButtonCell(); b.Value = "名称"; MainTable.Rows.Add(b);它不起作用
        【解决方案9】:

        如果你只有一列并且默认是按钮列

        MainTable.Rows.Add("Some text");
        

        足够了。

        【讨论】:

        • 希望对只使用一栏的 smbdy 有所帮助。
        • 是的。对整个 dgv 只使用一个按钮列的人可能需要很多帮助 8)
        【解决方案10】:

        [Colimn.Name].Text = "点击我"

        [列名].UseColumnTextForButtonValue = True

        button_Text 在 Form_Load 事件中使用此代码 - 它将将该列中的所有按钮命名为“单击我”。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-08
          • 2013-11-20
          • 2018-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-30
          • 2018-10-28
          相关资源
          最近更新 更多