Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 840961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:42:49+00:00 2026-05-15T05:42:49+00:00

I have a DataGrid containing a small table. Until now, I had a double

  • 0

I have a DataGrid containing a small table. Until now, I had a double click handler on this grid which iterated over all rows to find the selected one:

DataTable table = (DataTable)this.dataGrid.DataSource;
int selectedRow = -1;
for (int i=0; i<table.Rows.Count; i++)
  if (this.dataGrid.IsSelected(i))
    selectedRow = i;
    break;
  }
if ( selectedRow != -1 ) {
  DataRow row = table.Rows[selectedRow];
  // More code ...
}

Problem: When the user clicks on a column header and sort the table, table.Rows does not return the right rows. It still contains the unsorted rows.

How can I get the right column?

Edit 1: I have a System.Windows.Forms.DataGrid, not a DataGridView. I don’t know the difference, because I don’t know .Net very much. Can I simply replace DataGrid with DataGridView?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:42:50+00:00Added an answer on May 15, 2026 at 5:42 am

    DataGrid (Windows)

    Try DataGrid.GetSelectedDataRows below, where MyBase is the name of your DataGrid.

        Public Function GetSelectedDataRows() As DataRow()
            Dim oSelectedRows As New ArrayList
            Dim oDataTable As DataTable = DirectCast(MyBase.DataSource, DataTable)
            For i As Integer = 0 To oDataTable.Rows.Count - 1
                If MyBase.IsSelected(i) Then
                    oSelectedRows.Add(oDataTable.DefaultView(i).Row)
                End If
            Next
            Return DirectCast(oSelectedRows.ToArray(GetType(DataRow)), DataRow())
        End Function
    

    DataGridView

    Use the SelectedRows property. It returns the collection of DataGridViewRow objects. Since you know you are binding a DataTable, the DataGridViewRow.DataBoundItem property will be a DataRow. Check the above object help topics for examples.

    References

    • Differences Between the Windows Forms DataGridView and DataGrid Controls at http://msdn.microsoft.com/en-us/library/ms171628.aspx

    • DataGridView Class at
      http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx

    • DataGrid Class at http://msdn.microsoft.com/en-us/library/system.windows.forms.datagrid.aspx

    Excerpts

    The DataGrid control is retained for
    backward compatibility and for special
    needs. For nearly all purposes, you
    should use the DataGridView control.
    The only feature that is available in
    the DataGrid control that is not
    available in the DataGridView control
    is the hierarchical display of
    information from two related tables in
    a single control. You must use two
    DataGridView controls to display
    information from two tables that are
    in a master/detail relationship.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this datagrid view: Now when the first row is double clicked i
I have Datagrid which is bound to my Database <WpfToolkit:DataGrid Name=DataGrid1 Grid.Row=1 ItemsSource={Binding Path=MainSearchBinding}
I have an usercontrol containing DataGrid (datagrid allow user edit on grid), but when
I have a Silverlight app with a DataGrid containing some custom columns and all
I have a datagrid in C#.net I have made some columns in grid editable.
I have a datagrid view which is editable. I'm getting the value of a
I have a silverlight datagrid which is bound to a PagedCollectionView displaying a collection
I have a View that displays a DataGrid which is bound to an ObservableCollection
I have MainWindow containing a datagrid and a filter panel. The filter panel can
I have Datagrid which is clicking by mouse in each row is showing data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.