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.
Thanks for the response Vince. I've tried what you've suggested but the outcome is still the same. I'm doing exactly as you suggested above and still no luck building the grid dynamically. Here's a copy of the code I'm using to build the grid:
protected void OnButtonClicked(object sender, CommandEventArgs e) { BuildWebGrid(); }
protected virtual void BuildWebGrid() { foreach (Field f in FieldList) { UltraGridColumn col = new UltraGridColumn(true); col.BaseColumnName = f.Name; col.IsBound = true col.Header.Caption = f.Name; UltraWebGrid1.Columns.Add(col); } UltraWebGrid1.DataSource = ResultSet; UltraWebGrid1.DataBind();}
Unfortunately, the problem is beyond what I can diagnose directly. Please submit a support incident to Infragistics Developer Support from this page of our website, and a Developer Support Engineer will assist you further.It would be very helpful if you can provide a sample project that we can run and debug that reproduces the problem. This will ensure that what Developer Support tests is as close to what you're working with as possible.
I thought I would add my .02 cents in here just for the fun of it.
Just a couple questions FirstWhy are you adding the columns dynamcally to your ultraWebGrid and then binding the grid to a datasource?
Are you trying to insert and additional Column or all columns in your DatTable?
If you are just trying to reformat the text in your columns of your datatable then insted bind the table as you would normally and allow fo generated columns.
Then inside of the GridReport_InitializeLayout() event you can change your text on the fly depending on the column you are processing.
This is just example code:
for x = 0 to e.Layout.Bands(0).Columns.Count() - 1{ if e.Layout.Bands(0).Columns(0).Header.Key="whatever your column name in the table is" { e.Layout.Bands(0).Columns(iLoopCount).HeaderText = "Some new columns header" }}
I have a better example on my website located here, it uses a Grid View but it also applies to infragistics.
http://www.aboutmeadows.com/articles/making-a-client-customizable-asp.net-gridview.aspx
Let me know if this helps
Thanks
Well, the reason I'm adding the columns dynamically is because the result set from the DataTable is being generated at runtime. The application I'm working on allows the user to define a query with their chosen set of columns, then the user would execute that query to return a result set to display. It's very similiar to displaying a report. So, because I would never know what columns the user chooses to view, I have to dynamically create each column to display the grid.
Thanks for your .02 cents. I appreciate the help.