Microsoft KB Archive/817247

= How to retrieve the DataGrid row that is currently selected after you sort, insert, or delete DataGrid rows by using Visual Basic .NET =

Article ID: 817247

Article Last Modified on 9/13/2005

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



IN THIS TASK

 * SUMMARY
 * Sample code to retrieve the current row of the DataGrid after you sort, insert, or delete rows
 * Verify that it works
 * REFERENCES



SUMMARY
This 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 top

Sample code to retrieve the current row of the DataGrid after you sort, insert, or delete rows
To 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.</li> From the toolbox, add a DataGrid control to Form1.</li> From the toolbox, add two Button controls and two TextBox controls to Form1.</li> In Solution Explorer, right-click Dataset1, and then click View Code.</li>  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 = &quot;Get Current Row&quot; Button2.Text = &quot;Sort on City&quot;

' 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 </li>  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 </li>  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 = &quot;City&quot; ' Refresh the Grid to display the Sorted data DataGrid1.Refresh End Sub </li> On the Build menu, click Build Solution.</li></ol>

back to the top

Verify that it works
To verify that it works, follow these steps:
 * 1) On the Debug menu, click Start to run the application.
 * 2) Click to select a row in the DataGrid.
 * 3) Click Get Current Row.

The current row data is displayed in the TextBox.
 * 1) Click Sort on City.

The rows in the DataGrid are sorted based on the city.
 * 1) Click Get Current Row again.

The current row data is displayed in the TextBox.

back to the top

<div class="references_section">