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
C# DataGridViewButtonCell set buttons text
提问by animekun
I need to add my DataGridViewButtonCell
to Column
, and I need to name each other with different names.
我需要将 my 添加DataGridViewButtonCell
到Column
,并且我需要用不同的名字命名彼此。
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
you looking for DataGridViewCell.Value Property
回答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 mit
回答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 constructor
and 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:
如果要为所有按钮设置相同的文本,您可以:
- In
GridView
properties click on edit columns - Click on your button column
- Set
Text
to the text you want to see on each button - Set
UserColumnTextForButtonValue
toTrue
- 在
GridView
属性中单击编辑列 - 单击您的按钮列
- 设置
Text
为您希望在每个按钮上看到的文本 - 设置
UserColumnTextForButtonValue
为True
See below:
见下文: