There are many scenarios in which the data can be bound in a
Gridview, in which
various operation other than provided by default by
Gridview needs to be implemented.
In this article we will try to bind such operations with some more features.
This article describes various events of the
Gridview and in addition some more
functionalities which are needed in most scenarios but not provided by ASP.NET
control by default. This article is written for beginner's i.e. We will be using
a lot of looping and basic language constructs and most of the code can be optimized.
Using the code
We will be implementing the following additional functionalities with a data bound
- Getting the sum of any column in footer of gridview.
- Possibility to check few or all records in gridview for bulk operations.
Lets start by looking at the Database schema which we will be using in our demo
implementation. We have a simple Database with a single table named
consists of 4 columns
In the table shown above:
- ID which is an
- uname which is
- totalMarks which is of
float datatype and the last one is
- selectedItem with
First we will create a function named
BindGridView() in which the details fetched
Datatable will be bound to the
protected void BindGridView()
DataTable dt = null;
using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tempdbConn"].ConnectionString))
using (SqlCommand cmd = conn.CreateCommand())
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select row_number() OVER (ORDER BY id) " +
"AS sno,id,uname,totalMarks,selectedItem from tmp_table";
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
dt = new DataTable();
GridView1.DataSource = dt;
RowDataBound event of
Gridview, we will calculate the grand total of marks
column and display them in the
Footer of the respective column.
Follwing code snippet shows the variable named
the cumulative sum as the rows are getting data bound, then assigned the total to
label placed in a Footer.
if (e.Row.RowType == DataControlRowType.DataRow)
lgTots += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "totalMarks"));
if (e.Row.RowType == DataControlRowType.Footer)
Label lgTotal = (Label)e.Row.FindControl("lblGrandTotal");
lgTotal.Text = lgTots.ToString();
Now to look into the other functionality i.e. to Select few or All
will do that by having a
Select All in
Header and selecting/unselecting
it will result in selecting/unselecting all the records respectively.
CheckBox chkA = GridView1.HeaderRow.FindControl("chekSelectALL") as CheckBox;
foreach (GridViewRow gv in GridView1.Rows)
CheckBox chkS = gv.FindControl("chekSelect") as CheckBox;
chkS.Checked = true;
chkS.Checked = false;
Now the additional functionalities which we originally planned to implement are
done. For the sake of completeness, we will also implement the usual functionalities
and events like
RowUpdating for the
Now when we run the application, we can see that all the usual functionalities of
Gridview are working along with the added functionalities that we have implemented.
Points of Interest
In this article we tried to implement some commonly needed functionalities with a
Gridview control in addition to the default functionalities provided by
The code can be improved in many ways but since it is written for the beginner programmers,
I tried to keep the logic simple. I will implement more such functionalities and update this
- 01 November 2012: First version.