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

Article translations Article translations
Article ID: 817247 - View products that this article applies to.
Expand all | Collapse all

On This Page

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.



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:
  1. 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.
  2. On the Data tab of the toolbox, double-click SqlDataAdapter, and then click Next.
  3. Click New Connection. Type the name of your Microsoft SQL Server, user name, and password to connect to the SQL Server.
  4. From the Select the database on the server list, click to select Northwind, and then click OK.
  5. After you create the connection, click Next, and then click Next again.
  6. On the Generate the SQL statement page, type the SQL statement Select * from Customers, and then click Finish.
  7. Right-click SqlDataAdapter1, and then click Generate Dataset. Click OK.
  8. From the toolbox, add a DataGrid control to Form1.
  9. From the toolbox, add two Button controls and two TextBox controls to Form1.
  10. In Solution Explorer, right-click Dataset1, and then click View Code.
  11. 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
  12. 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
  13. 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
  14. On the Build menu, click Build Solution.


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.
  4. Click Sort on City.

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

    The current row data is displayed in the TextBox.

REFERENCES

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

Properties

Article ID: 817247 - Last Review: September 13, 2005 - Revision: 2.2
APPLIES TO
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbwindowsforms kbsystemdata kbsqlclient kbdatabinding kbdatabase kbdataadapter kbhowtomaster KB817247

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com