How to change the Header Text of a datagrid column dynamically?

Hi ,

 Can any one guide me on how to change the header text of a datagrid column dunamically in code behind?

i am using the follwing code in datagrid item databound, but its not working


if (e.Item.ItemType == ListItemType.Header)



if (Session["RoleID"].ToString() == "6" | Session["RoleID"].ToString() == "13")


e.Item.Cells[5].Text = "Select Record";


10/25/2007 4:10:55 PM 72751 articles. 3 followers. Follow

2 Replies

Similar Articles

[PageSpeed] 19

My personal way of doing it is to define a custom header. Then I can grab an instance of a lable or literal control in this instance in the ItemdataBound event handler, check for the Header row. Then I can set it at run-time as the row is being built by the server.

Chris Love
ASP.NET 2.0 Your Visual Blueprint for developing Web Applications
10/25/2007 4:31:45 PM

This worked for me:


<asp:datagrid id="DataGrid1" runat="server" autogeneratecolumns="false" datasourceid="SqlDataSource1" onitemdatabound="DataGrid1_ItemDataBound">
		<asp:boundcolumn datafield="ProductID" />
		<asp:boundcolumn datafield="ProductName" />
<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
	selectcommand="SELECT [ProductID], [ProductName] FROM [Alphabetical list of products]">


protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
	if (e.Item.ItemType == ListItemType.Header)
		e.Item.Cells[0].Text = "ID";
		e.Item.Cells[1].Text = "Product Name";

Thanks, Ed

Microsoft MVP - ASP/ASP.NET

10/25/2007 4:39:11 PM

Similar Artilces:

Weekdays changing dynamically in Datagrid Column Header Texts
Dear All,I hav a prob.I hav a table t_custinfo containing customer ID,name and personal details.Now I hav a datagrid where in I hav to retrieve custID and custname in two columns.In other columns I need to display weekdays say Mon to Sun with dates..The weekdays hav to change dynamically.In the grid user enters the amount collected from the customers for ex:CustID Custname Mon-2/2/06 Tue-3/2/06 Wed-4/2/06....till Sun-..1      bharath   100          2000      1000   2  ...

Datagrid column header text change
How to change the datagrid (MyDatagrid) column hedare text via load event without using itemdatabound event. Datagrid name: MyDatagrid column 2 text = OrderNo Thank you very much for the helpful info.     You can't! The Page.Load event is too early in the Page life cyce. You must wait until the ItemDataBound event or after.Thanks, EdMicrosoft MVP - ASP/ASP.NET Can you please tell me, how to change the header text of datagrid in the itemdatabound.   Thanks.   void DataGrid1_itemDataBound(object sender, DataGridItemEventArgs e)    { &nb...

Dynamically change the dw column header text
Hi I have a question, anyone know how to change the text displayed on dw columns header dynamically. Thx. ernest wrote: > > Hi > I have a question, anyone know how to change the text displayed on dw > columns header dynamically. > > Thx. Try: dw_yourdwname.Object.yourtextname.text = 'yournewtext' example:dw_2.Object.dept_id_t.text = 'newtext' You can also use "Modify" function. In article <>, says... > I have a question, anyone know how to change the text displayed on ...

Changing column header text of a gridview inside user control
Hi,I have an ascx file with a gridview, and I need to change the header text of the gridview's column in the code-behind. Something like that:foreach (DataControlField col in GridView1.Columns){ col.HeaderText = GetString(col.HeaderText);}However, no matter where I place that code, the header texts return to their original value, and that's what's being displayed.What could be the reason for that? And how can it be solved?  ...Anyone? I'm having the feeling that I'm missing something quite basic here, any help would be very appreciated. Amit00: col.HeaderT...

