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.

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!