Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView Choose Column Types Dynamically
 

DataGridView Choose Column Types Dynamically

Hello!

Is it possible to have an empty DataGridView control with AutoGenerateColumns = true, and at run-time bind to a DataSource and depending on the DataType decide which DataGridViewColumn to use for each DataColumn?

/Miro
gunde  Tuesday, August 04, 2009 12:03 PM
private void loadDynamicGrid()
{
#region Code for filling datatable
//instance of datatable
DataTable dt = new DataTable();
//instance of datacolumn
DataColumn dc = new DataColumn("ID",typeof(System.Int32));
dc.AutoIncrement = true;
dt.Columns.Add(dc);
dc = new DataColumn("NAME", typeof(System.String));
dt.Columns.Add(dc);
for (int i = 0; i < 5; i++)
{
//creating instance for datarow
DataRow dr = dt.NewRow ();
dr["NAME"] = "ABC";
dr["ID"] = i;
dt.Rows.Add(dr);
}
#endregion
//loop through columns create boundfield dynamically
foreach (DataColumn col in dt.Columns)
{
BoundField bfield = new BoundField();
bfield.DataField = dc.ColumnName;
bfield.HeaderText = dc.ColumnName;
dngrview.Columns.Add(bfield);
}
dngrview.DataSource = dt;
dngrview.DataBind();
}
May be this can help you?
Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
  • Marked As Answer bygunde Tuesday, August 04, 2009 12:41 PM
  •  
_SuDhiR_  Tuesday, August 04, 2009 12:31 PM
private void loadDynamicGrid()
{
#region Code for filling datatable
//instance of datatable
DataTable dt = new DataTable();
//instance of datacolumn
DataColumn dc = new DataColumn("ID",typeof(System.Int32));
dc.AutoIncrement = true;
dt.Columns.Add(dc);
dc = new DataColumn("NAME", typeof(System.String));
dt.Columns.Add(dc);
for (int i = 0; i < 5; i++)
{
//creating instance for datarow
DataRow dr = dt.NewRow ();
dr["NAME"] = "ABC";
dr["ID"] = i;
dt.Rows.Add(dr);
}
#endregion
//loop through columns create boundfield dynamically
foreach (DataColumn col in dt.Columns)
{
BoundField bfield = new BoundField();
bfield.DataField = dc.ColumnName;
bfield.HeaderText = dc.ColumnName;
dngrview.Columns.Add(bfield);
}
dngrview.DataSource = dt;
dngrview.DataBind();
}
May be this can help you?
Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
  • Marked As Answer bygunde Tuesday, August 04, 2009 12:41 PM
  •  
_SuDhiR_  Tuesday, August 04, 2009 12:31 PM
When you bind a DataGridView control and set the AutoGenerateColumns property to true, columns are automatically generated using default column types appropriate for the data types contained in the bound data source.

Columnswhich are automatically generated are

DatagridViewTextBoxColumn ------Generated automatically when binding to numbers and strings
DataGridCheckBoxColumn --------Used with Boolean and CheckState values. Generated automatically when binding to values of these types

DataGridViewImageColumn -----Generated automatically when binding to byte arrays, Image objects, or Icon objects


if you want other columnsyou want to write your own code.

refer following link,
http://msdn.microsoft.com/en-us/library/bxt3k60s.aspx

NareshG  Tuesday, August 04, 2009 12:31 PM
Thanks! That seems to be a solution suitable for my needs. I'll give it a try.

Regards
Miro
gunde  Tuesday, August 04, 2009 12:43 PM
You are right NareshG. I began with that but the DataGridViewImageColumn gave me an "Parameter is not valid" error because my Byte[] columns contains Geometry-information and not a drawable Image object. Therefore I decided to write my own DataGridViewColumn to handle these DataColumns but wanted to be sure that I could useit in my DataGridView.

Regards
Miro
gunde  Tuesday, August 04, 2009 12:52 PM

You can use google to search for other answers

Custom Search

More Threads

• Force DataGridView update
• How do I set current cell in DVG when entering a cell
• BindingNavigator updateproblems
• Databinding and refill single row in datagrid from database
• How should I load this datarow?
• DataGrid With SortList DataSource (update datasouce problem)
• Add a DataGridViewComboBoxCell to the Insertion Row only
• Databindings pushing too much
• How do I call this.tableadapter.update without knowing which table adapter?
• Datagridview Combobox Problem...solution