Data Grid View is a data control which is used to display data from the database. Data Grid view Control has the many properties.
Binding of data grid view
Steps
Below steps shows how to bind a simple data from data base.
Step 1
First of all, one need to create a database in the SQL SERVER.
Step 2
Create a table and insert some values.
Step 3
Now, Create a Windows Application.
Step 4
Give the name for windows form.
Step 5
Drag and drop the data grid view onto the form and Give the Name.
Step 6
Now, Double click on the form and follow the below-mentioned code.
Add the below namespace under the namespace section.
[csharp]using System.Data.SqlClient;[/csharp]
Step 6
Now, write the below code in the load event.
[csharp] private void DataGridView_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=IQUICK/MSSQL;Initial Catalog=Sample;Persist Security Info=True;User ID=sa;Password=123");
SqlCommand cmd = new SqlCommand("select * from tbl_emp",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"tbl_emp");
dgvEmployee.DataSource = ds;
dgvEmployee.DataMember="Sample";
}[/csharp]
Step 7
Press f5 to run the application. Then, that's going bring a form as shown in the below figure.