Home All Groups Group Topic Archive Search About

Equivalent of DataGridTableStyle for DataGridView?

Author
28 Jul 2006 6:01 PM
Mitchell S. Honnert
Is there an equivalent of the DataGrid's DataGridTableStyle for the
DataGridView?  If not, is there an easy way to duplicate the
DataGridTableStyle's functionality for the DataGridView?

Here's the background for my question...

Before I switched my application over to the Fx 2.0, I used a DataGrid to
display my data.  I would store different DataGridTableStyles (each one with
a custom set of columns) in the DataGrid.TableStyles property and would use
the DataGrid's ability to switch between these sets of columns depending on
the source data.  You could say that the DataGrid's DataGridTableStyle
allowed you to have a set of "context sensitive" columns.  If the data
changed, so too did the columns used to view the data.

But when I switched over to the Fx 2.0's DataGridView, I couldn't find an
analog to the DataGridTableStyle.  To get the same functionality, whenever I
switch the data source of my DataGridView, I have to clear out the
DataGridView.Columns and add the appropriate columns back.  This method
works, but is far less elegant than the way that the old DataGrid works.  My
initial idea was to store my sets of custom columns in different
DataGridViewColumnCollection instances and set the DataGridView.Columns
property as appropriate, but the DataGridView.Columns is read-only, so I
couldn't do that.

So, is there an easy way to assign a DataGridView columns in one step or am
I stuck with my current method of clearing and reloading the columns every
time?

Thanks,

   - Mitchell S. Honnert

PS: In the MSDN page "Differences Between the Windows Forms DataGridView and
DataGrid Controls" (http://msdn2.microsoft.com/en-us/library/ms171628.aspx),
it says this...

"The only feature that is available in the DataGrid control that is not
available in the DataGridView control is the hierarchical display of
information from two related tables in a single control."

I'm hoping that this statement is true and that I'm just overlooking the
functionality in the DataGridView that equates to the DataGrid's
DataGridTableStyle.

Author
29 Jul 2006 11:10 AM
Ken Tucker [MVP]
Hi,

        Setting the Datagridview's AutoGenerateColumns property to false
will prevent the framework from adding the columns for you.  There is not
any tablestyle property for the datagridview.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.autogeneratecolumns.aspx

