使用LINQ基于记录ID获取最大DataGridViewRow

时间:2016-02-14 08:35:41

标签: c# winforms linq datagridview datagridviewrow

我正在尝试使用max record id值获取DataGridViewRow。 我能够获得记录ID本身,但我需要找到行本身。

这是我获取最大记录ID的代码,它运行正常:

var addedRow =
                dataGridView.Rows
                .Cast<DataGridViewRow>()
                .Where(r => r.Cells[Glossary.RowType].Value.ToString().Equals(Glossary.RowTypeCustom))
                .Select(r => Convert.ToInt(r.Cells[Glossary.RecordId].Value))
                .Max();

我试过了:

.Max(r => r.Cells[Glossary.RecordId].Value);

我不清楚如何获取行本身,所以我可以专注于它:

dataGridView.CurrentCell = addedRow;

1 个答案:

答案 0 :(得分:4)

尝试使用此功能。

    var addedRow =
            dataGridView.Rows
            .Cast<DataGridViewRow>()
            .Where(r => r.Cells[Glossary.RowType].Value.ToString().Equals(Glossary.RowTypeCustom))
            .OrderByDescending(r => Convert.ToInt(r.Cells[Glossary.RecordId].Value))
            .FirstOrDefault();
希望它会有所帮助。

相关问题