Pages

28 Aug 2013

Asp.net interview questions on gridview C#, vb.net

What is DataGrid...

We can define DataGrid as a Powerfull WebServer tool which is used for displaying information from a DataSource in a tabular form an enables us to perform Sorting, Paging and Editing by setting a few properties and creating a couple of event handlers.

How Web DataGrid is Different from Windows DataGrid Control...

The WebPage DataGrid control is Different from Windows DataGrid in entire programming paradigm. Let's see...

* Web DataGrid Control performs a round trip to the server.
* Web DataGrid manage State.
* Windows DataGrid Control Supports master-detail data structure while web Datagrid doesn't.
* In Web DataGrid ,you can Edit only one row at a time.
* Web DataGrid Does not inherently support sorting, but raises events while Windows DataGrid does support.
* Web DataGrid supports Paging.
* You can bind Web DataGrid Control to any object that supports the IEnumerable interface.

How do you bind data in the DataGrid?

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.

No comments:

Post a Comment