Jump to content
Software FX Community

Grouping not working when adding Grid programmatically


elder1
 Share

Recommended Posts

 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.

Link to comment
Share on other sites

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 ?

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 month later...

 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; 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...