Microsoft KB Archive/317164

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

Article ID: 317164

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q317164



For a Microsoft Visual Basic .NET version of this article, see 317041.

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:

308484 HOW TO: Display Parent and Child Records in a DataGrid Using Windows Forms with Visual C# .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, Windows 2000 Server, Windows 2000 Advanced Server, or 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 C# .NET:  Start Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Application under Templates.</li></ol> </li>  Add the following line of code to the section where the namespaces are declared to add the System.Data.SqlClient namespace: using 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 section of the 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); CurrencyManager cm = (CurrencyManager)this.BindingContext[dataSet11, &quot;Customers&quot;]; DataView dv = (DataView)cm.List; 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 the 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;, ds, &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;, ds.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:

Consumers of Data on Windows Forms

http://msdn2.microsoft.com/en-us/library/aa983578(VS.71).aspx

back to the top

<div class="references_section">