TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Binding DataGridView in Windows Application using C#
Pintoo Yadav
Apr 23
2015
Code
1.5
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
//This connection string is used to connect to database
String strConnection =
"Data Source=MYSYSTEM;Initial Catalog=Golivecode.com;Integrated Security=True"
;
/ / Establish SQL Connection
SqlConnection con =
new
SqlConnection(strConnection);
//Open database connection to connect to SQL Server
con.Open();
//Data table is used to bind the resultant data
DataTable dtusers =
new
DataTable();
// Create a new data adapter based on the specified query.
SqlDataAdapter da =
new
SqlDataAdapter(
"Select * from User_MST"
, con);
//SQl command builder is used to get data from database based on query
SqlCommandBuilder cmd =
new
SqlCommandBuilder(da);
//Fill data table
da.Fill(dtusers);
//assigning data table to Datagridview
dataGridView1.DataSource = dtusers;
//Resize the Datagridview column to fit the gridview columns with data in datagridview
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
con.Close();
Binding DataGridView
ASP.NET
DataGridView