Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manually add data to a dataGridView?

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. Parameter name: 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++;
    }
}

I manually created the columns in the datagridview, and now I would like to populate the fields using this small method.

like image 443
Only Bolivian Here Avatar asked May 23 '11 03:05

Only Bolivian Here


People also ask

How do you add data to DataGrid view?

To bind the control to a data source ) on the upper-right corner of the DataGridView control. Click the drop-down arrow for the Choose Data Source option. If your project does not already have a data source, click Add Project Data Source and follow the steps indicated by the wizard.

How do you add columns in DataGrid?

To add a column using the designer ) on the upper-right corner of the DataGridView control, and then select Add Column. In the Add Column dialog box, choose the Databound Column option and select a column from the data source, or choose the Unbound Column option and define the column using the fields provided.


4 Answers

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);
like image 135
Hamza Anjum Avatar answered Sep 18 '22 05:09

Hamza Anjum


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.

like image 31
Nikhil Girraj Avatar answered Sep 18 '22 05:09

Nikhil Girraj


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.

like image 36
Mamta D Avatar answered Sep 20 '22 05:09

Mamta D


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.

like image 39
Marko Leben Avatar answered Sep 19 '22 05:09

Marko Leben