如何手动将数据添加到dataGridView?

时间:2011-05-23 03:09:52

标签: c# .net datagridview

我正在尝试运行此代码,但我得到一个例外:

  

指数超出范围。一定是   非负和小于的大小   集合。参数名称:index

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)
    {
        DataGridViewRow row = new DataGridViewRow();

        row.Cells[0].Value = i.ToString();
        row.Cells[1].Value = student.LastNameFather + " " + student.LastNameMother + ", " + student.Name;

        dataGridView1.Rows.Add(row);
        i++;
    }
}

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

5 个答案:

答案 0 :(得分:27)

你只是错过了一行:-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);

答案 1 :(得分:7)

简单,

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。

答案 2 :(得分:3)

新创建的行中有0个单元格,这就是您获得该异常的原因。您不能使用

之类的语句
row.Cells[0].Value = i.ToString();

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

答案 3 :(得分:2)

我的版本:

                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++;
                };

感谢您的回答。

答案 4 :(得分:1)

简单版本:

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