How can I manually add data to a dataGridView?

前端 未结 5 626
梦谈多话
梦谈多话 2020-12-16 19:23

I\'m trying to run this code, and I get an exception:

Index was out of range. Must be non-negative and less than the size of the collection. Param

相关标签:
5条回答
  • 2020-12-16 19:51

    My version of this:

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

    Thanks for your answers.

    0 讨论(0)
  • 2020-12-16 19:58

    Simple version:

    dataGridView1.Rows.Add(new object[] { cell0Value, cell1Value });
    

    My extension that I like to use:

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-16 20:01

    Its simple,

    myDataGridView.Rows.Add(value1, value2, value3...);
    

    It worked when I had configured my DGV previously for the coming data columns through the GUI. So in your case, it would be like:

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

    May be you have to configure the DGV for the columns and their names separately.

    0 讨论(0)
  • 2020-12-16 20:04

    You are just missing one line :-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);
    
    0 讨论(0)
  • 2020-12-16 20:06

    There are 0 cells in the newly created row, that's why you are getting that exception. You cannot use statements like

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

    unless you manually add cells to the blank row.

    0 讨论(0)
提交回复
热议问题