Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Introduction:

In this article I will explain how to change gridview header dynamically at runtime using asp.net


Description:
  
In previous posts I explained many articles relating to Asp.net and Gridview. Now I will explain another article relating to Gridview. During working in one of my application I got one requirement like change gridview header dynamically whenever we click on some button.
To implement this first design table in your database and give name as UserInformantion

Column Name
Data Type
Allow Nulls
UserId
int(set identity property=true)
No
UserName
varchar(50)
Yes
LastName
varchar(50)
Yes
Location
varchar(50)
Yes
After completion of table enter some dummy data to bind that one to gridview after that create new website and design your aspx page like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Change Gridview Header Dynamically at runtime in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnChange" Text="Change Header Text" runat="server" onclick="btnChange_Click" />
<asp:GridView ID="gvUserInfo" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
After completion of aspx page design add the following namespaces in code behind

C# Code


using System;
using System.Data;
using System.Data.SqlClient;
After that write the following code in code behind


protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind gridview from database
protected void BindGridview()
{
SqlConnection con=new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
con.Open();
SqlCommand cmd=new SqlCommand("select TOP 10 UserName,LastName,Location from UserInformation",con);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}
// This button click event is used to change gridview header based on requirement
protected void btnChange_Click(object sender, EventArgs e)
{
if(gvUserInfo.Rows.Count>0)
{
gvUserInfo.HeaderRow.Cells[0].Text = "Name";
gvUserInfo.HeaderRow.Cells[2].Text = "Destination";
}
}
VB.NET Code


Imports System.Data
Imports System.Data.SqlClient
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridview()
End If
End Sub
' This method is used to bind gridview from database
Protected Sub BindGridview()
Dim con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("select TOP 10 UserName,LastName,Location from UserInformation", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
gvUserInfo.DataSource = ds
gvUserInfo.DataBind()
End Sub
' This button click event is used to change gridview header based on requirement
Protected Sub btnChange_Click(ByVal sender As Object, ByVal e As EventArgs)
If gvUserInfo.Rows.Count > 0 Then
gvUserInfo.HeaderRow.Cells(0).Text = "Name"
gvUserInfo.HeaderRow.Cells(2).Text = "Destination"
End If
End Sub
End Class


Demo

Before                                                                               After Button Click      
        
                                                       

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

9 comments :

Anonymous said...

hkh

Anonymous said...

Hi,
I created a custom panel, it contains gridview.
In design time grid is not displaying but run time its working fine.
Any idea?

i-phone training said...

i done successfully this is really useful post .....

Anonymous said...

I tried the same but am not getting. Please help me out.

if(rblsrcht.SelectedItem.Text=="Likely")
{

SqlConnection con = new SqlConnection("Data Source=SPSERVER;Initial Catalog=SuperPOS;Integrated Security=True");
string sqlstr2 = "select POS004_ITEM_ID,POS004_DESC,POS004_SALE_PRICE2,POS004_AVAIL_QTY,POS004_UNIT_QTY from POS_ITEMMST_004 where " + ddsbyprod.SelectedValue + " like '%" + tbsvprod.Text + "%'";
SqlDataAdapter da = new SqlDataAdapter(sqlstr2, con);
DataSet ds = new DataSet();
con.Open();
da.Fill(ds);
gv1.DataSource = ds;
gv1.DataBind();

gv1.HeaderRow.Cells[1].Text = "NAME";

con.Close();
}

Anonymous said...

where is the download link?..

Srikanth Surakala said...

can u tell me about datasource. i mean u hv name it ur db as user information, and u gave dsource as sureshdasari. i want the explnation abt that plzz

Srikanth Surakala said...

can u explain abt the datasource. my doubt is ur db name is usrinformation and ur dsorce is sureshdasari so wat is data source.

Anonymous said...

Thank u very much....your all articles are ery very helpful...

Mohit Sharma said...

Thanks, it work for me.

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.