I have a grid bound to a WebHierarchichalDataSource. I want to change the data field that one column is bound to according to a users choice.
I have tried setting the data field for the column like thisNewGrid.Columns(NoteCol).Key = "DueNote"or like thisNewGrid.GridView.Columns(NoteCol).Key = "DueNote"
And I either see no change or the error "Object reference not set to an instance of an object". I have tried setting the value in various places during DataBind and in Data_bound but get the same error.I feel I need to set the DataFieldName for the column but DataFieldName is not a member of Columns (although it appears to be in the designer) and I can find no way to set this server side.
I have found the solution. Define a variable as a BoundDataField and use that to set the DataFieldName.
Dim bf as BoundDatafield
bf =NewGrid.Columns(3)
bf.DatafieldName = "[anotherfieldName]"
I suppose this cannot be accessed directly from the Column because the column could be different types (e.g. BoundCheckBoxField) of bound or unbound datafield.
Amazing how cathartic posting a question can be! I spent hours before posting trying to solve this and yet solved it minutes after.
You basically did exactly what I said to do. Like I said, when you access the columns from the Columns collection, we return it as a GridField, because that is the common class for all types (bound, unbound, template, and the checkboxes). So you can only access common properties (header, css, tooltip). You need to cast to the appropriate type before accessing column specific properties. You have accomplished it in a different way, but it works.
-Dave
BUT it doesn't work if I enable sorting!
if I enable sorting then I cannot change the columns, not even the headers! My code says that the header text has changed but the grid shows the original header. Disable sorting and all is fine again.
Hmm, maybe trying clearing the Records on the GridView and rebinding the WHDG to make sure your changes are carried to the grid?
I do that already. What I am now trying is to add the sorting behaviour after I have rebuilt the grid.
I am trying
NewGrid.Behaviors.Add(NewGrid.Behaviors.CreateBehavior(Of Infragistics.Web.UI.GridControls.Sorting)) NewGrid.Behaviors.Sorting.Enabled = True NewGrid.RefreshBehaviors()And this seems to do something, it is just that the grid appears 'pre-sorted' on a particular column and and clicking on the headers results in the ajax activity indicator working but the grid does not sort.The above syntax for creating a behavior is completely new to me.
Hello nickhoare,
Please let us know if you have further questions.