开发者

Get selected DataGridViewRows in currently displayed order

I have a DataGridView with unbound data that contains three different 开发者_如何学JAVADataColumns. The rows can be sorted by each column, but other than that no manipulation of the displayed data is allowed.

When I query the SelectedRows property the rows are sorted in the order I initially inserted them, and not like I expected in the currently displayed or selected order. Is there a way to change this behavior?


I have same problem. Its look the shortest way:

List<DataGridViewRow> rows = 
    (from DataGridViewRow row in dgv.SelectedRows 
    where !row.IsNewRow 
    orderby row.Index 
    select row).ToList<DataGridViewRow>();


The SelectedRows property contains the selected rows but in the reverse order and the most recent item is at the start of the list. To get the correct user selected order do the following code:

List<DataGridViewRow> dgList = new List<DataGridViewRow>();
foreach (DataGridViewRow r in dgv.SelectedRows)
{
    dgList.Insert(0, r);
}
foreach(DataGridViewRow r in dgList)
{
   //Print/consume your row here.
   int selectedIndex = r.Index;
}

Note: No need to sort.


I don't think there's a one-line way to do this. You will need to redo the sort into your own list, then use IndexOf with the SelectedItems to find out the visual position.

List<DataGridViewRow> l = new List<DataGridViewRow>();
foreach (DataGridViewRow r in dgv.Rows)
{
    l.Add(r);
}

l.Sort((x, y) =>
    {
        IComparable yComparable = (IComparable)x.Cells[dgv.SortedColumn.Index].Value;
        IComparable yc = (IComparable)x.Cells[dgv.SortedColumn.Index].Value;

        if (dgv.SortOrder == SortOrder.Ascending)
            return yc.CompareTo(yComparable);
        else
            return yComparable.CompareTo(yc);
    }
    );

foreach(DataGridViewRow r in dgv.SelectedRows)
{
    int selectedIndex = l.IndexOf(r);
}

Note the above has not been compile tested and might need some tweaking.


here is the vb.net version of the above code.

C#

List<DataGridViewRow> rows = 
    (from DataGridViewRow row in dgv.SelectedRows 
    where !row.IsNewRow 
    orderby row.Index 
    select row).ToList<DataGridViewRow>();

VB.NET

Dim rows As List(Of DataGridViewRow) = (From row As DataGridViewRow 
   In dgv.SelectedRows Where Not row.IsNewRow Order By row.Index).ToList()

The C# to VB.NET converters do not translate this correctly.

Hope this helps any VB.NET coder wishing to use this.

Paul


You should be able to refer to the values in the selected cell like this:

Private Sub determinePKVals(ByRef PKVal As String, ByRef PKVal2 As String, Optional ByVal row As Integer = -1)
    If row = -1 Then        ' optional value not passed
        row = dgvDisplaySet.CurrentRow.Index
    End If

    PKVal = dgvDisplaySet.Rows(row).Cells(0).Value
    PKVal2 = dgvDisplaySet.Rows(row).Cells(1).Value
End Sub


SelectedCells seem to be in click order or at least reverse. This one works as simple as possible:

foreach (DataGridViewRow row in dataGridLog.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        if (cell.Selected)
            sb.AppendLine($"{cell.Value.ToString()}");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