Solved

DataGrid - Resizing and Hiding Columns

Posted on 2004-10-17
6
427 Views
Last Modified: 2010-04-23
How can I resize columns within a DataGrid and how can I hide certain columns within a DataGrid?
0
Comment
Question by:PeterErhard
6 Comments
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 500 total points
Comment Utility
You need to create a DataGridTableStyle Type object and DataGridColumnStyle Type object and set its properties

eg.

        Dim ts1 As New DataGridTableStyle
        ts1.MappingName = "MyTable" 'The table underlying the DataGrid

'Setting Column Width
        Dim Col As New DataGridTextBoxColumn
        Col.MappingName = "Col1" 'The Column you need to format
        Col.HeaderText = "Title of Col1"
        Col.Width = 150 'This will set the Width of the Column
        ts1.GridColumnStyles.Add(Col)

'Hiding a Column
        Dim Col As New DataGridTextBoxColumn
        Col.MappingName = "Col2" 'The Column you need to Hide
        Col.HeaderText = ""
        Col.Width = 0 'This will Hide the Column
        ts1.GridColumnStyles.Add(Col)

Then Don't Forget to add the Style to the Grid

DataGrid1.TableStyles.Add(ts1)


Alternately you can hide the column of underlying Table use:

DataGrid1.Tables("MyTable").Columns(Col2).ColumnMapping=MappingType.Hidden


0
 

Author Comment

by:PeterErhard
Comment Utility
Thanks for that. I'm using the following but all the columns show up as EMail. What am I doing wrong?

 Dim ts1 As New DataGridTableStyle()
            Dim Col As New DataGridTextBoxColumn()

            ts1.MappingName = "Providers"

            Col.MappingName = "ReferralServiceProviderIDAuto"
            Col.HeaderText = ""
            Col.Width = 0
            ts1.GridColumnStyles.Add(Col)
            Col.MappingName = "ProviderName"
            Col.HeaderText = "Name"
            Col.Width = 100
             ts1.GridColumnStyles.Add(Col)
            Col.MappingName = "Address"
            Col.HeaderText = "Address"
            Col.Width = 100
            ts1.GridColumnStyles.Add(Col)
            Col.MappingName = "City"
            Col.HeaderText = "City"
            Col.Width = 100
            ts1.GridColumnStyles.Add(Col)
            ts1.GridColumnStyles.Add(Col)
            Col.MappingName = "ContactPerson"
            Col.HeaderText = "ContactPerson"
            Col.Width = 100
           ts1.GridColumnStyles.Add(Col)
            Col.MappingName = "Phone"
            Col.HeaderText = "Phone"
            Col.Width = 100
            ts1.GridColumnStyles.Add(Col)
            Col.MappingName = "EMail"
            Col.HeaderText = "EMail"
            Col.Width = 100
            ts1.GridColumnStyles.Add(Col)

            Grid_SearchResult.TableStyles.Add(ts1)
0
 
LVL 10

Expert Comment

by:123654789987
Comment Utility
U can hide a column by setting its width to 0

DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0  // This will hide your ProviderName column ( first column)


U can follow the following link

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskaddingdeletingorhidingcolumns.asp
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

Expert Comment

by:checoo
Comment Utility
check out the following site for all datagrid related queries

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp
0
 
LVL 19

Expert Comment

by:arif_eqbal
Comment Utility
You are not Resetting the Object Reference for Col

After each

ts1.GridColumnStyles.Add(Col)

Insert a Line

Col = New DataGridTextBoxColumn()

0
 

Author Comment

by:PeterErhard
Comment Utility
Thanks very much - works perfectly!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now