如果是垂直滚动条,如何在DataGridView中标记最后一行?

时间:2013-09-21 09:33:36

标签: c# winforms datagridview

我已尝试MyDataGridView.Rows[index].Selected = true;,但这标记了所有行。

我想要的是在垂直滚动条的情况下我想看到最后一行而不是手动

这不起作用,因为像Selected property它标记了所有行:

        int last = DataGridView.Rows.Count;
        last = DataGridView.FirstDisplayedScrollingRowIndex;

这是我的添加项目功能:

    private void AddToDataGridView(MyObject obj)
    {
        this.Invoke((MethodInvoker)delegate
        {
            int index = dataGridView.Rows.Add(
                obj.machineIpAddress,
                obj.fileSize,
                obj.fileName,
                obj.processId,
                DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"),
                "Start...");
            dataGridView.Rows[index].Cells[5].Style.BackColor = Color.SkyBlue;
            ColorRow(index, obj.fileSize);
            dataGridView.CurrentCell.Selected = false;
            var lastRow = dataGridView.Rows[dataGridView.Rows.Count - 1];
            lastRow.Selected = true;
            dataGridView.FirstDisplayedCell = lastRow.Cells[0];
        });
    }

此函数聚焦最后一行但选择了DataGridView中的所有行

1 个答案:

答案 0 :(得分:1)

试试这个:

dataGridView1.CurrentCell.Selected = false;
var lastRow = dataGridView1.Rows[dataGridView1.Rows.Count - 1];
//de-select the last selected rows;
dataGridView1.SelectedRows.OfType<DataGridViewRow>().ToList().ForEach(x=>x.Selected=false);
lastRow.Selected = true;
dataGridView1.FirstDisplayedCell = lastRow.Cells[0];