C#datagridviewbuttoncolumn无文字表现 [英] C# datagridviewbuttoncolumn no text showing
问题描述
我需要显示在 DataGridViewButtonColumn
一些文本。我读过许多关于这些类似的问题对 SO
。他们中的许多答案,建议设置 UseColumnTextForButtonValue
到真
这不适合我。这似乎以一种奇怪的方式,微软
使得它至少有一个行
这样的按钮,将显示文本
I need to show some texts on the DataGridViewButtonColumn
. I've read lots of similar question about these on SO
. Many of them answers recommend setting UseColumnTextForButtonValue
to True
which doesn't for me. It seems in an odd way that Microsoft
makes it to have at least a row
so that the button will display the text.
下面是我的代码:
DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
EditColumn.HeaderText = "Complete";
EditColumn.Name = "Complete";
EditColumn.UseColumnTextForButtonValue = True;
dataGridView.Columns.Add(EditColumn);
这代码没有按不显示在 DataGridViewButtonColumn $ C $文本C>:
This code doesn not show the text on the DataGridViewButtonColumn
:
这是在这里工作的代码:
The code that works here:
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "a";
dataGridView1.Columns[1].Name = "b";
ArrayList row = new ArrayList();
row.Add("1");
row.Add("2");
dataGridView1.Rows.Add(row.ToArray());
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.Name = "text";
btn.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(btn);
此代码工作,但不过我从数据库中获取我的数据,因此我不会使用 dataGridView.Rows.Add(row.ToArray())
在这里。所以,我怎么让文字显示在 DataGridViewButtonColumn
?
This code works but however I am getting my data from a database and thus I won't be using dataGridView.Rows.Add(row.ToArray())
here. So how do I get the text to show on the DataGridViewButtonColumn
?
推荐答案
你有两个问题需要解决:
You have two problems to solve:
- 如何绑定
按钮
列于数据源
- 如何让
数据源
不仅产生了默认列类型也是一个(或多个)按钮列
- How bind a
Button
column to aDataSource
- How to make the
DataSource
create not only default column types but also one (or more) button column
第一个问题基本上是通过设置 DataPropertyName
解决。
The first issue is basically solved by setting the DataPropertyName
of the column.
但我不知道它是如何,甚至是否有可能影响 ColumnTypes
自动创建。
But I do not know how or even if it is possible to influence the ColumnTypes
created automatically.
因此,我提出一个解决办法:不要依靠的的AutoGenerateColumns
我们能做到自己在一个小的辅助函数:
Therefore I suggest a workaround: Instead of relying on the AutoGenerateColumns
we can do it ourselves in a little helper function:
这将清除的列,并为在一个数据表各列新的
,使用在表中的列的属性。告诉它的列(S),我们想成为的按钮,我们传递一个字符串的列名,用空格包围:
This clears the columns and creates a new one for each column in a DataTable
, using the properties of the columns in the table. To tell it which column(s) we want to be buttons we pass a string with the column names, surrounded by spaces:
void CreateSpecialColumns(DataTable dt, DataGridView dgv, string buttons)
{
dgv.Columns.Clear();
for (int i = 0; i < dt.Columns.Count; i++ )
{
DataColumn dc = dt.Columns[i];
if (buttons.Contains(" " + dc.ColumnName + " "))
{
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.HeaderText = dc.Caption;
buttonColumn.Name = dc.ColumnName;
buttonColumn.ValueType = dc.DataType;
buttonColumn.DataPropertyName = dc.ColumnName;
dgv.Columns.Add(buttonColumn);
}
else
{ // normal columns
int c = dgv.Columns.Add(dc.ColumnName, dc.Caption);
dgv.Columns[c].ValueType = dc.DataType;
dgv.Columns[c].DataPropertyName = dc.ColumnName;
}
}
}
下面是我把它称为使用 DataTable的DT
:
Here is how I called it using a DataTable DT
:
dataGridView1.AutoGenerateColumns = false;
CreateSpecialColumns(DT, dataGridView1, " Cook Waiter ", "");
dataGridView1.DataSource = DT;
请注意,这只是工作,如果 UseColumnTextForButtonValue
属性的不可以设置为true!
Note that this only works if the UseColumnTextForButtonValue
property is not set to true!
这篇关于C#datagridviewbuttoncolumn无文字表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!