简体   繁体   English

手动将行值添加到Gridview

[英]Manually Add Row Values to Gridview

I have a winform app, where in i have a grid view that is supposed to search data from database and display to a gridview. 我有一个winform应用程序,其中有一个网格视图,该视图应该从数据库搜索数据并显示到Gridview。

here is my code to create columns and add row values to it.. 这是我的代码,用于创建列并向其中添加行值。

                while (dr.Read())
                {
                    DataGridViewColumn ad = new DataGridViewColumn();
                    DataGridViewCell cell = new DataGridViewTextBoxCell(); //Specify which type of cell in this column
                    ad.CellTemplate = cell;
                    ad.HeaderText = "Serial No";
                    ad.Name = "Serial No";
                    ad.Visible = true;
                    dataGridView2.Columns.Add(ad);
                    DataGridViewColumn ad1 = new DataGridViewColumn();
                    DataGridViewCell cell1 = new DataGridViewTextBoxCell(); //Specify which type of cell in this column
                    ad1.CellTemplate = cell1;
                    ad1.HeaderText = "Enrollment No.";
                    ad1.Name = "Enrollment No.";
                    ad1.Visible = true;
                    dataGridView2.Columns.Add(ad1);
                    DataGridViewColumn ad2 = new DataGridViewColumn();
                    DataGridViewCell cell2 = new DataGridViewTextBoxCell(); //Specify which type of cell in this column
                    ad2.CellTemplate = cell1;
                    ad2.HeaderText = "Student Name";
                    ad2.Name = "Studen Name";
                    ad2.Visible = true;
                    dataGridView2.Columns.Add(ad2);
                    DataGridViewRow row = new DataGridViewRow();
                    row.CreateCells(dataGridView2);
                    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);
                    i++;
                }

i have created 3 columns and i'm trying to store three values from database to the three corresponding cells. 我已经创建了3列,并且我试图将数据库中的三个值存储到三个对应的单元格中。

but while executing i get the following error 但是在执行时出现以下错误

No row can be added to a DataGridView control that does not have columns. 没有行可以添加到没有列的DataGridView控件中。 Columns must be added first. 必须首先添加列。

what am i doing wrong? 我究竟做错了什么? Please let me know. 请告诉我。 thanks in advance for all your help... 预先感谢您的所有帮助...

Problem : You are Adding the columns to dataGridView2 but finally you are trying to add the rows to dataGridView1 . 问题:您正在将列添加到dataGridView2但是最后您尝试将行添加到dataGridView1

Solution : if your intention isto add the rows to the dataGridView2 replace the follwing statement 解决方案:如果您打算将行添加到dataGridView2替换以下语句

Replace This: 替换为:

dataGridView1.Rows.Add(row);

With This: 有了这个:

dataGridView2.Rows.Add(row);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM