Binding to Datagrid

Most simpler way to bind your data values to datagridview in Windows Application.
Just need to call the method and your datagrid is bound.

public void bindgrid()
DataSet ds = new DataSet();
string strconnection = "Dsn=test; uid=root;";
OdbcConnection Conn = new OdbcConnection(strconnection);
DataTable dt;
OdbcDataAdapter ad;
ds = new DataSet();
ad = new OdbcDataAdapter("select * from sample", Conn);
ad.Fill(ds, "sample");

dt = ds.Tables[0];
this.dataGridView1.DataSource = dt.DefaultView;


Also if u need to set the column width programmatically then u need to add

DataGridTableStyle tabStyle = new DataGridTableStyle();
tabStyle.MappingName = dt.TableName;

tabStyle.GridColumnStyles[0].Width = 125;


Author: Praveen13 May 2009 Member Level: Gold   Points : 2


For Width you can directly give


then why to give for DataGridTableStyle Class

And as your are selecting data from database then all the columns are of type textboxes then why to call databingings method
can u plz send me the exact definition of the code

Author: Deepika Haridas13 May 2009 Member Level: Gold   Points : 0


And In windows sometimes it doesn't allow you to apply Width the way which you said so This is another method of assigning Width.

Hope you got it.


Author: Kirankumar Potnuru15 May 2009 Member Level: Gold   Points : 1

Do you have any idea for data binding for data grid at device aplication (compact framework 2.0 or greater versions) and data is coming from xml not from data base......!!!!

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: