【问题标题】:How can I manually add data to a dataGridView?如何手动将数据添加到 dataGridView?
【发布时间】:2019-04-17 17:07:21
【问题描述】:

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

索引超出范围。一定是 非负且小于 集合。参数名称:索引

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 中手动创建了列,现在我想使用这个小方法填充字段。

【问题讨论】:

  • 您确定有可用于datagridview1 的列吗?
  • 什么意思 - “我在 datagridview 中手动创建了列”?您的意思是您在放置在表单上的 DataGridView 实例的“列”属性表中定义了列,还是您的意思是您在其他地方使用 C# 代码定义了列?

标签: c# .net datagridview


【解决方案1】:

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

【讨论】:

  • 这个答案依赖于程序员在表单的 DataGridView 实例中定义了列(称为“dataGridView1”)。对 CreateCells 方法的调用依赖此定义来设置新行的列。对于寻求完全以编程方式定义 DataGridView 的人来说,您首先需要定义列。见 SO@stackoverflow.com/questions/5524075/…
【解决方案2】:

很简单,

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。

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      我的版本:

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

      感谢您的回答。

      【讨论】:

        【解决方案5】:

        简单版:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-13
          • 1970-01-01
          相关资源
          最近更新 更多