Hiding columns in a data grid (.net, c#)

R

Rain County

I wish to display, in a datagrid, only six columns of a table containing
about twenty. I've created a data adapter which selects only the desired
columns. I created a dataset and a dataview, and specified the dataview as
the data source for my datagrid.

The datagrid displays the 6 desired columns, in the sequence that I selected
them, followed by the remaning 14. I read someplace that this is the
default behavior.

Two questions:

(1) How do I hide the unwanted 14 coulumns?
(2) How can I change the column headings to something other than the column
name of the underlying table?

Thanks, Phil

raincounty<*>hotmail.com -- replace the <*> with you know what
 
C

Cor Ligthert [MVP]

Rain,

First a queston if you have "selected" 6 columns how can there than be 20
columns in your datatable.

The datagrid can only show what is in the datatable.

In otherwords what do you mean by *selected*.

Tell us as well if you are using 2003 or 2005, the dataview can select
columns in the 2005 version.

Cor
 
P

Paul Gielens

One option is to add a TableStyle to your datagrid in which you can
configure the visible column and column headers. See this article
Customizing the Windows Forms DataGrid
http://msdn.microsoft.com/library/d...ry/en-us/dnwinforms/html/wnf_custdatagrid.asp.

For a more dynamic behavior you can set the Visible property during runtime
grid.Columns[index].Visible = false;

Another option is to disable the "Create Columns automatically at runtime"
via the Property Builder by right clicking the datagrid.

###
Best regards,
Paul Gielens

Visit my blog @ http://weblogs.asp.net/pgielens/
 
R

Rain County

Cor, thanks for the reply.

By 'selected', I mean that the SELECT statement in my OleDbDataAdapter
specifies 6 columns instead of all (*). I am still using VS 2003, and in
case it is pertinent, my data source is an Access table.


Phil
 
R

Rain County

Thanks Paul,

I should mention that I am using VS 2003, and that my data source is a table
in an Access mdb.

I am now studying the article you referred me to below. Hopefully
DataGridTableStyle will provide what I need.

I tried your second suggestion and got: "'System.Windows.Forms.DataGrid'
does not contain a definition for 'Columns'". Is that property available in
2003? Is it available for web pages only?

As to the Property Builder, I can't find it. Is that available for Windows
Forms, or is it just a web page thing?

Phil

Paul Gielens said:
One option is to add a TableStyle to your datagrid in which you can
configure the visible column and column headers. See this article
Customizing the Windows Forms DataGrid
http://msdn.microsoft.com/library/d...ry/en-us/dnwinforms/html/wnf_custdatagrid.asp.

For a more dynamic behavior you can set the Visible property during
runtime grid.Columns[index].Visible = false;

Another option is to disable the "Create Columns automatically at runtime"
via the Property Builder by right clicking the datagrid.

###
Best regards,
Paul Gielens

Visit my blog @ http://weblogs.asp.net/pgielens/

Rain County said:
I wish to display, in a datagrid, only six columns of a table containing
about twenty. I've created a data adapter which selects only the desired
columns. I created a dataset and a dataview, and specified the dataview
as the data source for my datagrid.

The datagrid displays the 6 desired columns, in the sequence that I
selected them, followed by the remaning 14. I read someplace that this
is the default behavior.

Two questions:

(1) How do I hide the unwanted 14 coulumns?
(2) How can I change the column headings to something other than the
column name of the underlying table?

Thanks, Phil

raincounty<*>hotmail.com -- replace the <*> with you know what
 
R

Rain County

Paul, thanks a lot. Option one, adding a table style as shown in the link
you provided works perfectly. A bit tedious, but it allows a lot of
flexibility!

Phil

Paul Gielens said:
One option is to add a TableStyle to your datagrid in which you can
configure the visible column and column headers. See this article
Customizing the Windows Forms DataGrid
http://msdn.microsoft.com/library/d...ry/en-us/dnwinforms/html/wnf_custdatagrid.asp.

For a more dynamic behavior you can set the Visible property during
runtime grid.Columns[index].Visible = false;

Another option is to disable the "Create Columns automatically at runtime"
via the Property Builder by right clicking the datagrid.

###
Best regards,
Paul Gielens

Visit my blog @ http://weblogs.asp.net/pgielens/

Rain County said:
I wish to display, in a datagrid, only six columns of a table containing
about twenty. I've created a data adapter which selects only the desired
columns. I created a dataset and a dataview, and specified the dataview
as the data source for my datagrid.

The datagrid displays the 6 desired columns, in the sequence that I
selected them, followed by the remaning 14. I read someplace that this
is the default behavior.

Two questions:

(1) How do I hide the unwanted 14 coulumns?
(2) How can I change the column headings to something other than the
column name of the underlying table?

Thanks, Phil

raincounty<*>hotmail.com -- replace the <*> with you know what
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top