Solved

DataGrid - Resizing and Hiding Columns

Posted on 2004-10-17
6
432 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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