简体   繁体   English

DataGridView显示行标题单元格

[英]DataGridView display row header cell

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. 我正在尝试显示一个链接到DataTable的简单DataGridView,我想最终将DataTable中的第一列作为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. 我可以显示DataGridView包含我的所有行和列,以及列标题单元格,但没有行标题单元格。 I check the value in the row.HeaderCell.Value, and the data I put there is there. 我检查了row.HeaderCell.Value中的值,我放在那里的数据就在那里。 I check row.HeaderCell.Displayed and it is false, but this is read only, so I can't make it true. 我检查row.HeaderCell.Displayed并且它是false,但这是只读的,所以我不能使它成为现实。 How do I make the row header cell display? 如何显示行标题单元格?

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);

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. 将代码移动到窗体的Load事件中,它应该可以正常工作。 A common problem with Windows.Forms and C# is the use of improper initialization in the constructor. Windows.Forms和C#的一个常见问题是在构造函数中使用了不正确的初始化。 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. (我忘记了原因,但我认为这是因为尚未创建句柄。)如果按照Microsoft的建议在Load事件中初始化,则可以避免许多“解决方法”。

只需调用ToString(),如下所示

 row.HeaderCell.Value = ("Row " + rowNumber).ToString();

The answer appears to be handling the DataGridView.CellFormatting event. 答案似乎是处理DataGridView.CellFormatting事件。 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: 我添加了以下事件处理程序,一切都很好:

    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);

        }
    }

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. 我使用了Karl的答案,但是将事件处理程序更改为RowsAdded,假设行标题名称在创建行后没有更改。 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. 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();
        }
    }

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应该有一些更简单的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM