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
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