Win Datagrid Hide Column

How can I hide a column in a windows datagrid - specifically a mobile datagrid - setting width to 0 is not the answer I am hoping for...
tbaseflugAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

dkloeckCommented:
if you have a datagrid called datagrid, u just can get the right column by index and set his visible property to false like this:

dataGridView1.Columns[0].Visible = false;
dkloeckCommented:
it should say
"datagrid called dataGridView1" of course :)
tbaseflugAuthor Commented:
here is what I get:  'System.Windows.Forms.DataGrid' does not contain a definition for 'Columns'      
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

dkloeckCommented:
sorry, i was using DataGridView, one moment :)
dkloeckCommented:
"How to Hide a DataGrid Column Dynamically?" i hope you enjoy this article

http://www.c-sharpcorner.com/Code/2003/June/HideDataGridColDyna.asp
AGBrownCommented:
Hmmm, that article applies to the web datagrid. Is that what you need tbaseflug? If not, then this is what i use in windows clients, i'm not sure about mobile clients. I work with the TableStyles collection property of the datagrid. This is the mechanism by which, in 1.1, you could alter the appearance of specific columns in a datagrid. You add only the columns to the TableStyle collection that you want to see.

Do you want a code example?

Andy
tbaseflugAuthor Commented:
Andy -

That is exactly what I am looking for.
AGBrownCommented:
Sorry, I'm not clear - do you need a code example? Or did you mean that that answered your question?
tbaseflugAuthor Commented:
Code example would be great - thanks!
tbaseflugAuthor Commented:
Here is what I am using now - The problem is that when it gets to the following line it throws this error:

line of error: grdOrders.TableStyles.Add(dgtsStyle);
error: Value does not fall within the expected range.


Here is the code that I am using now:
/////////////////////////////////////////////
        public void AddCustomDataTableStyle(int Mode)
        {
            DataGridTableStyle dgtsStyle;
            DataGridTextBoxColumn dgtsColumn;
            DataGridTextBoxColumn dgtsColumn1;
            DataGridTextBoxColumn dgtsColumn2;

            dgtsStyle = new DataGridTableStyle();
            dgtsStyle.MappingName = grdOrders.DataSource.ToString();
            dgtsStyle.GridColumnStyles.Clear();

            if (Mode == 1 || Mode == 3)
            {
                // Set Order Quantity
                dgtsColumn1 = new DataGridTextBoxColumn();
                dgtsColumn1.MappingName = "OrderQuantity";
                dgtsColumn1.HeaderText = "Ord";
                dgtsColumn1.Width = 26;
                dgtsStyle.GridColumnStyles.Add(dgtsColumn1);
            }

            if (Mode == 2 || Mode == 3)
            {
                // Set Quantity On Hand
                dgtsColumn2 = new DataGridTextBoxColumn();
                dgtsColumn2.MappingName = "QuantityOnHand";
                dgtsColumn2.HeaderText = "QOH";
                dgtsColumn2.Width = 26;
                dgtsStyle.GridColumnStyles.Add(dgtsColumn2);
            }

            // Set Par Level
            dgtsColumn = new DataGridTextBoxColumn();
            dgtsColumn.MappingName = "ParLevel";
            dgtsColumn.HeaderText = "Par";
            dgtsColumn.Width = 26;
            dgtsStyle.GridColumnStyles.Add(dgtsColumn);

            // Set Item Description
            dgtsColumn = new DataGridTextBoxColumn();
            dgtsColumn.MappingName = "Description";
            dgtsColumn.HeaderText = "Description";
            dgtsColumn.Width = 160;
            dgtsStyle.GridColumnStyles.Add(dgtsColumn);

            // Set ItemID - set width to 0 to hide from display
            dgtsColumn = new DataGridTextBoxColumn(); ;
            dgtsColumn.MappingName = "ItemID";
            dgtsColumn.Width = 0;
            dgtsStyle.GridColumnStyles.Add(dgtsColumn);

            //if (TablesAlreadyAdded) return;

            //  Add the style to the DataGrid.
            grdOrders.TableStyles.Add(dgtsStyle);

            // Set the TablesAlreadyAdded to prevent redundant execution
            //TablesAlreadyAdded = true;
            //return dgtsStyle;

        }
SystemExpertCommented:

Hi  100 % solution what you want in c#

Test code can be donload at this link

http://www.wwwcoder.com/main/parentid/254/site/5423/68/default.aspx

Thanks
AGBrownCommented:
The only difference between your code and the code example I started writing for you last night is that I was using

grdOrders.TableStyles.Clear();
grdOrders.TableStyles.Add(dgtsStyle);

i.e., clearing the table styles collection first.

Does that work?

Andy

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
AGBrownCommented:
SystemExpert: your link is for web datagrids. tbaseflug needs windows datagrids - do you have any you would recommend for that?

Andy
e1vCommented:
I haven't looked at your code, but have you seen this?:

 http://msdn2.microsoft.com/en-us/library/79zaexf1.aspx
(How to: Delete or Hide Columns in the Windows Forms DataGrid Control)
AGBrownCommented:
Is it possible that one of your mappingname's is not corresponding to a column name in your datasource table?

Andy
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
C#

From novice to tech pro — start learning today.