在DataGridView窗口中创建行表单应用程序

时间:2018-03-07 04:19:12

标签: c# windows forms datagridview

private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 65; i < 91; i++)
        {
            DataGridViewColumn col = new DataGridViewColumn();
            col.Name = Convert.ToString((char)i);
            col.HeaderText = col.Name;
            dataGridView1.Columns.Add(col);
        }

        for (int i = 0; i < 50; i++)
        {
            DataGridViewRow row = new DataGridViewRow();
            row.HeaderCell.Value = (i + 1).ToString();
            for (int j = 0; j < dataGridView1.ColumnCount; j++)
            {
                row.Cells[j].Value = " ";
            }
            dataGridView1.Rows.Add(row);
        }
    }

上面我有我的代码,我试图用空字符串值填充每个行单元格。问题是我让列显示但行没有填充...这是一个输出屏幕的图像与上面的代码:

enter image description here

非常感谢任何帮助正确填充行的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

有简单的方法可以做到这一点,请尝试这个

this.dataGridView1.Rows.Add("five", "six", "seven","eight");

或者

this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");

如果你已经有一行,那么

DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
row.Cells["a"].Value = "Name";
row.Cells["b"].Value = 10.2;
yourDataGridView.Rows.Add(row);