简体   繁体   中英

How to mark last row in DataGridView in case of vertical scroll bar?

I have try MyDataGridView.Rows[index].Selected = true; but this marked all the rows.

What i want is in case of vertical scroll bar i want to see the last row without do it manually

This is not working because like Selected property it mark all the rows:

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

This is my add item function:

    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];
        });
    }

This function focus the last row but selected all the rows in DataGridView

Try this:

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM