Solved

Hiding/showing column in datagrid

Posted on 2003-11-17
9
547 Views
Last Modified: 2008-02-01
I'm doing a windows aplications and need to be able to hide a column in a datagrid. I also need to be able to have it reapear. This is to be done with the checking/unchecking of a checkbox.

This is the code I have (important parts) ("result" is the datagrid in the form)

public DataTable table = new DataTable("result");
public DataView myDataView;
string columns = new String[13] {"date", "time", "c-ip", "c-port", "cs-username", "cs-method", "cs-uri-stem", "cs-uri-query", "sc-status", "sc-bytes", "cs-bytes", "s-name", "s-port"};
int i;
for(i=0; i<=12; i++)
{
table.Columns.Add(columns[i],typeof(String));
}
myDataView = new DataView(table);
result.DataSource = myDataView;

I also have a function for retreiving data and placing it in the table and it works fine. I don't want to run this every time I unhide a column again. I've tried accessing width of a column, visibility etc but in vain. How can this be done?

Regards,
MHQ
0
Comment
Question by:MHQ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 9768283
0
 
LVL 3

Author Comment

by:MHQ
ID: 9771563
I've tried doing that previously but it doesn't tell me how to unhide the column at a later stage
0
 

Expert Comment

by:Natasja_
ID: 9772148
private void ToggleColumn( int ColumnIndex) {

bool isVisible = result.Columns[ int ColumnIndex].Visible;
result.Columns[ int ColumnIndex].Visible = ( !isVisible );

}

this will hide/show your column after you fill your datagrid
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:MHQ
ID: 9772989
'System.Windows.Forms.DataGrid' does not contain a definition for 'Columns'
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 9775478
Set ColumnMapping to MappingType.Element
0
 
LVL 3

Author Comment

by:MHQ
ID: 9779092
myDataView.Table.Columns[2].ColumnMapping = MappingType.Element;

Doesn't work... the columns is hidden (as I have previously managed to do) but won't reappear.
0
 
LVL 3

Author Comment

by:MHQ
ID: 9780347
I got some help from a tutor and solved the problem. What I had to do was to control the width of the column using DataGridTableStyle in conjunction with some other Styles.

DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = MyDataGrid.DataMember;
MyDataGrid.TableStyles.Add(tableStyle);

and to toggle:
private void Toggle()
{
DataGridColumnStyle column = MyDataGrid.TableStyles[0].GridColumnStyles["ID"];
if (column.Width==0)
{
column.Width = 75;
}
else
{
column.Width = 0;
}
}

Thanx for trying!
Regards,
MHQ
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 9802898
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question