DataGridView中显示行标题单元格 [英] DataGridView display row header cell

查看:171
本文介绍了DataGridView中显示行标题单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个简单的DataGridView链接到一个数据表,我想,最终,在数据表我的第一列是在DataGridView行标题单元格。在这一点上,我会满足于有行标题单元格的任意值。我可以显示在DataGridView我所有的行和列,并与列标题细胞,但没有行标题单元格。我检查值在row.HeaderCell.Value,我把那里的数据是存在的。我检查row.HeaderCell.Displayed,这是假的,但是这是只读的,所以我不能让真正的。如何让我的行标题单元格显示?

下面是一个什么样我试图得到这个工作的一个简单的示例:

 数据表表=新的DataTable();
        的for(int i = 0;我小于10;我++)
        {
            table.Columns.Add(新的DataColumn(column-+ i)段);
        }

        的for(int i = 0;我小于10;我++)
        {
            DataRow的theRow = table.NewRow();

            为(诠释J = 0; J&小于10; J ++)
                theRow [J]。= I + - + J;
            table.Rows.Add(theRow);

        }

        dataGridView1.DataSource =表;
        dataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
        INT rowNumber = 1;
        的foreach(的DataGridViewRow行dataGridView1.Rows)
        {
            如果(row.IsNewRow)继续;
            row.HeaderCell.Value =行+ rowNumber;
            rowNumber = rowNumber + 1;
        }
        dataGridView1.AutoResizeRowHeadersWidth(
            DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
 

解决方案

如果你把这个code在构造函数中,它不会工作。将code到窗体的加载事件,它应该工作的罚款。一个常见的​​问题Windows.Forms的和C#是在构造函数中使用不正确的初始化。许多控件都是不完全,直到构造完成后创建。 (我忘了为什么,但我相信这是因为手柄没有创建。)许多变通是可以避免的,如果你在加载事件初始化推荐微软。

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

解决方案

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.

这篇关于DataGridView中显示行标题单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