Microsoft Knowledge Base Email Alertz

KBAlertz.com: (317041) - This article describes how to retrieve a DataView object from a Windows Forms bound control. Description of the Technique To bind a data source to a Windows Forms control, you can code it yourself or use design-time binding. When the binding occurs at...

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! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

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




Article ID: 317041 - Last Review: May 13, 2007 - Revision: 2.1

HOW TO: Retrieve DataView of a Windows Forms Bound Control in Visual Basic .NET

This article was previously published under Q317041

On This Page

SUMMARY

This article describes how to retrieve a DataView object from a Windows Forms bound control.

Description of the Technique

To bind a data source to a Windows Forms control, you can code it yourself or use design-time binding. When the binding occurs at design time, you may need to access the DataView that the bound control uses. However, the DataView is not exposed by default in design-time binding.

You can use one of these methods to access the DataView:
  • Use the BindingContext class to obtain a reference to the CurrencyManager object that is generated from the binding. Subsequently, use the CurrencyManager to obtain a reference to the DataView. This is the most reliable method.

    For additional information about how to use the CurrencyManager in Visual Basic .NET, click the article number below to view the article in the Microsoft Knowledge Base:
    308052  (http://kbalertz.com/Feedback.aspx?kbNumber=308052/EN-US/ ) HOW TO: Display Parent and Child Records in a DataGrid Using Windows Forms by Using Visual Basic .NET
  • Use the DefaultView property of the DataTable object. However, you cannot guarantee that the DefaultView of the DataTable is the same DataView that the bound control uses.

    For example, if multiple DataGrid controls are bound to a DataTable, the binding may or may not generate additional DataView objects, depending on the syntax that you use to bind. For more information about how differences in binding syntax can generate multiple DataView objects, refer to the Troubleshooting section.
This article demonstrates how to use the BindingContext class to access the DataView.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows NT 4.0 Server
  • Microsoft Visual Studio .NET
  • Microsoft SQL Server 7.0 or later
This article assumes that you are familiar with the following topics:
  • Visual Studio .NET
  • ADO.NET fundamentals and syntax

Create Project and Add Code

  1. Follow these steps to create a new Windows Application project in Visual Basic .NET:
    1. Start Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates.
  2. In the General Declarations section of Form1, add the following line of code:
    Imports System.Data.SqlClient
    					
  3. Drag a SqlDataAdapter object from the Data section of the toolbox to your form.
  4. Follow these steps in the Configuration Wizard:
    1. When the Configuration Wizard starts, click Next.
    2. Click New Connection, and then configure a connection to the Northwind database on your SQL Server. Click OK, and then click Next.
    3. Click Use SQL statements, and then click Next.
    4. Type the following SQL Query string:
      select * from customers
      						
    5. Click Finish.
  5. Right-click the SqlDataAdapter in the pane below your form, and then click Generate Dataset. Make sure that the New is selected, and then click OK.
  6. Drag a DataGrid control from the Windows Forms toolbox onto your form. Click to select the DataGrid.
  7. In the Properties pane, select the DataSet that you generated for the DataSource property, and then click Customers for the DataMember property.
  8. Double-click the form to add a Form1_Load event handler, and then add the following code to the event handler:
    SqlDataAdapter1.Fill(DataSet11)
    Dim cm As CurrencyManager = CType(Me.BindingContext(DataSet11, "Customers"), CurrencyManager)
    Dim dv As DataView = CType(cm.List, DataView)
    dv.Sort = "contactname DESC"
    						
    NOTE: Make sure that the DataSet and the SqlDataAdapter are named according to the objects that you added to your form.

    NOTE: If you are using SQL Authentication, make sure that the password is included in the ConnectionString property of SQLConnection1.
  9. Press F5 to compile and to run that application. Notice that the data in your DataGrid is sorted by contact name in descending order. You have successfully retrieved the DataView from the bound DataGrid.

Troubleshooting

The syntax that you use to bind a control to a data source determines whether the control shares a CurrencyManager object that has already been created or creates a new CurrencyManager. Be consistent in how you set the DataSource, the DisplayMembers, the DataMembers, and the DataBindings properties. If you are not consistent, the BindingContext object creates multiple CurrencyManager objects for the DataSet. This can result in unexpected behavior.

For example, if you bind a DataGrid at design time by setting its DataSource property to DataSet1 and by setting its DataMember property to Customers, you must use the following code to bind a TextBox control to the same DataSet with the same CurrencyManager:
TextBox1.DataBindings.Add("text", DataSet1, "customers.contactname")
				
Although the code to follow illustrates a valid data binding example, the DataGrid is bound inconsistently. Therefore, this code creates a second CurrencyManager object.
TextBox1.DataBindings.Add("text", DataSet1.Tables("customers"), "contactname")
				
If you want to bind a DataGrid to the DataSet and share the same CurrencyManager that is generated for the TextBox in the last binding code sample, you must set the DataSource property to DataSet1.Customers and leave the DataMember property empty.

For more information about this topic, see the following Microsoft Visual Studio .NET Help documentation on Microsoft Developer Network (MSDN) Library:
Consumers of Data on Windows Forms
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconConsumersOfDataOnWindowsForms.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconConsumersOfDataOnWindowsForms.asp)

REFERENCES

For more information about how to create and how to use DataView objects, refer to the following Microsoft .NET Framework Software Development Kit (SDK) documentation:
Creating and Using DataViews
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcreatingusingdataviews.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcreatingusingdataviews.asp)
For more information about the CurrencyManager class, refer to the following MSDN documentation:
CurrencyManager Class
http://msdn2.microsoft.com/en-us/library/system.windows.forms.currencymanager(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.currencymanager(vs.71).aspx)

APPLIES TO
  • Microsoft ADO.NET 1.1
  • Microsoft ADO.NET 1.0
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbdataadapter kbdatabinding kbhowtomaster kbsqlclient kbsystemdata KB317041
       

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

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please