Microsoft KB Archive/317041

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

Article ID: 317041

Article Last Modified on 5/13/2007

-

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

-



This article was previously published under Q317041



For a Microsoft Visual C# .NET version of this article, see 317164.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.SqlClient

IN THIS TASK
SUMMARY
 * Description of the Technique
 * Requirements
 * Create Project and Add Code
 * Troubleshooting

REFERENCES



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

back to the top

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 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.

back to the top

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

back to the top

Create Project and Add Code
 Follow these steps to create a new Windows Application project in Visual Basic .NET:  Start Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates.</li></ol> </li>  In the General Declarations section of Form1, add the following line of code: Imports System.Data.SqlClient </li> Drag a SqlDataAdapter object from the Data section of the toolbox to your form.</li> Follow these steps in the Configuration Wizard: <ol style="list-style-type: lower-alpha;"> When the Configuration Wizard starts, click Next.</li> Click New Connection, and then configure a connection to the Northwind database on your SQL Server. Click OK, and then click Next.</li> Click Use SQL statements, and then click Next.</li>  Type the following SQL Query string: select * from customers </li> Click Finish.</li></ol> </li> 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.</li> Drag a DataGrid control from the Windows Forms toolbox onto your form. Click to select the DataGrid.</li> In the Properties pane, select the DataSet that you generated for the DataSource property, and then click Customers for the DataMember property.</li>  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, &quot;Customers&quot;), CurrencyManager) Dim dv As DataView = CType(cm.List, DataView) dv.Sort = &quot;contactname DESC&quot; 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. </li> 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.</li></ol>

back to the top

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(&quot;text&quot;, DataSet1, &quot;customers.contactname&quot;) 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(&quot;text&quot;, DataSet1.Tables(&quot;customers&quot;), &quot;contactname&quot;) 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

back to the top

<div class="references_section">