How to retrieve the DataGrid row that is currently selected after you sort, insert, or delete DataGrid rows by using Visual Basic .NET (817247)
The information in this article applies to:
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
SUMMARYThis step-by-step article describes how to retrieve the DataGrid row that is currently selected after you sort, insert, or delete DataGrid rows. When you sort, insert, or delete DataGrid rows, the currency manager still holds the data in actual format.
The default view for the DataGrid is modified instead of the currency manager. The default view of
the DataGrid incorporates any sorting, inserting, or deleting of the rows. You
can use the default view to retrieve the DataGrid row that is currently selected. back to the topSample code to retrieve the current row of the DataGrid after you sort, insert, or delete rowsTo retrieve the current row of the DataGrid after you sort, insert, or delete rows, follow these steps:
- In Microsoft Visual Studio .NET, create a new Windows
application project by using Microsoft Visual Basic .NET or Microsoft Visual C#
.NET.
By default, Form1 is created. - On the Data tab of the toolbox,
double-click SqlDataAdapter, and then click
Next.
- Click New Connection. Type the name of
your Microsoft SQL Server, user name, and password to connect to the SQL
Server.
- From the Select the database on the server
list, click to select Northwind, and then click
OK.
- After you create the connection, click
Next, and then click Next again.
- On the Generate the SQL statement page,
type the SQL statement Select * from Customers, and then
click Finish.
- Right-click SqlDataAdapter1, and then
click Generate Dataset. Click OK.
- From the toolbox, add a DataGrid control to Form1.
- From the toolbox, add two Button controls
and two TextBox controls to Form1.
- In Solution Explorer, right-click
Dataset1, and then click View
Code.
- Add the following code to bind the
DataGrid with the DataSet:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Text = "Get Current Row"
Button2.Text = "Sort on City"
' Fill the DataSet with the Data from the database
Me.SqlDataAdapter1.Fill(Me.DataSet11)
' Bind the DataGrid with the DataSource
DataGrid1.DataSource = DataSet11.Tables(0).DefaultView
End Sub - Add the following code to the
Button1_click event handler to retrieve the current row data:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Get the Currency Manager by using the BindingContext of the DataGrid
Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember), CurrencyManager)
' Retrieve the default DataView of the DataGrid
Dim dv As DataView = CType(cm.List, DataView)
' Use Currency Manager and DataView to retrieve the Current Row
Dim dr As DataRow
dr = dv.Item(cm.Position).Row
' Display the Current Row Data
TextBox1.Text = dr(0).ToString
TextBox2.Text = dr(1).ToString
End Sub - Add the following code to the
Button2_click event handler to sort the data in the
DataGrid:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Sort the Data
DataSet11.Tables(0).DefaultView.Sort = "City"
' Refresh the Grid to display the Sorted data
DataGrid1.Refresh()
End Sub - On the Build menu, click Build
Solution.
back to the
topVerify that it worksTo verify that it works, follow these steps:
- On the Debug menu, click
Start to run the application.
- Click to select a row in the
DataGrid.
- Click Get Current Row.
The
current row data is displayed in the TextBox. - Click Sort on City.
The rows in
the DataGrid are sorted based on the city. - Click Get Current Row again.
The
current row data is displayed in the TextBox. back to the
topREFERENCES
For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
308070
How to implement a searchable DataGrid by using ADO.NET and Windows Forms
313154 How to create a summary row for a DataGrid in ASP.NET by using Visual Basic .NET
back to the
top
Modification Type: | Minor | Last Reviewed: | 9/13/2005 |
---|
Keywords: | kbWindowsForms kbSystemData kbSqlClient kbDataBinding kbDatabase kbDataAdapter kbHOWTOmaster KB817247 kbAudDeveloper |
---|
|