.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Bind DataGrid

Posted By:Deepika Haridas       Posted Date: October 01, 2009    Points: 10    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

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;


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend