Professional Documents
Culture Documents
Introduction:
In the last article we looked at few of the new controls available in Asp.net 2.0. We also talked
about Grid View Control which has replaced the Asp.net 1.1 DataGrid control for good. In this
article we will examine more features of the Grid View control. In this article we will see some of
the common operations that can be performed using the GridView control. Apart from these
operations there are many more functions that can be performed on the control and which we will
see in the later articles.
In this article I will be using the Northwind database which comes with SQL Server 2000. In the
last article I explained how easy it is to bind the Grid View control to the SqlDataSource. You can
perform all these actions using the wizard. If you want to code and bind the datasource you can
easily perform it using the following code sample:
if (!Page.IsPostBack)
{
BindData();
// 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"];
myGridView.DataSource = ds;
myGridView.DataBind();
You will need to set the Bound column in order to see the text in 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.
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:
At this moment if you try to select the Checkbox column, meaning if you try to check or uncheck
the Status column it won't work. Let's see how we can make it work with our grid view control.
In the above image you see that the Checkboxes are not marked. The reason is because in the
database the Checkbox column is NULL. If you make the column value to either 1 or 0 you will
see check marks where the value is 1. See the image below and you will have a better idea of
what I mean.
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
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.
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
The Cells start with index '0' which means Cell[0] is CategoryID , Cell[1] is CategoryName and so
on.
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.
if (row != null)
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.
myGridView.PageIndex = e.NewPageIndex;
BindData();
Here is a small image shown below of paging. I have limited the page size to 3 so that the
distribution of page will take place. In the image below we are in the page number 2.
You can also use the powerful SqlDataSource control to run and make your query. The
SqlDataSource also contains the QUERY builder
interface which can enhance the user experience in making SQL Queries. Apart from the
SELECT query there are also INSERT, DELETE and UPDATE query builders wizards that you
can use in your application.
When you use SqlDataSource you gets the advantage of having the paging, sorting implemented
automatically which is pretty cool in some cases.
In the next article we will see some more features of the Grid View control. I hope you liked the
article
Happy Programming !
The ASP.NET 1.x DataGrid control requires you to write a lot of custom code to handle
common operations such as paging, sorting, editing, and deleting data. For example,
while the DataGrid control can raise events when the user clicks to save or cancel
changes, it doesn't offer much more than that. If you want to store changes to a persistent
medium, such as a database, you have to handle the UpdateCommand event yourself,
retrieve changed values, prepare a SQL command, and then proceed from there to
commit the update.
The GridView control is the successor to the DataGrid and extends it in a number of
ways. First, it fully supports data source components and can automatically handle data
operations, such as paging, sorting, and editing, provided its bound data source object
supports these capabilities. In addition, the GridView control offers some functional
improvements over the DataGrid. In particular, it supports multiple primary key fields
and exposes some user interface enhancements and a new model for handling and
canceling events.
The GridView comes with a pair of complementary view controls: DetailsView and
FormView. By combining these controls, you can easily set up master/detail views using
very little code and sometimes no code at all
More information at
Asp.net 2.0
<connectionStrings>
<add name="ConnString" connectionString="Data Source=ServerName;Initial
Catalog=DBNAME;Integrated Security=True;"
providerName="System.Data.SqlClient"/>
connectionStrings>
Code Behind
Responses
Author: RamarajSundaram 26 Jul 2006Member Level: DiamondRating:
Points: 2
In .NET 2.0 we are using GridView. More or less Datagrid and GridView both are
same.
http://msdn2.microsoft.com/en-us/library/05yye6k9.aspx
You have some extra facilities and fetures in grid view than datagrid......
another key difference between the datagrid and gridview controls lies in the
adaptive user interface. unlike the version 1.1 datagrid, the gridview can render
on mobile devices, too. in other words, to build reports on mobile devices, you
can use the same grid control you would use for desktop pages. The datagrid in
version 2.0 can also render adaptively, but its UI capabilities are not quite as rich
as those of the gridview. ASP.NET 2.0, the datagrid control has been enhanced
to support common control features such as themes and personalization.
refer this link http://www.codeproject.com/aspnet/GridViewInsideGridView.asp
Grid view Control take care automatically deletes or updates records from the
datasource.
Both the DataGrid and GridView controls are derived from the WebControl class.
Although it has a similar object model to that of the DataGrid control, the
GridView control also has a number of new features and advantages over the
DataGrid control, which include:
Differences between the GridView control and the DataGrid control include:
gridView..
1.No code required.
2.No code required for PageIndexChanged.
3.Needs little code for update operation.
4.GridView supports events fired before and after database updates
Regards
Sridhar R
No