Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > adding Rows Error
 

adding Rows Error

I want to add Rows in DataGridView Dynamicaly,,, I have written following code...



SqlDataAdapter
da = new SqlDataAdapter("select * from auth",cs);

SqlCommandBuilder cmb = new SqlCommandBuilder(da);

DataTable dt = new DataTable();

da.Fill(dt);

int found = dt.Rows.Count;

DataRow dr;

for (int i = 0; i <found; i++)

{

dr = dt.Rows[i];

dataGridView1.Rows.Add(dr);

}
_____________________________________

But this says that

"No row can be added to a DataGridView control that does not have columns. Columns must be added first."


Howto correct this?


NIIT HOUSE GKP INDIA Please mark the replies as answers if they help and unmark if they don't.
Nitin Chaudhary  Tuesday, August 04, 2009 4:40 PM
Hi Nitin Chaudhary,

As JohnGrove suggested, using databinding is quite easy in this situation. If you don't want to use that. You can first add column to DataGridView using DataGridView.Columns.Add() method. Then put a loop to through all the rows in DataTable and get data, add them to dataGridView using DataGridView.Rows.Add() method.

Look at the following sample
http://msdn.microsoft.com/en-us/library/zf3zx9fy.aspx
Pay attention to the "PopulateDataGridView" method code in the sample. That can meet your need.

Please tell me if you have anything unclear.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, August 06, 2009 3:41 AM

You should clone the structure of your populated DataTable and bind it to the DataGridView. Then you can add the items.

SqlDataAdapter da = new SqlDataAdapter("select * from auth",cs);
SqlCommandBuilder cmb = new SqlCommandBuilder(da);
DataTable dt = new DataTable();
da.Fill(dt);
DataTable structire = dt.Clone();
dataGridView1.DataSource = structure;

//The structure is only the columns. Now you can safely add the rows


John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Tuesday, August 04, 2009 5:37 PM

You should clone the structure of your populated DataTable and bind it to the DataGridView. Then you can add the items.

SqlDataAdapter da = new SqlDataAdapter("select * from auth",cs);
SqlCommandBuilder cmb = new SqlCommandBuilder(da);
DataTable dt = new DataTable();
da.Fill(dt);
DataTable structire = dt.Clone();
dataGridView1.DataSource = structure;

//The structure is only the columns. Now you can safely add the rows


John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Tuesday, August 04, 2009 5:37 PM
Why not just bind the DataTable?
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Tuesday, August 04, 2009 5:39 PM
Hi Nitin Chaudhary,

As JohnGrove suggested, using databinding is quite easy in this situation. If you don't want to use that. You can first add column to DataGridView using DataGridView.Columns.Add() method. Then put a loop to through all the rows in DataTable and get data, add them to dataGridView using DataGridView.Rows.Add() method.

Look at the following sample
http://msdn.microsoft.com/en-us/library/zf3zx9fy.aspx
Pay attention to the "PopulateDataGridView" method code in the sample. That can meet your need.

Please tell me if you have anything unclear.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, August 06, 2009 3:41 AM

You can use google to search for other answers

Custom Search

More Threads

• Data Rowview - Adding new row
• Sorting a gridview with a custom datasource
• DatagridviewCombobox programaticly select an Item
• Can datagridview contain treeview like items? (image included)
• Display Images in dataGridView problem?
• Copy the Records from one Table to another Table
• DataGridView acting some kind of wierd
• Creating A Dot Net Windows Summary to Detail Application
• Dynamic Database Connection
• Select rows using keyboard in DataGridView