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.