Bind Datagrid

Posted by Bubbly under Windows Forms category on | Views : 1791
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;

Comments or Responses

Login to post response