How do you bind data in the DataGrid?

Answer

Binding Data in DataGrid means how we show the data from a table or a relation in our DataGrid.

Suppose We want to show the data of "Customers" table of "Northwind" database present in SQL Server on the Button Click. Now Follow the given steps....

step 1: Drag a DataGrid and a Button control on the Page.
step 2: In the Code behind Use the namespace as
using System.Data.SqlClient;
step 3: Write the following code in the Button's Click event handler...

protected void Button1_Click(object sender,EventArgs e)
{
SqlConnection con=new SqlConnection ("server=rajkumar;uid=sa;pwd=kumar;database=northwind) ;
SqlDataAdapter da_show=new SqlDataAdapter (" select ProductName, UnitPrice, UnitsInStock from Products", con);
DataSet ds=new DataSet;
da_show.Fill(ds, "Products");
GridView1.DataSource=ds.Tables["products"];
GridView1.DataBind( );
}

This will show all the rows and selected column in the DataGrid. The Column names in the DataGrid will be same as attributes name in the database table.

How do you customize the DataGrid...

Here I will show you how to Display only Selected Column in the DataGrid even when you selected all the columns in your Query in Code Behind file. Also I will show you how to change the Name of the Column in the DataGrid.

Suppose I want to Display only ProductName and UnitPrice from the table and Also Want To display ProductName as Product Name and UnitPrice as Price......So

Step 1 : In the Code behind file the code will be same as above.
Step 2 : Set the Property AutoGenerateColumns of DataGrid to false.
Step 3 : Go to source View and Insert TemplateColumns in the datagrid tag.

All DATA GRID Questions

Ask your interview questions on data-grid

Write Your comment or Questions if you want the answers on data-grid from data-grid Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---