Solved

DataGrid - Resizing and Hiding Columns

Posted on 2004-10-17
6
428 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
ID: 12336134
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
ID: 12336194
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
ID: 12336282
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:checoo
ID: 12336304
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
ID: 12337181
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
ID: 12341337
Thanks very much - works perfectly!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

15 Experts available now in Live!

Get 1:1 Help Now