Dynamic change the 'Read Only' in datagrid(template column/Bound column)
I create few template column and bound column in datagrid. One of the template column contain the StudentMark. I want to know how to change that column to (Read only) or "Not" (Read only) when click the edit button. It is based on a dropdownlist to determine to read only or not. I think i must use the template column, since i need to do the validation to the StudentMark. (For example, If select value '1' in dropdownlist, the "StudentMark" is become 'Read only' and select '2' become vice versa. I was success to bind the datagrid and "StudentMark" become a textbox...

dynamically changing header text
How can I change header text for datagrid control dynamically? <asp:TemplateColumn> <HeaderTemplate> <%# now.month()-1%>/<%# now.year()%> <br> Qty On Hand </HeaderTemplate> <ItemTemplate> <%# databinder.eval(container.dataitem,"prevonhand")%> </ItemTemplate> </asp:TemplateColumn> How do I change Header text (current month/year) to selected month and year from ddl control index change event) Handle ItemCreated in the grid and watch for the HeaderItem. You can set the values there. You'll n...

Change datagrid header text
I have a datagrid and I need to programatically change each column's header text based on user's previous input. I put the code in OnItemDataBound event like this:  if (e.Item.ItemType == ListItemType.Header) {  for (int i = 0; i <= e.Item.Cells.Count - 1; i++)  { e.Item.Cells[i].Text = "something"; } } The header text did get changed, but it changed it into static text which is not clickable so sorting cannot be performed. How do I change the header text but still make sorting enabled?It's been bothering me for a while now ... thanks a lot! Penn i am surprised ...

Changing the datagrid header text
Is this possible at all? I have an application that runs in different languages and I want to display the header in the current language (stored in the session). Thanks in advance Heki Yes, it is defenetely possible. You will have to use resource files to achieve that. Idea is - there will be one resource file to store the default string values and one resource file for each language you want to support. So, say i add two resource files to my project: MyForm.resx and first being the default resource and second containing versions of the strings in Russian. ...

Changing Header Text on a Datagrid
I have headers that post back to sort - I am attempting to change the text with each sort so as to indicate that is the current sort column and the sort direction howevere when I  do something like this it wipes out the link - can anyone please advise how to  thanks in advanve If e.Item.ItemType = ListItemType.Header Then e.Item.Cells(i).Text = "whatever" End If I've never had to do anything like that before, but i think you have to find the link in the header and change the text value. Do a google search, i'm sure something will pop up.  DarmarkMark as Answer, if ...

Dynamically changing a column in datagrid
How do I programmatically change / hide a column of a datagrid ? What kind column it is?This posting is provided "AS IS" with no warranties, and confers no rights....

Should I use DataGrid(add dynamic control) or just dynamic control?
Hi, I am debating between using just dynamic control's or actually adding the control's dynamically into a datagrid.  I have tried with just adding dynamically, but I am having so much trouble because the selectedindexchanged even it firing for no reason.  I have asked a lot of people, but nobody knows the answer for that and I searched in the web and I am not getting anything either. Now someone suggested to use DataGrid and add the control's dynamically.  In each row, I have a dynamic column, which could be 2 and above.  It will depends on a number that I get fromt he ...

Dynamically Loaded Control Wants to Change The Dynamically Loaded Control
I have been playing around with dynamically loading controls and AJAX and have the following:An UpdatePanel has a PlaceHolder which which contains a dynamically loaded control.  The control that is dynamically loaded has a button that is supposed to change the control that was loaded.  So after the link button is clicked on DynamicControlA, DynamicControlB should now be loaded where DynamicControlA used to be. Is there anything in the framework to do this?  If not, any ideas on how to tackle? I did it with Panels.  Made 5 panels all the same size, and toggled visi...

change column header text (SaveAs)
I'm saving the content of my datawindow as Excels (SaveAs) but the name of the header column are not the name as on screen. Meaning the header on my column is Name and then my rows shows the names, but SaveAs gives first_name as my header. Is there a way we can change the header when saving as Excels. thanks in advance. Mike Hi Mike; The SaveAs grabs the column names from the DW buffer not the labels. So what you will see are the raw column names or some hybrid of the table and column name together. That is the DW Painters default at construction time. So .....

How to change header text in gridview dynamically ?
I am trying to change the background of a header cell based on the value of  the headet text. Checking the current value is where the problem lies... when I am doing like as below, the e.Row.Cells(x).Text value always returns blank. I have an autogenerated gridview columns. Sub GridView1_OnDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)     If e.Row.RowType = DataControlRowType.Header Then       Dim x As Integer       For x = 0 To e.Row.Cells.Count - 1     &nbs...

Web resources about - How to change the Header Text of a datagrid column dynamically? -

Placebase team at Apple file “Schematic Maps” patent dynamically detailing important data
In a report from Computerworld way back in 2009, we learned that Apple had quietly acquired Maps API company Placebase. Then, founder of Placebase ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

In a dynamically typed language, is it a bad idea to return different data types?
In some cases, returning different types from a single function works.

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ... launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...

A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97% Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 11/29/2015 2:12:39 AM