Hi,
I'm having a problem binding a DataTable to a WebGrid. This is the setup:
There's a button on the page that causes a postback. And during the postback in the event method, I dynamically define each UltraWebColumn and then add each column to the WebGrid. Then after each column is defined, I assign the datasource to the DataTable and then execute the WebGrid.DataBind() method to bind.
So, this is the issue. When I click the button for the first time, no data is rendered to the page. But, the WebGrid will render data when the button is clicked a second time and any other time after. But when I clear the webgrid at runtime then click the button to render the grid, no data is rendered again until I click the the button a second time.
Does anyone have an idea why this might be happening? Thanks.
What's likely happening is that your dynamically-added columns are not being stored in ViewState, and thus aren't being recreated on subsequent postbacks (after they've been added by clicking the button). This is because you're likely using one of the following two approaches to create the UltraGridColumn objects (using C# code):// first approach - use the default constructor, then add to the grid's Columns collectionUltraGridColumn col1 = new UltraGridColumn();this.ultraWebGrid1.Columns.Add(col1);// second approach - get a reference to the column as it's being addedUltraGridColumn col2 = this.ultraWebGrid1.Columns.Add();Instead, use the constructor of the UltraGridColumn class that takes in a boolean value, and pass "true" for that value. This will ensure that, so long as the grid is stored in ViewState, so will the column.// suggested approach - create the column to be stored in ViewState, then add to grid's Columns collectionUltraGridColumn col3 = new UltraGridColumn(true);this.ultraWebGrid1.Columns.Add(col3);You'll have to be careful that you check whether or not the particular column's Key exists before you add it; otherwise, you'll start getting exceptions when you click that button in the future.As a simpler approach, assuming that these columns are supposed to correspond to columns in your DataTable, let the grid auto-generate the columns for you, and update the properties of each column as desired when you handle the grid's InitializeLayout event. This won't help for adding any dynamic, unbound columns, or if you need to avoid auto-generation of columns; in either case, you'll still need to use the above-mentioned approach.Please remember that these forums area a peer-to-peer resource to share issues and ideas with other Infragistics users. If you require official support from Infragistics for further assistance, please submit a support incident to Infragistics Developer Support from this page of our website.