Clearing a DataGrid

  • Thread starter Thread starter Brad
  • Start date Start date
B

Brad

I'm working with a DataGrid in C#, and the user needs to be able add
and remove columns from the DataGrid on the fly, without opening and
closing the form. The scenario is this. I have a setup table that
allows users to rename columns, resize columns, and select whether or
not they want them to be visible or not. While the user is working
with a certain set of data, they will want to see certain columns, and
in certain situations they will want to add or remove columns to the
datagrid without necessarily refreshing the data. I am using the
following code to set the column defaults, where ds is my DataSet.

foreach (DataColumn dcol in ds.Tables["AnalysisLINE"].Columns)
{
strColumn = dcol.ColumnName.ToString();
strName = dc.DisplayName(strColumn, ref boShow);
ds.Tables["AnalysisLINE"].Columns[strColumn].ColumnName = strName;

if (boShow == false && ds.Tables["AnalysisLINE"].Rows.Count > 0)
{
ds.Tables["AnalysisLINE"].Columns[strName].ColumnMapping =
MappingType.Hidden;
}
}

dc.DisplayName returns the modified Column Name, and a bool boShow.
The name is set, and if boShow is false, the column will be hidden.
This code works great the first time it is executed, however, if the
form is not closed, and a column that was marked boShow = true
switches to false, as the user no longer wants to see that column,
when I refresh the DataGrid, the column is still there. The same
holds true in the opposite scenario when I want to show a column that
was not originally there.

I have tried clearing the dataset,
ds.Clear();
ds.AcceptChanges();
DataGrid.DataBindings.Clear();
DataGrid.Refresh();

But when I attempt to reload thd DataGrid, the data will be accurate,
but the selected Columns will not change. Is there a way to unload
and reload the DataGrid?

What am I missing?

Your help will be most appreciated, as I have been working on this for
several hours.

Thanks in advance,

Brad
 
Brad,

You might have to set the data source to null, and then back to the
data, or create a data view (a new one based on the table) and set the data
source to that. I believe that the DataSource property of the DataGrid
checks against what it is already bound to, and if it is the same data
source, it doesn't do anything. This is probably your best bet.

Hope this helps.
 
Nicholas,

Setting the Data Source to null and changing the data source to a
different name actually does work, and displays correctly. So that is
great, and I much appreciate that insight. However, my users could be
adding and removing columns many times before they close their windows
form, forcing me to almost have a dynamic data source. Although it will
work, it doesn't seem all that clean. Is there no way to totally reset
the datagrid? I've tried to Dispose it and recreate it, but then I get
an Index out of range error.

Thanks again,

Brad
 
Brad,

Why not have the data set contain all of the data, and set the
visibility of the columns when you want to add or remove them? You don't
have to change the data, just the visibility. In that case, check the
DataGridTableStyle instance corresponding to the table in the grid. From
there, check the GridColumnStyles property to get the DataGridColumnStyle
instance for the column. You can remove it from the collection in order to
make it not visible.

Hope this helps.
 
Back
Top