Solved

Hiding/showing column in datagrid

Posted on 2003-11-17
9
540 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
9 Comments
 
LVL 69

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

Author Comment

by:MHQ
ID: 9772989
'System.Windows.Forms.DataGrid' does not contain a definition for 'Columns'
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 69

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now