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.
- In the General Declarations section of Form1, add the following line of code:
Imports System.Data.SqlClient
- Drag a SqlDataAdapter object from the Data section of the toolbox to your form.
- Follow these steps in the Configuration Wizard:
- When the Configuration Wizard starts, click Next.
- Click New Connection, and then configure a connection to the Northwind database on your SQL Server. Click OK, and then click Next.
- Click Use SQL statements, and then click Next.
- Type the following SQL Query string:
select * from customers
- Click Finish.
- 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.
- Drag a DataGrid control from the Windows Forms toolbox onto your form. Click to select the DataGrid.
- In the Properties pane, select the DataSet that you generated for the DataSource property, and then click Customers for the DataMember property.
- 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. - 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.
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("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:
back to the top