Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
No row can be added to a DataGridView control that does not have columns. Columns must be added first
C#
try
            {
                dgv.ReadOnly = true;
                CommonFunc.FormatGridLook(dgv, false, false, false);
                Purchasefecade facade = new Purchasefecade();

              List<purchaseentity> entityList = facade.GetPurchase(Convert.ToString(TxtPurNo.Text.Trim()));
              
               foreach (PurchaseEntity entity in entityList)
               {
                   dgv.Rows.Add(new object[] { entity.ProductId });
               }
            }
          
               // }
          
            catch
            {
                dgvPrd1.DataSource = null;
            }</purchaseentity>
Posted
Updated 2-May-13 23:05pm
v2
Comments
CHill60 3-May-13 5:07am    
That is correct. What is your problem though?
Amarender1479 3-May-13 5:10am    
when i was excute foreachloop not working and the error is "no row can be added to a DataGridView control that does not have columns. Columns must be added first".
why this error came pls help me
CHill60 3-May-13 5:13am    
You're getting the error because you haven't defined any columns in your data grid view - this can either be done at design time, or at run-time but has to be done before you can add a row
Amarender1479 3-May-13 5:15am    
In design time data grid appear but when i was bind with data this error came...
Amarender1479 3-May-13 8:28am    
I got the answer. thank you sir...........

1 solution

You are getting the error because you have not defined any columns for the datagridview.
You could do this at design time by right-click on the control and choosing "Add column..." but this would tightly bind your UI to the current implementation of facade.GetPurchase so is not the best solution.
You could also add the columns programatically e.g. dgv.Columns.Add(... but again this is not recomemended because if your purchaseentity class changes you have to make changes elsewhere (breaks the principle of encapsulation for example).
You are not actually binding data to the control - with your foreach loop you are trying to create individual rows.
My advice would be to convert your List<purchaseentity></purchaseentity> to a DataTable and then use
dgv.DataSource = dt.DefaultView;

If you are not in a position to change facade.GetPurchase to return a DataTable instead of a List then you can convert the List using the following function
public DataTable ConvertToDataTable<T>(IList<T> data)
{
	// with thanks to Sai Kumar Koona 
	//http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/6ffcb247-77fb-40b4-bcba-08ba377ab9db
	PropertyDescriptorCollection properties =
	   TypeDescriptor.GetProperties(typeof(T));
	DataTable table = new DataTable();
	foreach (PropertyDescriptor prop in properties)
		table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
	foreach (T item in data)
	{
		DataRow row = table.NewRow();
		foreach (PropertyDescriptor prop in properties)
			row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
		table.Rows.Add(row);
	}
return table;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900