?
Solved

Win Datagrid Hide Column

Posted on 2006-03-23
15
Medium Priority
?
709 Views
Last Modified: 2012-08-14
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...
0
Comment
Question by:tbaseflug
  • 5
  • 4
  • 4
  • +2
15 Comments
 
LVL 10

Expert Comment

by:dkloeck
ID: 16269993
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
 
LVL 10

Expert Comment

by:dkloeck
ID: 16270014
it should say
"datagrid called dataGridView1" of course :)
0
 

Author Comment

by:tbaseflug
ID: 16270057
here is what I get:  'System.Windows.Forms.DataGrid' does not contain a definition for 'Columns'      
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:dkloeck
ID: 16270102
sorry, i was using DataGridView, one moment :)
0
 
LVL 10

Expert Comment

by:dkloeck
ID: 16270449
"How to Hide a DataGrid Column Dynamically?" i hope you enjoy this article

http://www.c-sharpcorner.com/Code/2003/June/HideDataGridColDyna.asp
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 16271213
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
 

Author Comment

by:tbaseflug
ID: 16271262
Andy -

That is exactly what I am looking for.
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 16271486
Sorry, I'm not clear - do you need a code example? Or did you mean that that answered your question?
0
 

Author Comment

by:tbaseflug
ID: 16271578
Code example would be great - thanks!
0
 

Author Comment

by:tbaseflug
ID: 16271601
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
 
LVL 10

Expert Comment

by:SystemExpert
ID: 16277570

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

Accepted Solution

by:
AGBrown earned 2000 total points
ID: 16279956
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
 
LVL 12

Expert Comment

by:AGBrown
ID: 16279972
SystemExpert: your link is for web datagrids. tbaseflug needs windows datagrids - do you have any you would recommend for that?

Andy
0
 
LVL 6

Expert Comment

by:e1v
ID: 16309454
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
 
LVL 12

Expert Comment

by:AGBrown
ID: 16319973
Is it possible that one of your mappingname's is not corresponding to a column name in your datasource table?

Andy
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 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