How to get the right row in a sorted DataView?
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 differ开发者_如何学运维ence, because I don't know .Net very much. Can I simply replace DataGrid with DataGridView?
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.
Why not use DataGridView.SelectedRows
property? Then use DataBoundItem
for those rows to access underlying data. This may be of type DataRowView
. In that case, use the DataRowView.Row
property.
foreach (DataGridViewRow dgvrow in dataGrid.SelectedRows)
{
DataRow row = null;
if (dgvrow.DataBoundItem is DataRowView)
row = (dgvrow.DataBoundItem as DataRowView).Row as DataRow;
else
row = dgvrow.DataBoundItem as DataRow;
// ... stuff
}
精彩评论