elder1 Posted July 10, 2008 Report Share Posted July 10, 2008 Hi, Can you help? In testing I dragged a Grid onto a WebForm, configured a SqlDataSource. I did some formatting and got grouping to work ok. BUT now I need to to do the same thing programmatically. I have added the grid and can display the data but cannot get Grouping to work. If I try to set the Grouping RootLevelField in Grid_DataBound eventhandler I get a NullReference Exception. Here is the code: GridFX.WebForms.Grid Grid2 = null; DataTable dtab = new DataTable(); protected void Page_Init(object sender, EventArgs e) { Grid2 = new Grid(); Grid2.ID = "Grid2"; Page.Controls.Add(Grid2); Grid2.DataSourceInCode = true; Grid2.DataSourceSelecting += new EventHandler<DataSourceSelectingEventArgs>(Grid2_DataSourceSelecting); Grid2.DataBound += new EventHandler(Grid2_DataBound); using (SqlConnection dbConn = new SqlConnection(sConnStr)) { dbConn.Open(); SqlCommand dbCmd = null; string sSQL = "sp_GetSalesBreakdown"; using (dbCmd = new SqlCommand(sSQL, dbConn)) { dbCmd.CommandType = System.Data.CommandType.StoredProcedure; SqlDataAdapter adapter = new SqlDataAdapter(dbCmd); adapter.Fill(dtab); } } } void Grid2_DataSourceSelecting(object sender, DataSourceSelectingEventArgs e) { e.DataSource = dtab.DefaultView; } void Grid2_DataBound(object sender, EventArgs e) { // A DataField object is cast as the grid's date field DataField rootLevelField = (DataField)Grid2.DataFields[0]; Grid2.Grouping.Enabled = true; Grid2.Grouping.RootLevel.Field = rootLevelField; Grid2.Grouping.RootLevel.Expanded = true; Grid2.Grouping.RootLevel.Style.HorizontalAlign = HorizontalAlign.Left; } Any help appreciated. Thanks, Chris. Quote Link to comment Share on other sites More sharing options...
StephenP Posted July 10, 2008 Report Share Posted July 10, 2008 Chris, could you try the DataBinding event instead? That should do the trick. Quote Link to comment Share on other sites More sharing options...
elder1 Posted July 10, 2008 Author Report Share Posted July 10, 2008 Thanks for the quick response Stephen. Unfortunately this doesn't work. I can debug this and during Grid2_DataBinding event handler Grid2.DataFields.Count is zero So this code: DataField rootLevelField = (DataField)Grid2.DataFields[0]; throws an ArgumentOutOfRangeException I assume we need to set the column for Grouping after the grid is aware of the data. Which is why I tried it in DataBound handler Any other ideas ? Quote Link to comment Share on other sites More sharing options...
elder1 Posted July 11, 2008 Author Report Share Posted July 11, 2008 Any other suggestions? Columns are not available during the DataBinding event handler Quote Link to comment Share on other sites More sharing options...
StephenP Posted July 11, 2008 Report Share Posted July 11, 2008 Nuts. Right now grouping is done during data binding, between the DataBinding/ed events. And so is the part where it generates the DataFields and Columns from the data schema. I have submitted a task to provide an event that takes place after the DataFields/Columns generation. This will be the event you can handle and adjust grouping to your heart's content. This will be included in the next service pack, however if you can't wait I can issue a hotfix sometime next week. It could introduce new instabilities, though. Would that work for you or would you rather wait a month for the service pack? Quote Link to comment Share on other sites More sharing options...
elder1 Posted July 11, 2008 Author Report Share Posted July 11, 2008 Ah! A new event after Columns are generated and before data is bound sounds good. We plan to demonstrate this financials grid Thursday next week so I'll try the hot fix if possible. The GridFX version we are using is: 1.0.3007.30962 Is there another way to do this? maybe pre-define the columns with templates ? Cheers, Chris. Quote Link to comment Share on other sites More sharing options...
StephenP Posted July 11, 2008 Report Share Posted July 11, 2008 If the data schema doesn't change throughout the page lifecycle, you should add the Columns and DataFields in markup. Then you will have DataFields filled in DataBinding. Can you do that? Quote Link to comment Share on other sites More sharing options...
elder1 Posted July 14, 2008 Author Report Share Posted July 14, 2008 How do I add Columns in markup, I am writing this in a class library. No Webform (aspx). Quote Link to comment Share on other sites More sharing options...
StephenP Posted July 14, 2008 Report Share Posted July 14, 2008 Just fill the DataFields collection, then add a FieldColumn to the Columns for each of those data fields. Do it right after you create the new grid, before you add it to the control tree. Quote Link to comment Share on other sites More sharing options...
elder1 Posted August 15, 2008 Author Report Share Posted August 15, 2008 Update: Here is the code (this worked ok). Thanks. NumberField mthfield = new NumberField();mthfield.DataPath = "ForecastMonth";Grid2.DataFields.Add(mthfield);FieldColumn col2 = new FieldColumn(mthfield);col2.Style.Header.Bold = true;Grid2.Columns.Add(col2);NumberField numfield = new NumberField();numfield.DataPath = "ForecastWeek";Grid2.DataFields.Add(numfield);FieldColumn col3 = new FieldColumn(numfield);col3.Style.Header.Bold = true;col3.Style.Header.HorizontalAlign = HorizontalAlign.Center;col3.Style.HorizontalAlign = HorizontalAlign.Center;Grid2.Columns.Add(col3);Grid2.Sorting.SortedFields.Add(numfield);numfield = new NumberField();numfield.DataPath = "ForecastUnits";Grid2.DataFields.Add(numfield);FieldColumn col4 = new FieldColumn(numfield);col4.Style.Header.Bold = true;col4.Style.Header.HorizontalAlign = HorizontalAlign.Center;col4.Style.HorizontalAlign = HorizontalAlign.Center;Grid2.Columns.Add(col4);Grid2.DataFields["ForecastMonth"].Title = "Month";Grid2.DataFields["ForecastWeek"].Title = "Week";Grid2.DataFields["ForecastUnits"].Title = "Forecast Units";Grid2.Grouping.Enabled = true;Grid2.Grouping.RootLevel.Field = mthfield;Grid2.Grouping.RootLevel.Expanded = true; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.