Ken
-------------------
Show quoteHide quote
"Mitchell S. Honnert" <n**@hRoEnMnOeVrEt.com> wrote in message
news:uhy7e$msGHA.1888@TK2MSFTNGP03.phx.gbl...
> Is there an equivalent of the DataGrid's DataGridTableStyle for the
> DataGridView?  If not, is there an easy way to duplicate the
> DataGridTableStyle's functionality for the DataGridView?
>
> Here's the background for my question...
>
> Before I switched my application over to the Fx 2.0, I used a DataGrid to
> display my data.  I would store different DataGridTableStyles (each one
> with a custom set of columns) in the DataGrid.TableStyles property and
> would use the DataGrid's ability to switch between these sets of columns
> depending on the source data.  You could say that the DataGrid's
> DataGridTableStyle allowed you to have a set of "context sensitive"
> columns.  If the data changed, so too did the columns used to view the
> data.
>
> But when I switched over to the Fx 2.0's DataGridView, I couldn't find an
> analog to the DataGridTableStyle.  To get the same functionality, whenever
> I switch the data source of my DataGridView, I have to clear out the
> DataGridView.Columns and add the appropriate columns back.  This method
> works, but is far less elegant than the way that the old DataGrid works.
> My initial idea was to store my sets of custom columns in different
> DataGridViewColumnCollection instances and set the DataGridView.Columns
> property as appropriate, but the DataGridView.Columns is read-only, so I
> couldn't do that.
>
> So, is there an easy way to assign a DataGridView columns in one step or
> am I stuck with my current method of clearing and reloading the columns
> every time?
>
> Thanks,
>
>   - Mitchell S. Honnert
>
> PS: In the MSDN page "Differences Between the Windows Forms DataGridView
> and DataGrid Controls"
> (http://msdn2.microsoft.com/en-us/library/ms171628.aspx), it says this...
>
> "The only feature that is available in the DataGrid control that is not
> available in the DataGridView control is the hierarchical display of
> information from two related tables in a single control."
>
> I'm hoping that this statement is true and that I'm just overlooking the
> functionality in the DataGridView that equates to the DataGrid's
> DataGridTableStyle.
>
Author
29 Jul 2006 1:15 PM
Mitchell S. Honnert
Ken, I do use the AutoGenerateColumns property to prevent automatic column
loading, but my question was intended to focus more on the setting of custom
columns, especially on the possibility of switching between a predefined set
of custom columns in the DataGridView.

I understand that there is no DataGridViewTableStyle class which directly
corresponds to the DataGridTableStyle, but I was hoping there might be a
roughly similar feature in the DataGridView, albeit one that was implemented
using a different approach.  The quote in the Microsoft documentation about
the DataGridView having all but one off the features of the DataGrid (see my
original post) made me think that the "switch between predefined set of
columns" feature was in the DataGridView *somewhere*; I just had to find it.

Thanks,

- Mitchell S. Honnert


Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:u3v1n%23vsGHA.1296@TK2MSFTNGP02.phx.gbl...
> Hi,
>
>        Setting the Datagridview's AutoGenerateColumns property to false
> will prevent the framework from adding the columns for you.  There is not
> any tablestyle property for the datagridview.
>
> http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.autogeneratecolumns.aspx
>
> Ken
> -------------------
> "Mitchell S. Honnert" <n**@hRoEnMnOeVrEt.com> wrote in message
> news:uhy7e$msGHA.1888@TK2MSFTNGP03.phx.gbl...
>> Is there an equivalent of the DataGrid's DataGridTableStyle for the
>> DataGridView?  If not, is there an easy way to duplicate the
>> DataGridTableStyle's functionality for the DataGridView?
>>
>> Here's the background for my question...
>>
>> Before I switched my application over to the Fx 2.0, I used a DataGrid to
>> display my data.  I would store different DataGridTableStyles (each one
>> with a custom set of columns) in the DataGrid.TableStyles property and
>> would use the DataGrid's ability to switch between these sets of columns
>> depending on the source data.  You could say that the DataGrid's
>> DataGridTableStyle allowed you to have a set of "context sensitive"
>> columns.  If the data changed, so too did the columns used to view the
>> data.
>>
>> But when I switched over to the Fx 2.0's DataGridView, I couldn't find an
>> analog to the DataGridTableStyle.  To get the same functionality,
>> whenever I switch the data source of my DataGridView, I have to clear out
>> the DataGridView.Columns and add the appropriate columns back.  This
>> method works, but is far less elegant than the way that the old DataGrid
>> works. My initial idea was to store my sets of custom columns in
>> different DataGridViewColumnCollection instances and set the
>> DataGridView.Columns property as appropriate, but the
>> DataGridView.Columns is read-only, so I couldn't do that.
>>
>> So, is there an easy way to assign a DataGridView columns in one step or
>> am I stuck with my current method of clearing and reloading the columns
>> every time?
>>
>> Thanks,
>>
>>   - Mitchell S. Honnert
>>
>> PS: In the MSDN page "Differences Between the Windows Forms DataGridView
>> and DataGrid Controls"
>> (http://msdn2.microsoft.com/en-us/library/ms171628.aspx), it says this...
>>
>> "The only feature that is available in the DataGrid control that is not
>> available in the DataGridView control is the hierarchical display of
>> information from two related tables in a single control."
>>
>> I'm hoping that this statement is true and that I'm just overlooking the
>> functionality in the DataGridView that equates to the DataGrid's
>> DataGridTableStyle.
>>
>
>
Author
29 Jul 2006 5:22 PM
Kerry Moorman
Mitchell,

The DataGrid is still available in VS2005, why not continue to use it?

Kerry Moorman


Show quoteHide quote
"Mitchell S. Honnert" wrote:

> Is there an equivalent of the DataGrid's DataGridTableStyle for the
> DataGridView?  If not, is there an easy way to duplicate the
> DataGridTableStyle's functionality for the DataGridView?
>
> Here's the background for my question...
>
> Before I switched my application over to the Fx 2.0, I used a DataGrid to
> display my data.  I would store different DataGridTableStyles (each one with
> a custom set of columns) in the DataGrid.TableStyles property and would use
> the DataGrid's ability to switch between these sets of columns depending on
> the source data.  You could say that the DataGrid's DataGridTableStyle
> allowed you to have a set of "context sensitive" columns.  If the data
> changed, so too did the columns used to view the data.
>
> But when I switched over to the Fx 2.0's DataGridView, I couldn't find an
> analog to the DataGridTableStyle.  To get the same functionality, whenever I
> switch the data source of my DataGridView, I have to clear out the
> DataGridView.Columns and add the appropriate columns back.  This method
> works, but is far less elegant than the way that the old DataGrid works.  My
> initial idea was to store my sets of custom columns in different
> DataGridViewColumnCollection instances and set the DataGridView.Columns
> property as appropriate, but the DataGridView.Columns is read-only, so I
> couldn't do that.
>
> So, is there an easy way to assign a DataGridView columns in one step or am
> I stuck with my current method of clearing and reloading the columns every
> time?
>
> Thanks,
>
>    - Mitchell S. Honnert
>
> PS: In the MSDN page "Differences Between the Windows Forms DataGridView and
> DataGrid Controls" (http://msdn2.microsoft.com/en-us/library/ms171628.aspx),
> it says this...
>
> "The only feature that is available in the DataGrid control that is not
> available in the DataGridView control is the hierarchical display of
> information from two related tables in a single control."
>
> I'm hoping that this statement is true and that I'm just overlooking the
> functionality in the DataGridView that equates to the DataGrid's
> DataGridTableStyle.
>
>
>
Author
29 Jul 2006 7:10 PM
Mitchell S. Honnert
I thought about it, but there are just too many other improvements to the
DataGridView to warrant staying with the DataGrid.  All in all, it's a good
trade-off.  There's only one thing that I ran across that the DataGridView
can't do, and I have a workaround for that, so I don't regret the switch.  I
just wanted to make sure I wasn't missing a "DataGridView way" of switching
between predefined column sets.

- Mitchell S. Honnert


Show quoteHide quote
"Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message
news:C384CA9A-FE4F-494C-81D8-0D3F40EC008C@microsoft.com...
> Mitchell,
>
> The DataGrid is still available in VS2005, why not continue to use it?
>
> Kerry Moorman
>
>
> "Mitchell S. Honnert" wrote:
>
>> Is there an equivalent of the DataGrid's DataGridTableStyle for the
>> DataGridView?  If not, is there an easy way to duplicate the
>> DataGridTableStyle's functionality for the DataGridView?
>>
>> Here's the background for my question...
>>
>> Before I switched my application over to the Fx 2.0, I used a DataGrid to
>> display my data.  I would store different DataGridTableStyles (each one
>> with
>> a custom set of columns) in the DataGrid.TableStyles property and would
>> use
>> the DataGrid's ability to switch between these sets of columns depending
>> on
>> the source data.  You could say that the DataGrid's DataGridTableStyle
>> allowed you to have a set of "context sensitive" columns.  If the data
>> changed, so too did the columns used to view the data.
>>
>> But when I switched over to the Fx 2.0's DataGridView, I couldn't find an
>> analog to the DataGridTableStyle.  To get the same functionality,
>> whenever I
>> switch the data source of my DataGridView, I have to clear out the
>> DataGridView.Columns and add the appropriate columns back.  This method
>> works, but is far less elegant than the way that the old DataGrid works.
>> My
>> initial idea was to store my sets of custom columns in different
>> DataGridViewColumnCollection instances and set the DataGridView.Columns
>> property as appropriate, but the DataGridView.Columns is read-only, so I
>> couldn't do that.
>>
>> So, is there an easy way to assign a DataGridView columns in one step or
>> am
>> I stuck with my current method of clearing and reloading the columns
>> every
>> time?
>>
>> Thanks,
>>
>>    - Mitchell S. Honnert
>>
>> PS: In the MSDN page "Differences Between the Windows Forms DataGridView
>> and
>> DataGrid Controls"
>> (http://msdn2.microsoft.com/en-us/library/ms171628.aspx),
>> it says this...
>>
>> "The only feature that is available in the DataGrid control that is not
>> available in the DataGridView control is the hierarchical display of
>> information from two related tables in a single control."
>>
>> I'm hoping that this statement is true and that I'm just overlooking the
>> functionality in the DataGridView that equates to the DataGrid's
>> DataGridTableStyle.
>>
>>
>>
Author
31 Jul 2006 12:23 PM
Brian Tkatch
Mitchell S. Honnert wrote:
Show quoteHide quote
> Is there an equivalent of the DataGrid's DataGridTableStyle for the
> DataGridView?  If not, is there an easy way to duplicate the
> DataGridTableStyle's functionality for the DataGridView?
>
> Here's the background for my question...
>
> Before I switched my application over to the Fx 2.0, I used a DataGrid to
> display my data.  I would store different DataGridTableStyles (each one with
> a custom set of columns) in the DataGrid.TableStyles property and would use
> the DataGrid's ability to switch between these sets of columns depending on
> the source data.  You could say that the DataGrid's DataGridTableStyle
> allowed you to have a set of "context sensitive" columns.  If the data
> changed, so too did the columns used to view the data.
>
> But when I switched over to the Fx 2.0's DataGridView, I couldn't find an
> analog to the DataGridTableStyle.  To get the same functionality, whenever I
> switch the data source of my DataGridView, I have to clear out the
> DataGridView.Columns and add the appropriate columns back.  This method
> works, but is far less elegant than the way that the old DataGrid works.  My
> initial idea was to store my sets of custom columns in different
> DataGridViewColumnCollection instances and set the DataGridView.Columns
> property as appropriate, but the DataGridView.Columns is read-only, so I
> couldn't do that.
>
> So, is there an easy way to assign a DataGridView columns in one step or am
> I stuck with my current method of clearing and reloading the columns every
> time?
>
> Thanks,
>
>    - Mitchell S. Honnert
>
> PS: In the MSDN page "Differences Between the Windows Forms DataGridView and
> DataGrid Controls" (http://msdn2.microsoft.com/en-us/library/ms171628.aspx),
> it says this...
>
> "The only feature that is available in the DataGrid control that is not
> available in the DataGridView control is the hierarchical display of
> information from two related tables in a single control."
>
> I'm hoping that this statement is true and that I'm just overlooking the
> functionality in the DataGridView that equates to the DataGrid's
> DataGridTableStyle.

The constructor for a DataGridViewColumnCollection requires the
DataGridView it is associated with. So, it cannot be held as instances.

However, as you are doing it, it can be cleared and refilled. Or, more
than one DataGridView can be used, Hide()ing and Show()ing them as
required.

B.
Author
31 Jul 2006 2:15 PM
Mitchell S. Honnert
Show quote Hide quote
"Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
news:1154348583.105535.213230@m73g2000cwd.googlegroups.com...
>
> Mitchell S. Honnert wrote:
>> Is there an equivalent of the DataGrid's DataGridTableStyle for the
>> DataGridView?  If not, is there an easy way to duplicate the
>> DataGridTableStyle's functionality for the DataGridView?
>>
>> Here's the background for my question...
>>
>> Before I switched my application over to the Fx 2.0, I used a DataGrid to
>> display my data.  I would store different DataGridTableStyles (each one
>> with
>> a custom set of columns) in the DataGrid.TableStyles property and would
>> use
>> the DataGrid's ability to switch between these sets of columns depending
>> on
>> the source data.  You could say that the DataGrid's DataGridTableStyle
>> allowed you to have a set of "context sensitive" columns.  If the data
>> changed, so too did the columns used to view the data.
>>
>> But when I switched over to the Fx 2.0's DataGridView, I couldn't find an
>> analog to the DataGridTableStyle.  To get the same functionality,
>> whenever I
>> switch the data source of my DataGridView, I have to clear out the
>> DataGridView.Columns and add the appropriate columns back.  This method
>> works, but is far less elegant than the way that the old DataGrid works.
>> My
>> initial idea was to store my sets of custom columns in different
>> DataGridViewColumnCollection instances and set the DataGridView.Columns
>> property as appropriate, but the DataGridView.Columns is read-only, so I
>> couldn't do that.
>>
>> So, is there an easy way to assign a DataGridView columns in one step or
>> am
>> I stuck with my current method of clearing and reloading the columns
>> every
>> time?
>>
>> Thanks,
>>
>>    - Mitchell S. Honnert
>>
>> PS: In the MSDN page "Differences Between the Windows Forms DataGridView
>> and
>> DataGrid Controls"
>> (http://msdn2.microsoft.com/en-us/library/ms171628.aspx),
>> it says this...
>>
>> "The only feature that is available in the DataGrid control that is not
>> available in the DataGridView control is the hierarchical display of
>> information from two related tables in a single control."
>>
>> I'm hoping that this statement is true and that I'm just overlooking the
>> functionality in the DataGridView that equates to the DataGrid's
>> DataGridTableStyle.
>
> The constructor for a DataGridViewColumnCollection requires the
> DataGridView it is associated with. So, it cannot be held as instances.
>
> However, as you are doing it, it can be cleared and refilled. Or, more
> than one DataGridView can be used, Hide()ing and Show()ing them as
> required.
>
> B.
Thanks Brian.  I thought of the possibility of using different
DataGridViews, but one thing I didn't mention is that the underlying
DataTable is the same for each of my custom column sets.  It just seemed
like overkill to have multiple DataGridViews just to support my different
column sets.

So, basically, it looks like I'll stick with my current solution of
reloading the columns when I need to switch the column set.  I was hoping
that the DataGridView would have duplicated the DataGrid's
DataGridTableStyle functionality somehow, but it certainly looks like this
is not the case.

Thanks again,

- Mitchell S. Honnert
Author
31 Jul 2006 5:10 PM
Brian Tkatch
Mitchell S. Honnert wrote:
Show quoteHide quote
> "Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
> news:1154348583.105535.213230@m73g2000cwd.googlegroups.com...
> >
> > Mitchell S. Honnert wrote:
> >> Is there an equivalent of the DataGrid's DataGridTableStyle for the
> >> DataGridView?  If not, is there an easy way to duplicate the
> >> DataGridTableStyle's functionality for the DataGridView?
> >>
> >> Here's the background for my question...
> >>
> >> Before I switched my application over to the Fx 2.0, I used a DataGrid to
> >> display my data.  I would store different DataGridTableStyles (each one
> >> with
> >> a custom set of columns) in the DataGrid.TableStyles property and would
> >> use
> >> the DataGrid's ability to switch between these sets of columns depending
> >> on
> >> the source data.  You could say that the DataGrid's DataGridTableStyle
> >> allowed you to have a set of "context sensitive" columns.  If the data
> >> changed, so too did the columns used to view the data.
> >>
> >> But when I switched over to the Fx 2.0's DataGridView, I couldn't find an
> >> analog to the DataGridTableStyle.  To get the same functionality,
> >> whenever I
> >> switch the data source of my DataGridView, I have to clear out the
> >> DataGridView.Columns and add the appropriate columns back.  This method
> >> works, but is far less elegant than the way that the old DataGrid works.
> >> My
> >> initial idea was to store my sets of custom columns in different
> >> DataGridViewColumnCollection instances and set the DataGridView.Columns
> >> property as appropriate, but the DataGridView.Columns is read-only, so I
> >> couldn't do that.
> >>
> >> So, is there an easy way to assign a DataGridView columns in one step or
> >> am
> >> I stuck with my current method of clearing and reloading the columns
> >> every
> >> time?
> >>
> >> Thanks,
> >>
> >>    - Mitchell S. Honnert
> >>
> >> PS: In the MSDN page "Differences Between the Windows Forms DataGridView
> >> and
> >> DataGrid Controls"
> >> (http://msdn2.microsoft.com/en-us/library/ms171628.aspx),
> >> it says this...
> >>
> >> "The only feature that is available in the DataGrid control that is not
> >> available in the DataGridView control is the hierarchical display of
> >> information from two related tables in a single control."
> >>
> >> I'm hoping that this statement is true and that I'm just overlooking the
> >> functionality in the DataGridView that equates to the DataGrid's
> >> DataGridTableStyle.
> >
> > The constructor for a DataGridViewColumnCollection requires the
> > DataGridView it is associated with. So, it cannot be held as instances.
> >
> > However, as you are doing it, it can be cleared and refilled. Or, more
> > than one DataGridView can be used, Hide()ing and Show()ing them as
> > required.
> >
> > B.
> Thanks Brian.  I thought of the possibility of using different
> DataGridViews, but one thing I didn't mention is that the underlying
> DataTable is the same for each of my custom column sets.  It just seemed
> like overkill to have multiple DataGridViews just to support my different
> column sets.
>
> So, basically, it looks like I'll stick with my current solution of
> reloading the columns when I need to switch the column set.  I was hoping
> that the DataGridView would have duplicated the DataGrid's
> DataGridTableStyle functionality somehow, but it certainly looks like this
> is not the case.
>
> Thanks again,
>
>  - Mitchell S. Honnert

Yeah, that's how it looks. But note, a DataTable can have more than one
DataView. So, clearing the DataGridView and Fill()ing it again from the
other DataView should (i'm guessing) be enough.

Personally, i have Subs set my Column Collections (because other
DataGridViews on other forms may use the same collection), by accepting
a DataGridView as an input parameter. If i was handling your case, i'd
most likely just recall the appropriate function using the same
DataGridView, much like you expressed.

B.