C# DataGridViewButtonCell 设置按钮文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19159801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:15:36  来源:igfitidea点击:

C# DataGridViewButtonCell set buttons text

c#winformsdatagridviewdatagridviewcolumndatagridviewbuttoncolumn

提问by animekun

I need to add my DataGridViewButtonCellto Column, and I need to name each other with different names.

我需要将 my 添加DataGridViewButtonCellColumn,并且我需要用不同的名字命名彼此。

But I didn't find any text properties.

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

Can anyone help me, please?

任何人都可以帮助我吗?

i do that stuff

我做那些事

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

and it doesn't work

它不起作用

采纳答案by VineAndBranches

I admit that I don't understand whyyour method wouldn't work, but I was able to get the following to do what you wanted as a work-around:

我承认我不明白为什么你的方法行不通,但我能够得到以下内容来做你想做的解决方法:

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

In my example I'm assuming you only have one column (a DataGridViewButtonColumn). You should be able to modify this however you like so long as you set the value afteryou add the row. Again, not sure why that's the case...

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

回答by progpow

Try

尝试

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

回答by Damith

回答by Sriram Sakthivel

If you want all the buttons to have same text use UseColumnTextForButtonValueproperty.

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

If you want different text for each button then use DataGridViewCell.Valueproperty

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

回答by Batu.Khan

If you have just one column and that's a buttoncolumn by default

如果您只有一列并且默认情况下是按钮列

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

will suffice.

就足够了。

回答by Jinwoo.Seo

Use this to show the same text on all buttons with default button at first

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

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

回答by Sabyasachi Mishra

I know you got your answer accepted but I added these in constructorand it worked for me. The only thing I was missing is UseColumnTextForButtonValue = true

我知道你的答案被接受了,但我添加了这些constructor,它对我有用。我唯一缺少的是UseColumnTextForButtonValue = true

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

回答by JustLearning

If you want to set the same text for all buttons, you can:

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

  1. In GridViewproperties click on edit columns
  2. Click on your button column
  3. Set Textto the text you want to see on each button
  4. Set UserColumnTextForButtonValueto True
  1. GridView属性中单击编辑列
  2. 单击您的按钮列
  3. 设置Text为您希望在每个按钮上看到的文本
  4. 设置UserColumnTextForButtonValueTrue

See below:

见下文:

Edit Button Column

编辑按钮列