Windows Application - SPLessons

Win App Grid view Binding

Home > Lesson > Chapter 22
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Win App Grid view Binding

Windows Application Grid view Binding

shape Description

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

shape Steps

Below steps shows how to bind a simple data from data base.

shape Step 1

First of all, one need to create a database in the SQL SERVER.

shape Step 2

Create a table and insert some values.

shape Step 3

Now, Create a Windows Application.

shape Step 4

Give the name for windows form.

shape Step 5

Drag and drop the data grid view onto the form and Give the Name.

shape 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]

shape 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]

shape Step 7

Press f5 to run the application. Then, that's going bring a form as shown in the below figure.