Accessing GridView Header and Footer
By AzamSharp
Views: 14658

Introduction:

Most of the application gives you functionality to add the new data without leaving the current page. If you are displaying data using GridView control than you can easily add new data using Footer or Header template. In this article we will see how we can extract the values from the TextBoxes that reside in the Header and the Footer of the GridView control.

User interface:

The UI is pretty simple. It consists of the GridView control whose header and footer contains the TextBoxes.

Now we want to implement the code which will extract the values from the Header and the Footer template row.

Filling the GridView Control:

Here is a simple BindData method that populates the GridView control.

C# Code:

private void BindData()

{

SqlConnection myConnection = new SqlConnection(ConnectionString);

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

DataSet ds = new DataSet();

ad.Fill(ds, "Person");

GridView1.DataSource = ds;

GridView1.DataBind();

}

 

VB.NET Code:

Private Sub BindData()
        
Dim myConnection As SqlConnection = New SqlConnection(ConnectionString)
        
Dim ad As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Person", myConnection)
        
Dim ds As DataSet = New DataSet
        ad.Fill(ds, 
"Person")
        GridView1.DataSource 
ds
        GridView1.DataBind
    
End Sub

Getting the value from the Header:

To extract the value from the Header is pretty straight forward. Check out the code below:

C# Code:

protected void Button2_Click(object sender, EventArgs e)

{

// Gets the data from the Header Row

string firstName = String.Empty;

string lastName = String.Empty;

GridViewRow row = GridView1.HeaderRow;

firstName = ((TextBox)row.FindControl("TextBox3")).Text;

lastName = ((TextBox)row.FindControl("TextBox4")).Text;

Label1.Text = "Data Entered in the Header Row:" + lastName + " " +firstName;

}

VB.NET Code:

Protected Sub Button2_Click(ByVal sender As ObjectByVal As EventArgs)
        
' Gets the data from the Header Row 
        
Dim firstName As String = String.Empty
        
Dim lastName As String = String.Empty
        
Dim row As GridViewRow GridView1.HeaderRow
        firstName 
= CType(row.FindControl("TextBox3"),TextBox).Text
        lastName 
= CType(row.FindControl("TextBox4"),TextBox).Text
        Label1.Text 
("Data Entered in the Header Row:"  _
                    + (lastName + (
" " + firstName)))
    
End Sub

As, you can see there is no looping involved and all we did is to get the HeaderRow using a simple property HeaderRow.

Getting the value from the Footer:

The same technique is used to extract the values from the GridView footer.

C# Code:

protected void Button1_Click1(object sender, EventArgs e)

{

string firstName = string.Empty;

string lastName = String.Empty;

// Gets the footer row directly Cool right!

GridViewRow row = GridView1.FooterRow;

firstName = ((TextBox)row.FindControl("TextBox1")).Text;

lastName = ((TextBox)row.FindControl("TextBox2")).Text;

Label1.Text = "Data Entered in the Footer Row:" + lastName + " " + firstName;

}

VB.NET Code:

Protected Sub Button1_Click1(ByVal sender As ObjectByVal As EventArgs)
        
Dim firstName As String = string.Empty
        
Dim lastName As String = String.Empty
        
' Gets the footer row directly Cool right! 
        
Dim row As GridViewRow GridView1.FooterRow
        firstName 
= CType(row.FindControl("TextBox1"),TextBox).Text
        lastName 
= CType(row.FindControl("TextBox2"),TextBox).Text
        Label1.Text 
("Data Entered in the Footer Row:"  _
                    + (lastName + (
" " + firstName)))
    
End Sub

So, as you see extracting values from Header and Footer was pretty straight forward.

Now I have to go and study I have a big test tomorrow.

Happy Coding!

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Thanks
Name: Daniel Ranger
Date: 2/12/2007 3:57:50 AM
Comment:
Hi Azam,

Just a quick *thanks* for all your valuable help and time, much appreciated.

Daniel
Subject: Efficient Paging
Name: Roni
Date: 3/23/2007 2:00:51 AM
Comment:
Hi Azam,

First of all, your examples are very interesting and usefull.

Question: Please, can you mention the best way to do paging [Without ObjectDataSource Control], based on a DataSet [Without the need to retrieve all data for just displaying 10 rows]?
Subject: thanx
Name: afzal
Date: 5/15/2007 12:35:39 AM
Comment:
thanx my friend.
if u are on oekut plz join me as afzal ansari.
Subject: RE: Thanks
Name: AzamSharp
Date: 5/15/2007 10:21:38 AM
Comment:
Hi Afzal,

I am glad that you liked the article.

Subject: To Add the Cells in a Gridview
Name: Krishna
Date: 5/24/2007 4:54:01 AM
Comment:
How to add the Cells is there any method
Subject: thanxs
Name: suresh
Date: 6/14/2007 6:08:03 AM
Comment:
Its really nice
Subject: encabezado fijo en grid view
Name: maria
Date: 7/13/2007 12:50:52 PM
Comment:
I need to know ¿how do you to fixed header in a grid view?
Subject: Thanks
Name: swathi
Date: 8/1/2007 11:41:26 PM
Comment:
Very gud article ,it helped me....thank u verymuch.
Subject: article
Name: Glenn
Date: 1/6/2008 7:35:32 PM
Comment:
Very good. Please continue...
Subject: Button on header row
Name: Yücel
Date: 1/19/2008 6:08:18 AM
Comment:
can i insert a button on headerrow( like insert button near a headertext)?
Subject: RE: Button on header row
Name: AzamSharp
Date: 1/19/2008 9:44:02 AM
Comment:
Hi Yucel, You can use the HeaderTemplate to include ASP.NET control inside the GridView control.
Subject: Button on header row
Name: Yücel
Date: 1/21/2008 1:13:34 AM
Comment:
Thank you azamsharp. but a problem here. buttons in header no prob but if textbox1 in header then code sent an error called BC30451 when textbox1 move out on header the code works perfectly.
Subject: sleep function
Name: asp_novice
Date: 1/25/2008 9:56:08 AM
Comment:
your examples are excellent. Was wondering if you knew a way of displaying data in the gridview one after the other as supposed to all at once using a sleep function. ie all records have time attribute. record 1 = 10:01:00, record 2 = 10:01:10. so after displaying record 1, i want the system to sleep for 10 secods before displaying record 2. Any ideas of how i can achieve this. many thanks in advance. Any help will be greatly appreciate
Subject: Re:Thanking
Name: M.Senthil Nathan
Date: 2/20/2008 10:43:38 PM
Comment:
:-)
Subject: Grid view
Name: srinivas
Date: 2/23/2008 11:19:59 PM
Comment:
Thanks for the onformation.

I need to bind the GV from the text boxes.Can u give some infornation on it?




Join WebHost4Life.com






Copyright GridViewGuy 2007-2008