Hiding/showing column in datagrid

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++)
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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
MHQAuthor Commented:
I've tried doing that previously but it doesn't tell me how to unhide the column at a later stage
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
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

MHQAuthor Commented:
'System.Windows.Forms.DataGrid' does not contain a definition for 'Columns'
Éric MoreauSenior .Net ConsultantCommented:
Set ColumnMapping to MappingType.Element
MHQAuthor Commented:
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.
MHQAuthor Commented:
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;

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

Thanx for trying!
PAQed, with points refunded (125)

E-E Admin

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.