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?
 
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
0
 
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;
0
 
dkloeckCommented:
it should say
"datagrid called dataGridView1" of course :)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
tbaseflugAuthor Commented:
here is what I get:  'System.Windows.Forms.DataGrid' does not contain a definition for 'Columns'      
0
 
dkloeckCommented:
sorry, i was using DataGridView, one moment :)
0
 
dkloeckCommented:
"How to Hide a DataGrid Column Dynamically?" i hope you enjoy this article

http://www.c-sharpcorner.com/Code/2003/June/HideDataGridColDyna.asp
0
 
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
0
 
tbaseflugAuthor Commented:
Andy -

That is exactly what I am looking for.
0
 
AGBrownCommented:
Sorry, I'm not clear - do you need a code example? Or did you mean that that answered your question?
0
 
tbaseflugAuthor Commented:
Code example would be great - thanks!
0
 
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;

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

Andy
0
 
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)
0
 
AGBrownCommented:
Is it possible that one of your mappingname's is not corresponding to a column name in your datasource table?

Andy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.