Adding datagridview columns
-
Hello, I need to add datagridviewcolumns in a loop. For example:
string[] default_columns { "VlFile", "EquipName", "JobName", "NickName", "Operator", "EquipmentStartTime", "JobComment", "EquipTypeDesc" };
foreach (string s in default_columns)
{
//Here i need to add the a column with the name in S
}The columns need to be bounded. I have the bound source and all this but the problem is adding the columns. It will loop me through the array (default_columns) and will add this columns. How can i do it? I mean there is a problem because i need to declare a variable
-
Hello, I need to add datagridviewcolumns in a loop. For example:
string[] default_columns { "VlFile", "EquipName", "JobName", "NickName", "Operator", "EquipmentStartTime", "JobComment", "EquipTypeDesc" };
foreach (string s in default_columns)
{
//Here i need to add the a column with the name in S
}The columns need to be bounded. I have the bound source and all this but the problem is adding the columns. It will loop me through the array (default_columns) and will add this columns. How can i do it? I mean there is a problem because i need to declare a variable
I would create a datatable and point the datagridview to it
DataTable DT = new DataTable();
foreach (string s in default_columns)
{
DataColumn NewCol = new DataColumn(s);
DT.Columns.Add(NewCol);
}DataGridView.DataSource = DT;
Regards Mick Curley :)
-
I would create a datatable and point the datagridview to it
DataTable DT = new DataTable();
foreach (string s in default_columns)
{
DataColumn NewCol = new DataColumn(s);
DT.Columns.Add(NewCol);
}DataGridView.DataSource = DT;
Regards Mick Curley :)
OK but i load data to the datagridview from a database. And i need the columns to be bound to the database. I declared the database (datatable) as a bindingsource to the datagridview. But i need to add columns in the program (the user will choose which columns he wants to see)
-
OK but i load data to the datagridview from a database. And i need the columns to be bound to the database. I declared the database (datatable) as a bindingsource to the datagridview. But i need to add columns in the program (the user will choose which columns he wants to see)
I dont understand what your trying to do. If you load the data from your Database table into a DataTable haven't you got a reference to the columns within that DataTable?
Regards Mick Curley :)
-
I dont understand what your trying to do. If you load the data from your Database table into a DataTable haven't you got a reference to the columns within that DataTable?
Regards Mick Curley :)
I have an empty datagridview (without columns declared- in the wizard of the datagridview). The user choses which columns he wants to see from the database (using a listbox that i save the items in an array). I want to add columns to the datagridview from this array (that was created from the listbox). The problem is that i need them to be bound to that datatable. So i do in the foreach loop datagridview.Columns.Add(s, s); it creates me the columns right but they are not filled with data from the database. If i do: datagridviewcolumn col=new datagridviewtextboxcolumn(); col.headertext="abc"; col.datapropertyname="abc"; col.name="abc"; It does creates me the column and filles it with data. But i need to do it in a loop that will go through all the items in the array (from the listbox) and add this columns But i can use the same name to the datagridviewcolumn (datagridviewcolumn col=new datagridviewtextboxcolumn();), Because if i do it it says i already have this column (even if i change it's name in the loop)
-
I have an empty datagridview (without columns declared- in the wizard of the datagridview). The user choses which columns he wants to see from the database (using a listbox that i save the items in an array). I want to add columns to the datagridview from this array (that was created from the listbox). The problem is that i need them to be bound to that datatable. So i do in the foreach loop datagridview.Columns.Add(s, s); it creates me the columns right but they are not filled with data from the database. If i do: datagridviewcolumn col=new datagridviewtextboxcolumn(); col.headertext="abc"; col.datapropertyname="abc"; col.name="abc"; It does creates me the column and filles it with data. But i need to do it in a loop that will go through all the items in the array (from the listbox) and add this columns But i can use the same name to the datagridviewcolumn (datagridviewcolumn col=new datagridviewtextboxcolumn();), Because if i do it it says i already have this column (even if i change it's name in the loop)
I think i managed the problem. I had to declare the datagridviewcolumn (datagridviewcolumn col=new datagridviewtextboxcolumn();) inside the foreach loop (i declared it outside) and in this way it declares a ned datagridviewcolumn for each item in the loop.
-
I think i managed the problem. I had to declare the datagridviewcolumn (datagridviewcolumn col=new datagridviewtextboxcolumn();) inside the foreach loop (i declared it outside) and in this way it declares a ned datagridviewcolumn for each item in the loop.
Good to hear..
Regards Mick Curley :)