468,133 Members | 1,475 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

Clearing a DataGrid

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
Nov 16 '05 #1
3 14517
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brad" <br**@saveitnow.com> wrote in message
news:a7**************************@posting.google.c om...
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

Nov 16 '05 #2

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brad Louwagie" <br**@saveitnow.com> wrote in message
news:uf**************@TK2MSFTNGP12.phx.gbl...

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Gary | last post: by
5 posts views Thread by Alvin Bruney | last post: by
1 post views Thread by Asha | last post: by
3 posts views Thread by tshad | last post: by
3 posts views Thread by Ed Bick | last post: by
2 posts views Thread by dbuchanan | last post: by
1 post views Thread by amber | last post: by
2 posts views Thread by DaveP | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.