Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Problem dynamically changing column Header Text on a Gridview
 

Problem dynamically changing column Header Text on a Gridview

Hi,

I need to change the column header text on a grid view at run-time. I do this using the following code which adds a "...." to each string:

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

If e.Row.RowType = DataControlRowType.Header Then

Dim c As TableCell

For Each c In e.Row.Cells

c.Text &= "...."

Next

End If

End Sub

This works provided that AllowSorting is set False on the GridView.

If AllowSorting is set to True, then c.Text is empty on call to the the RowCreated event and adding any text into c.Text prevents any text (even the default) being generated for that column header text.

I would appreciate advice on how to change the column Header Text in a way that will work for both sorted and unsorted GridViews.

Thanks

Paul

Paul-I  Wednesday, March 12, 2008 4:46 PM

Hello Paul,

This is because a databindlinkbutton control will be inserted into cell when GridView supports Sorting.

Please try this way:

Sub GridView1_RowCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

If e.Row.RowType = DataControlRowType.Header Then Dim c As TableCell

Dim d As GridView = sender For Each c In e.Row.Cells If d.AllowSorting Then

Dim lb As System.Web.UI.WebControls.LinkButton

lb = c.Controls(0)

lb.Text &= "...."

Else

c.Text &= "...."

End If

Next End If

Sub

Hope this helps,

Best regards,

Wen Yuan

Wen Yuan Wang  Monday, March 17, 2008 4:18 AM

Hello Bob,

It seems vb compiler disallowed implicit conversion on your side. But, I believe you still can convert it by yourself. Please try this way and feel free to let me know if you still face any further issue. We are glad to assist you.

CType(c.Controls(0), System.Web.UI.WebControls.LinkButton)

Hope this helps.

Best regards,

Wen Yuan

Wen Yuan Wang  Tuesday, March 18, 2008 12:28 PM

Hello Paul,

This is because a databindlinkbutton control will be inserted into cell when GridView supports Sorting.

Please try this way:

Sub GridView1_RowCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

If e.Row.RowType = DataControlRowType.Header Then Dim c As TableCell

Dim d As GridView = sender For Each c In e.Row.Cells If d.AllowSorting Then

Dim lb As System.Web.UI.WebControls.LinkButton

lb = c.Controls(0)

lb.Text &= "...."

Else

c.Text &= "...."

End If

Next End If

Sub

Hope this helps,

Best regards,

Wen Yuan

Wen Yuan Wang  Monday, March 17, 2008 4:18 AM

Hi,

Thanks for the response. Unfortunately the code as published does not compile. You get the error:

"On strict disallows the implicit conversion of System.Web.UI.Control to System.Web.UI.WebControls.LinkButton" on the line lb = c.Controls(0)

Type System.Web.UI.Control does not have a Text property.

Best regards

Paul

Paul-I  Monday, March 17, 2008 9:42 AM

Hello Bob,

It seems vb compiler disallowed implicit conversion on your side. But, I believe you still can convert it by yourself. Please try this way and feel free to let me know if you still face any further issue. We are glad to assist you.

CType(c.Controls(0), System.Web.UI.WebControls.LinkButton)

Hope this helps.

Best regards,

Wen Yuan

Wen Yuan Wang  Tuesday, March 18, 2008 12:28 PM

Thanks this has been very helpful.

Paul-I  Wednesday, March 19, 2008 9:05 AM

Hi

I have a similar problem .I have to change the Gridview Header text dynamically on selected Index changed Event of a dropdown.

How do I access the GridView cell etc in SelectionIndex Changed Event of GridView.

Please can you help in this.

Swatim  Monday, December 29, 2008 7:34 AM

You can use google to search for other answers

Custom Search

More Threads

• changing datagrid, dataset, & code to reflect table field changes in db
• DataGridViewComboBoxColumn value display
• DataGridView MultiSelect drag problem
• inserting values to database
• Updating on form with many to many relationship
• TabPages and BindingContexts
• bindinglist for binding the datagrid view in winforms
• Binding to tables with a many-many relation
• Loop through datagrid to get values of boolean columns
• Programmatically add new row to DataGridView.