Asp .net 2.0 GridView control

We all know about powerful data bound control DataGrid in ASP.NET 1.x. Despite its amazing programming interface and versatility, DataGrid has its own shortcomings.

Shortcomings of DataGrid (ASP.NET 1.x)

  • DataGrid requires you to write custom code for handling common operations like sorting, paging and manipulation of data in DataGrid
  • when bound to DataSource control can only support select operation on DataSource. Updating DataSource through DataGrid can be done only through custom ADO.NET code
  • DataGrid supports a restricted event model
  • DataGrid does not support adaptive rendering on different platforms
Above limitations apply only to ASP.NET 1.x. ASP.NET 2.0 enhanced DataGrid Control is available which overcomes some of above mentioned shortcomings

GridView Control Features
  • Enhanced data source binding capabilities (Direct interaction with DataSource with any writing any ADO.NET code)
  • Built-in support for sorting and paging functionalities
  • Improved Design time features(Smart Panel Tag)
  • Customized pager user interface with PagerTemplate property
  • Additional Column types(ImageField)
  • New Event model with support for pre event and post event operations

Adding a SqlDataSource and binding to GridView Control

Here is a method by which we can bind a database to a grid view .below code helps u a lot.

void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{

// Binds the data

BindData();

}

}

public void BindData()
{

// you can use this line

string connectionString = myDataSource.ConnectionString;

// you can also use this line since by using the SqlDataSource it makes an entry in the
// web.config file if that is what choose to do
//string connectionString = (string)ConfigurationSettings.AppSettings["ConnectionString"];
// if you are using Enterprise Library you can also use
//string connectionString = (string)ConfigurationSettings.ConnectionStrings["ConnectionString"];

SqlConnection myConnection = new SqlConnection(connectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Categories", myConnection);

DataSet ds = new DataSet();

ad.Fill(ds, "Categories");

myGridView.DataSource = ds;

myGridView.DataBind();

}
You will need to set the Bound column in order to see the textin the cells of the grid view control. This can be easily done by using the Smart tag which appears when you right click on the grid view control.
For you people i have also added a Status column to show you the checkbox column feature. The Status column is a simple bit field whose value can be either 1 or 0. If you are using Access database you can choose Yes/No Column. Once you have set up all your columns and execute the page it will display something like this below


Editing in the Grid View Control

You can easily add the editing, updating, selecting, paging and sorting capabilities to your grid view control. Just add a new command column and you will be asked to add whatever functionality you desire.

If you are using the SqlDataSource to bind to the GridView control you will get most of the features by just using the SqlDataSource wizard. Since we are binding the control at runtime we need to add most of the functionality manually.

Edit Mode

The fields that are marked readonly = false will change into TextBoxes when the Row_Editing event triggers. Like DataGrid control its easy to set the fields in the edit mode.

// Editing mode

void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
{

myGridView.EditIndex = e.NewEditIndex;

BindData();

}
Cancel Edit

If you don't like to edit the row you can press cancel link button which will fire the RowCancelingEdit event. The code to turn the row back to its original form is quite simple and straight forward.

// Cancel Edit Mode

void myGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{

myGridView.EditIndex = -1;

BindData();

}
Selecting Row

Selecting row event is fired when you make a click on the select link. If you need any particular item in that row you can easily select it using the cells property

// Selecting row

void myGridView_SelectedIndexChanged(object sender, EventArgs e)
{

// This will contain the selectedValue of the row

string selectedCategory = myGridView.SelectedRow.Cells[1].Text;

}
Update Row

For Updating the row we first need to get the value from the row that is entered by the user
into the TextBox. For this purpose you can change the bound column into a Template column which will make is easier to locate the item.After changing the CategoryName column to a tem plated column we can easily use the RowUpdating event to find the Text entered by the user.

void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

GridViewRow row = myGridView.Rows[e.RowIndex];

if (row != null)

{

TextBox t = row.FindControl("TextBox1") as TextBox;

if (t != null)

{

Response.Write("The Text Entered is" + t.Text);
}
}
Paging

Paging can also be easily enabled in the grid view control. Its just like DataGrid from Asp.net 1.1. First you need to make set the allow paging property to true. And you can also set the page size. Here is a small code sample that will enable paging.
// Grid View Paging

void myGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

myGridView.PageIndex = e.NewPageIndex;

BindData();

}

0 comments:

Template by - Mathew | Mux99