c#在DataGridview中显示选定的行

时间:2018-07-18 10:19:50

标签: c# winforms

我的datagridview中填充了多行数据,我能够在我的datagridview中选择多行,然后想要更新datagridview以显示选定的行。

在这种情况下,我能够在屏幕上已经显示所选行的情况(简而言之:如果需要将datagridview向上滚动到所选行,或者将其滚动到结束行)

do
{
    for (int j = 0; j <= stringToHighlight.Length; j++)
    {
        if (ColID > 15)
        {
            rowID++;
            ColID = 0;
        }
        if (stringToHighlight[j].ToString().ToLower() == dataGridView1.Rows[rowID].Cells[ColID].Value.ToString().ToLower())
        {
            GridView_Binary.Rows[rowID].Cells[ColID].Selected = true;
            dataGridView1.Rows[rowID].Cells[ColID].Selected = true;
            ColID++;
        }
        else
        {
            GridView_Binary.ClearSelection();
            dataGridView1.ClearSelection();
            break;
        }

        if (j == stringToHighlight.Length - 1)
        {
            ISNext = false;

            return true;
        }
    }
    ColID++;
}
while (ISNext);

1 个答案:

答案 0 :(得分:0)

要滚动到选定的行,请使用此:

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;

如果要进行所有检查,代码将如下所示:

//Checking if data grid view have rows
if(dataGridView1.Rows.Count > 0)
{
    //Checking if anything is selected
    if(dataGridView1.SelectedRows.Count > 0)
    {
        //Selecting first of all selected rows in datagridview
        dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
    }
    else
    {
        //Selecting last row in data grid view
        dataGridView1.FirstDisplayedScrollingRowIndex = (dataGridView1.Rows.Count - 1);
    }
}