Hi
I have a simple winform application with a GridDataBoundGrid control displaying data from Sql server from a stored procedure. This grid control needs to source data from multiple stored procedures. The result sets coming from these stored procs are very different sets of columns. Hence i need to re-initialize the databoundgrid everytime before populating the results using the standard datasouce/datatable binding. In fact, without resetting, it was giving me very unpredictable results when i was running different stored procedures. I searched your forums and found a mechanism to reset the grid. Here it is:
this.ResultsGrid.BeginUpdate();
this.ResultsGrid.Binder.SuspendBinding();
this.ResultsGrid.DataSource = null;
this.ResultsGrid.DataMember = "";
this.ResultsGrid.Binder.InternalColumns.Clear();
this.ResultsGrid.Binder.ResetHierarchyLevels();
this.ResultsGrid.DataSource = new DataTable();
this.ResultsGrid.Binder.InitializeColumns();
this.ResultsGrid.EndUpdate();
this.ResultsGrid.Binder.ResumeBinding();
This is working however there is something not right with this. I have manually added a column to the grid that has a button to every row and associated a button click event. (code is below). When i press this button after going through a few refresh cycles on the grid, the button behaves strangely. it generates multiple click events on a single click. For example, if i have gone through two resets, it generates two clicks. 3 click events for 3 resets and so on!
So i think the reset/re-initialize of the DataboundGrid is not happening very cleanly. Some events or internal datastructures are still lingering around. Please help me fugure out the cleanest and fastest way to fully programmatically re-initialize the grid 100% (as if it was loaded for the very first time) with nothing internal lingering around from the prior data load.
BTW, i tried using dispose on the grid object and creating a new grid object but that did not work.
Here is the code i have to add a button column to the grid:
GridBoundColumnsCollection columnCollection = (GridBoundColumnsCollection)this.ResultsGrid.Binder.InternalColumns.Clone();
this.ResultsGrid.PushButtonClick += new GridCellPushButtonClickEventHandler(ResultsGrid_CellButtonClick);
//Add OptionQuotes Button
GridBoundColumn NewColumn = new GridBoundColumn();
NewColumn.HeaderText = "View";
NewColumn.MappingName = "View";
NewColumn.StyleInfo.CellType = "PushButton";
NewColumn.StyleInfo.Description = "ViewDetail";
columnCollection.Add(NewColumn);
this.ResultsGrid.Binder.GridBoundColumns = columnCollection;
GridButtonsAdded = true;
private void ResultsGrid_CellButtonClick(object sender, GridCellPushButtonClickEventArgs e)
{
int rownum = e.RowIndex, colnum = e.ColIndex;
---- My logic ---
}
Here is the code fragment for Data Binding from Stored Procedure:
DataTable dt = new DataTable();
adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
adapter.Dispose();
cmd.Dispose();
ResultsGrid.DataSource = dt;
this.ResultsGrid.BeginUpdate();
Thanks!
Vishal