473,394 Members | 1,709 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,394 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 15024
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.