【问题标题】:Get max DataGridViewRow based on Record ID using LINQ使用 LINQ 根据记录 ID 获取最大 DataGridViewRow
【发布时间】:2016-05-25 05:04:54
【问题描述】:

我正在尝试获取具有最大记录 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;

【问题讨论】:

    标签: c# winforms linq datagridview datagridviewrow


    【解决方案1】:

    尝试使用它。

        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();
    

    希望它会有所帮助。

    【讨论】:

    • 这个解决方案工作正常,但我不清楚如何使用 Max
    • MAX 返回与字段相同类型的变量。因此,使用Max,您必须获取最大值,然后通过编写另一个 linq 来获取行的值。
    猜你喜欢
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    相关资源
    最近更新 更多