473,216 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

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 14999
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gary | last post by:
I have a problem with the datagrid not clearing when the datasource changes, a few fields remain on the screen and even when new data would fill those grid squares the old data remains. I wind...
5
by: Alvin Bruney | last post by:
I have a server side datagrid control. I'd like to clear the grid before the post occurs using javascript. Anybody know how to do this? It would be a similar effect to binding to a null dataset in...
1
by: Asha | last post by:
greetings, how can iclear a datagrid content?
3
by: tshad | last post by:
How do I clear a DataGrid? I have 2 datagrid (DataGrid1 and DataGrid2). They are the same, except for a couple of extra fields that DataGrid has for detail. I only display one DataGrid at a...
3
by: Ed Bick | last post by:
I am binding a datagrid to SQL Adapter. I am wanting to clear it out and populate with a different data set based on user action. I can not seem to find the equivalent to a VB6 Clear method. ...
2
by: dbuchanan | last post by:
Help me understand the behavior of this code. If I run code 1 (below) the datagrid stays populated. === incidental code === Private dataSet1 As CLIP.dsTables private Sub FillDataSet()...
1
by: amber | last post by:
Hello, What is the best method of clearing a datagrid? I'm having issues with it not 'resetting' when I want to repopulate it. I usually design my datagrids using the .net toolbar/properties, but...
2
by: DaveP | last post by:
I've got to be missing something very simple here, but I'm stumped. I have a DataGrid control on a form. The form allows search of a database and display of resulting rows in the DataGrid. My...
1
by: Neelakandan v n | last post by:
Hi, I am unable to clear column headings in datagrid in a program using vb.net 2002, framework 1.0, after clearing a set of data displayed subsequent to a query. The database is in MySql 5.0. Could...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.