Bind Datagrid

Bubbly
Posted by Bubbly under Windows Forms category on | Views : 1840
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()
{
try
{
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;
this.dataGridView1.DataBindings.Control.CreateControl();

}
catch
{
}
}



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


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

dtBDGrid.TableStyles.Add(tabStyle);
tabStyle.GridColumnStyles[0].Width = 125;

Comments or Responses

Login to post response