views:

2948

answers:

4

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?

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);
+1  A: 

Try the DataGridView.RowHeadersVisible property.

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowheadersvisible.aspx

That turns the header cell on and off in it's entirety, my problem was that the value I put in the cell was not showing up in the display. Figured it out below.
A: 

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:

    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 believe this is some kind of bug. In VB.NET, you can set `HeaderCell.Value` as needed. Yet, your have to resort to your work around in C#.
Fred F.
This is because the code is run from constructor. Move code to Load event or other event handler.
AMissico
+1  A: 

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.

AMissico
A: 

I have a similar problem. The header cell is readonly when operating system display style is XP. However, when the operating system display style is classic header cell is not read only. Because, of this problem I am able to change the row header color successfully in classic display style but not in XP display style. I am changing the row header background in cell formatting event. I am using Mirsoft C# .net 2.0