C# 如何向dataGridView手动添加数据?

C# 如何向dataGridView手动添加数据?,c#,.net,datagridview,C#,.net,Datagridview,我正在尝试运行此代码,但出现异常: 索引超出范围。必须是 非负数且小于 收藏。参数名称:索引 我在datagridview中手动创建了列,现在我想用这个小方法填充字段。新创建的行中有0个单元格,这就是为什么会出现异常。不能使用这样的语句 row.Cells[0].Value = i.ToString(); 除非手动将单元格添加到空白行中。 简单版本: dataGridView1.Rows.Add(new object[] { cell0Value, cell1Value }); 我喜欢使用的

我正在尝试运行此代码,但出现异常:

索引超出范围。必须是 非负数且小于 收藏。参数名称:索引


我在datagridview中手动创建了列,现在我想用这个小方法填充字段。

新创建的行中有0个单元格,这就是为什么会出现异常。不能使用这样的语句

row.Cells[0].Value = i.ToString();

除非手动将单元格添加到空白行中。

简单版本:

dataGridView1.Rows.Add(new object[] { cell0Value, cell1Value });
我喜欢使用的扩展名:

static class DataGridViewExtension
{
    public static void AddCustomRow(this DataGridView dgv, object [] values, object tag = null)
    {
        int Index = dgv.Rows.Add(values);

        // Add a tag if one has been specified
        if (tag != null)
        {
            DataGridViewRow row = dgv.Rows[Index];
            row.Tag = tag;
        }
    }

    public static void AddCustomRow(this DataGridView dgv, string text, object tag = null)
    {
        AddCustomRow(dgv, new object[] { text }, tag);
    }
}
很简单,

myDataGridView.Rows.Add(value1, value2, value3...);
当我之前通过GUI为即将到来的数据列配置DGV时,它就工作了。 因此,在您的情况下,它将类似于:

private void LoadStudentGrades(int gradeParaleloId, int subjectId)
{
    GradeStudentRepository gradeStudentRepo = new GradeStudentRepository();
    students = gradeStudentRepo.FindAllGradeStudents().Where(g => g.GradeParaleloId == gradeParaleloId).Select(g => g.Student);

    int i = 1;
    foreach (var student in students)
    {
        dataGridView1.Rows.Add(i.ToString(), student.LastNameFather + " " + student.LastNameMother + ", " + student.Name);
        i++;
    }
}

可能您必须分别为列及其名称配置DGV。

您只缺少一行:-p

DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView1);  // this line was missing
row.Cells[0].Value = "Cell1";
row.Cells[1].Value = "Cell2";
dataGridView1.Rows.Add(row);
我的版本是:

                OracleCommand cmd = new OracleCommand(commandText, _oraConn);
                OracleDataReader dr = cmd.ExecuteReader();

                int i = 0;
                while (dr.Read())
                {
                    DataGridViewRow row = new DataGridViewRow();
                    row.CreateCells(dataGridView1);


                    row.Cells[0].Value = dr.GetValue(0).ToString();
                    row.Cells[1].Value = dr.GetValue(1).ToString();
                    row.Cells[2].Value = dr.GetValue(2).ToString();
                    row.Cells[3].Value = dr.GetValue(3).ToString();
                    row.Cells[4].Value = dr.GetValue(4).ToString();
                    row.Cells[5].Value = dr.GetValue(5).ToString();

                    dataGridView1.Rows.Add(row);

                    //MessageBox.Show( dr.GetValue("vz_id").ToString());
                    i++;
                };

谢谢你的回答

你确定有列可用于
datagridview 1
?你是什么意思-“我在datagridview中手动创建了列”?您是指您在表单上放置的DataGridView实例的“columns”属性表中定义了列,还是指您在其他地方使用C代码定义了列?这个答案取决于程序员在表单上的DataGridView实例(称为“dataGridView1”)中定义了列。对CreateCells方法的调用依赖于此定义来设置新行的列。对于寻求定义DataGridView的完全编程方式的人,首先需要定义列。看到了吗@
                OracleCommand cmd = new OracleCommand(commandText, _oraConn);
                OracleDataReader dr = cmd.ExecuteReader();

                int i = 0;
                while (dr.Read())
                {
                    DataGridViewRow row = new DataGridViewRow();
                    row.CreateCells(dataGridView1);


                    row.Cells[0].Value = dr.GetValue(0).ToString();
                    row.Cells[1].Value = dr.GetValue(1).ToString();
                    row.Cells[2].Value = dr.GetValue(2).ToString();
                    row.Cells[3].Value = dr.GetValue(3).ToString();
                    row.Cells[4].Value = dr.GetValue(4).ToString();
                    row.Cells[5].Value = dr.GetValue(5).ToString();

                    dataGridView1.Rows.Add(row);

                    //MessageBox.Show( dr.GetValue("vz_id").ToString());
                    i++;
                };