Microsoft Knowledge Base Email Alertz

KBAlertz.com: (330610) - When you bind a Windows Form DataGrid control, the DataGrid builds a DataGridTableStyle object that contains default properties of the grid column objects. However, you cannot access or modify a DataGridTableStyle object that is built this way. You...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]






Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

HOW TO: Generate a Fully Populated DataGridTableStyle

Article ID:330610
Last Review:July 16, 2004
Revision:2.1
This article was previously published under Q330610
On This Page

SUMMARY

When you bind a Windows Form DataGrid control, the DataGrid builds a DataGridTableStyle object that contains default properties of the grid column objects. However, you cannot access or modify a DataGridTableStyle object that is built this way. You can build one in code, or you can use the Property pane at design-time to add a DataGridTableStyle object to the DataGrid.TableStyles collection.

This step-by-step article describes how to programmatically use the DataGrid control to build a DataGridTableStyle object that you can modify on the fly.

To do this, pass the CurrencyManager object that the DataGrid control is bound to, to the DataGridTableStyle constructor:
Dim ts As New DataGridTableStyle(cm)
				

Back to the top

Sample Application

The sample application binds the DataGrid to the SQL Server Northwind sample database Customers table, and it sets the width of the CompanyName column to 200 pixels. It also has a button that hides or shows the CustomerID column.
1.Follow these steps to create a new Visual Basic Windows Application project:
a. Start Microsoft Visual Studio .NET.
b. On the File menu, point to New, and then click Project.
c. In the New Project dialog box, click Visual Basic Project under Project Types, and then click Windows Application under Templates. By default, Form1 is added.
2.Drag a DataGrid control (DataGrid1) and a Button control (Button1) from the toolbox to the form.
3. Double-click the form background to see the event code. Add the following code to the top of the code window:
Imports System.Data.SqlClient
					
4. Add the following member variable to the form declaration:
Dim ts As DataGridTableStyle
					
5. Add the following code to the Form.Load event:
'
' Fill DataSet
'
Dim con As New SqlConnection("server=localhost;integrated security=true;database=northwind")
Dim daCust As New SqlDataAdapter("Select * From Customers", con)
Dim ds As New DataSet()
daCust.Fill(ds, "Cust")
'
' Bind to grid
'
DataGrid1.DataSource = ds.Tables!Cust
'
' Get CurrencyManager
'
Dim cm As CurrencyManager
cm = CType(Me.BindingContext(ds.Tables!Cust), CurrencyManager)
'
' Create the DataGridTableStyle object and modify it
'
ts = New DataGridTableStyle(cm)
ts.MappingName = "Cust"
ts.GridColumnStyles(1).Width = 200
DataGrid1.TableStyles.Add(ts)
					
NOTE: You may have to modify the connection string based on your environment.

6. Switch back to the form designer, double-click Button1, and then add the following code to the Button1.Click event. The event code hides and unhides the column by changing the width between 0 and 75 pixels (75 is the default).
ts.GridColumnStyles(0).Width = 75 - ts.GridColumnStyles(0).Width
					
7. Run the application. The CompanyName column is now between two and three times as wide as the other columns. Click Button1 several times. The CustomerID column disappears and reappears.

Back to the top

Troubleshooting

• Make sure that the DataGridTableStyle.MappingName matches the name of the DataTable that you bind to. Otherwise, the DataGrid uses the default DataGridTableStyle.
• Make sure that you add the DataGridTableStyle object to the TableStyles collection of the DataGrid. Otherwise, the DataGrid uses the default DataGridTableStyle.
• Make sure that the CurrencyManager that you get is the same to which the DataGrid is bound. Otherwise, the DataGrid uses the default DataGridTableStyle. The following is another method that is more generic than hard-coding the DataTable:
cm = CType(Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember), CurrencyManager)
						
• Make sure that you modify the connection string, the SELECT statement, and the table names to match your database.

Back to the top


APPLIES TO
•Microsoft ADO.NET 1.0
•Microsoft ADO.NET 1.1
•Microsoft Visual Basic .NET 2002 Standard Edition
•Microsoft Visual Basic .NET 2003 Standard Edition

Back to the top

Keywords: 
kbhowtomaster kbsqlclient kbsystemdata kbdatabinding KB330610

Back to the top

   

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

idrees - idrees1979 NOSPAM-AT-NOSPAM hotmai.com Report As Irrelevant  
Written: 4/7/2004 7:38 PM
this is a very good example but i need to know that how data grid works in asp.net when we want to link it to the dataset and make some feilds hyperlink please reply me with thanks. idrees

Todd - todd NOSPAM-AT-NOSPAM datawisetechnologies.com Reported as Irrelevant  
Written: 6/12/2006 3:18 PM
Thanks for the information. I have an immediate and urgent need to lock the first column of a datagridtablestyle object I've created in code. For the life of me I can't figure out how this is done or if it is even possible (surely it is?!). Any help would be GREATLY appreciated. Thanks ahead of time. - Todd

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please