C# DataGridView 显示行标题单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1267357/
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
DataGridView display row header cell
提问by
I'm trying to display a simple DataGridView linked to a DataTable and I want, ultimately, my first column in the DataTable to be the row header cell for the DataGridView. At this point I will settle for having any value in the row header cell. I can display the DataGridView with all my rows and columns, and with column header cells, but no row header cell. I check the value in the row.HeaderCell.Value, and the data I put there is there. I check row.HeaderCell.Displayed and it is false, but this is read only, so I can't make it true. How do I make the row header cell display?
我正在尝试显示一个链接到 DataTable 的简单 DataGridView,我最终希望 DataTable 中的第一列成为 DataGridView 的行标题单元格。在这一点上,我将满足于在行标题单元格中有任何值。我可以显示包含所有行和列以及列标题单元格的 DataGridView,但没有行标题单元格。我检查了 row.HeaderCell.Value 中的值,我放在那里的数据就在那里。我检查了 row.HeaderCell.Displayed 并且它是假的,但这是只读的,所以我不能让它成为真的。如何使行标题单元格显示?
Here's a simple sample of what I've tried to get this to work:
这是我试图让它发挥作用的一个简单示例:
DataTable table = new DataTable();
for (int i = 0; i<10; i++)
{
table.Columns.Add(new DataColumn("column-" + i));
}
for (int i = 0; i < 10; i++)
{
DataRow theRow = table.NewRow();
for (int j = 0; j < 10; j++)
theRow[j] = i + "-" + j;
table.Rows.Add(theRow);
}
dataGridView1.DataSource = table;
dataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
int rowNumber = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.IsNewRow) continue;
row.HeaderCell.Value = "Row " + rowNumber;
rowNumber = rowNumber + 1;
}
dataGridView1.AutoResizeRowHeadersWidth(
DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
回答by
Try the DataGridView.RowHeadersVisible property.
试试 DataGridView.RowHeadersVisible 属性。
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowheadersvisible.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowheadersvisible.aspx
回答by
The answer appears to be handling the DataGridView.CellFormatting event. Why setting the value elsewhere doesn't work is beyond me, but I'm sure there's a reason. I added the following event handler and all is good:
答案似乎是处理 DataGridView.CellFormatting 事件。为什么在其他地方设置值不起作用超出了我的范围,但我确信这是有原因的。我添加了以下事件处理程序,一切都很好:
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView gridView = sender as DataGridView;
if (null != gridView)
{
gridView.Rows[e.RowIndex].HeaderCell.Value = gridView.Rows[e.RowIndex].Cells[0].Value;
Console.WriteLine("GridViewCell: " + gridView.Rows[e.RowIndex].Cells[0].Value);
}
}
回答by AMissico
If you place this code in the constructor, it will not work. Move the code into the form's Load
event and it should work fine. A common problem with Windows.Forms and C# is the use of improper initialization in the constructor. Many controls are not fully created until after the constructor finishes. (I forget why, but I believe it is because the handle is not created yet.) Many "work arounds" can be avoided, if you initialize in the Load
event as recommended by Microsoft.
如果将此代码放在构造函数中,它将不起作用。将代码移动到表单的Load
事件中,它应该可以正常工作。Windows.Forms 和 C# 的一个常见问题是在构造函数中使用了不正确的初始化。许多控件直到构造函数完成后才完全创建。(我忘记了为什么,但我相信这是因为尚未创建句柄。)如果您Load
按照 Microsoft 的建议在事件中进行初始化,则可以避免许多“变通办法” 。
回答by inam101
After assigning values to the datagridview, either by binding or manually, i did the following:
通过绑定或手动为 datagridview 分配值后,我执行了以下操作:
foreach (DataGridViewRow row in dgvValues.Rows)
{
row.HeaderCell.Value = (row.Index+1).ToString();
}
and it worked for me. I think there should be some even simpler workaround in Linq.
它对我有用。我认为在 Linq 中应该有一些更简单的解决方法。
回答by Abdulla Chozhimadathil
Just call ToString() as given below
只需调用 ToString() 如下所示
row.HeaderCell.Value = ("Row " + rowNumber).ToString();
回答by Mark Lakata
I used Karl's answer, but changed the event handler to RowsAdded, assuming the row header name is not changing once the row is created. The issue with CellFormatting is that it is called for every column, so it wastes some time setting the HeaderCell.Value over and over again.
我使用了 Karl 的答案,但将事件处理程序更改为 RowsAdded,假设行标题名称在创建行后不会更改。CellFormatting 的问题是每列都会调用它,因此会浪费一些时间一遍又一遍地设置 HeaderCell.Value。
private void dataGridViewVersions_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
DataGridView gridView = sender as DataGridView;
MyData vr = gridView.Rows[e.RowIndex].DataBoundItem as MyData ;
if (vr != null)
{
gridView.Rows[e.RowIndex].HeaderCell.Value = vr.RowName.ToString();
}
}