如何选择并滚动到Datagridview中的新行?

时间:2021-08-21 15:50:07

I have a DataGridView bound to a DB Table. The DataGridView is not editable, there are some text fields where the data can be edited, which is controlled with buttons. I have a NewRow Button with the following code:

我有一个绑定到数据库表的DataGridView。 DataGridView不可编辑,有一些文本字段可以编辑数据,这些字段由按钮控制。我有一个NewRow按钮,代码如下:

        dataGridView1.AllowUserToAddRows = true;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.Selected)
            { row.Selected = false; }
        } 
        dataGridView1.Rows[dataGridView1.NewRowIndex].Selected = true;

Wat I need is:

我需要的是:

  • the datagridview should scroll to the bottom (where the newRow is)
  • datagridview应滚动到底部(newRow所在的位置)

  • the newRow should be focused, so that the textfields show the new (empty) row (content)
  • 应该关注newRow,以便textfields显示新的(空)行(内容)

I've tried:

        bindSourceGS.Position = dataGridView1.NewRowIndex;

but that doesn't select the datagridview's newRow. I want to use the datagridview's newRow because when user presses cancel button I don't have to delete the row in the Dataset and the datagridview.Rows[i] has a IsNewRow Property.

但是这并没有选择datagridview的newRow。我想使用datagridview的newRow,因为当用户按下取消按钮时,我不必删除数据集中的行,而datagridview.Rows [i]有一个IsNewRow属性。

3 个解决方案

#1


12  

If its Winform then you can use this

如果它的Winform然后你可以使用它

dataGridView1.FirstDisplayedScrollingRowIndex

and set the datagridview.CurrentCell to your new row cell address.

并将datagridview.CurrentCell设置为新的行单元格地址。

Hope this helps

希望这可以帮助

#2


0  

Note: For FirstDisplayedScrollingRowIndex to succeed if you have mixed-hieght rows, you need to ensure that Row Template height is at least as great at the last row height. This is rather a problem since I find one cannot set any row height greater than the Row Template height.

注意:如果您有混合高行,FirstDisplayedScrollingRowIndex要成功,您需要确保行模板高度至少与最后一行高度一样好。这是一个相当大的问题,因为我发现无法将行高度设置为大于行模板高度。

#3


0  

I've been researching this problem and found that setting dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.count-1 works well as long as the AutoSizeRowsMode property is NOT set to DisplayedCells.

我一直在研究这个问题,发现只要AutoSizeRowsMo​​de属性未设置为DisplayedCells,设置dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.count-1就可以正常工作。

If you have problems with setting dataGridView1.FirstDisplayedScrollingRowIndex=dataGridView1.Rows.Count -1 and the grid not scrolling to bottom try setting the AutosizeRowsMode property to AllCells.

如果您在设置dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count -1时出现问题,并且网格未滚动到底部,请尝试将AutosizeRowsMo​​de属性设置为AllCells。

#1


12  

If its Winform then you can use this

如果它的Winform然后你可以使用它

dataGridView1.FirstDisplayedScrollingRowIndex

and set the datagridview.CurrentCell to your new row cell address.

并将datagridview.CurrentCell设置为新的行单元格地址。

Hope this helps

希望这可以帮助

#2


0  

Note: For FirstDisplayedScrollingRowIndex to succeed if you have mixed-hieght rows, you need to ensure that Row Template height is at least as great at the last row height. This is rather a problem since I find one cannot set any row height greater than the Row Template height.

注意:如果您有混合高行,FirstDisplayedScrollingRowIndex要成功,您需要确保行模板高度至少与最后一行高度一样好。这是一个相当大的问题,因为我发现无法将行高度设置为大于行模板高度。

#3


0  

I've been researching this problem and found that setting dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.count-1 works well as long as the AutoSizeRowsMode property is NOT set to DisplayedCells.

我一直在研究这个问题,发现只要AutoSizeRowsMo​​de属性未设置为DisplayedCells,设置dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.count-1就可以正常工作。

If you have problems with setting dataGridView1.FirstDisplayedScrollingRowIndex=dataGridView1.Rows.Count -1 and the grid not scrolling to bottom try setting the AutosizeRowsMode property to AllCells.

如果您在设置dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count -1时出现问题,并且网格未滚动到底部,请尝试将AutosizeRowsMo​​de属性设置为AllCells。