Solved

DataGrid - Resizing and Hiding Columns

Posted on 2004-10-17
6
433 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
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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

762 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