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.
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 |
<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> |
using System; using System.Data; using System.Data.SqlClient; |
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"; } } |
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 | |
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 Email
|
|||
|
|


Subscribe by RSS
Subscribe by Email
7 comments :
hkh
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 done successfully this is really useful post .....
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();
}
where is the download link?..
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
can u explain abt the datasource. my doubt is ur db name is usrinformation and ur dsorce is sureshdasari so wat is data source.