DatagridView Select last row

后端 未结 8 2012
你的背包
你的背包 2021-02-14 17:20

I have some trouble with setting the last row in my datagridview selected. I select the last row this way:

if (grid.Rows.Count > 0)
{
    try
    {
        gr         


        
8条回答
  •  灰色年华
    2021-02-14 17:49

    To avoid the IndexOutOfRangeException make sure only to select a visible row. I suggest:

    // Find last visible row
    DataGridViewRow row = dataGridView1.Rows.Cast().Where(r => r.Visible).Last(); 
    // scroll to last row if necessary
    dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.IndexOf(row);
    // select row
    row.Selected = true;
    

提交回复
热议问题