Solved

Hiding/showing column in datagrid

Posted on 2003-11-17
9
543 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 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular - break the dataset in chunks? 7 50
SQL Exceptions 3 44
Exit the loop 4 42
Problem!!! 8 33
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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