Dynamically Change DataField property on asp:BoundField

26. February 2009 20:32

So I needed a way to change the DataField property of the first column in a gridview.  The situation was that instead of this text when I click this button I want it to show this text.  So I added a link button outside the gridview and this was the code inside the onclick event

 Dim obj As System.Web.UI.WebControls.BoundField = gridView1.Columns(0)

 If obj.DataField = "COLUMN_1" Then
  obj.DataField = "COLUMN_2"
 Else
        obj.DataField = "COLUMN_2"
 End If

 gridView1.DataBind()

This turned out working perfect for dynamically changing the datafield property in a gridview.  I'm sure this could have been done with some sort of javascript or AJAX, but I needed a quick solution and this was it. 

Tags: ,

ASP.NET

blog comments powered by Disqus



My Random Thought

I think the OCW is a great thing to have available to those who are in school, just finished school or just want to educate themself

http://ocwconsortium.org/

John On Twitter

Discounts