DataGridView无法滚动到新输入的行 [英] DataGridView can't scroll to newly entered row
问题描述
大家好!
所以我正在尝试,当我的数据网格视图中插入一行时,向下滚动到该行,以便该行在视图中。为了给你一些上下文,dgv使用usb条形码扫描仪输入行。由于表单是在平板电脑上,我们很快就会开始
用完空间,以便在插入dgv时显示所有行。我想要做的是让新插入的项始终在底部可见(AKA总是滚动到dgv的底部)。
So I am attempting to, when a row is inserted into my datagridview, scroll down to that row so that the row is in view. To give you some context, the dgv is having rows entered using a usb barcode scanner. Since the form is on a tablet, we quickly begin to run out of room to display all the rows on the dgv as they are inserted. What I am wanting to do is have the newly inserted item be always visible at the bottom (AKA always scroll to the bottom of the dgv).
我试图使用dgv这样做。 FirstDisplayedScrollingRowIndex,但它似乎不起作用。
I have attempted to do this using dgv.FirstDisplayedScrollingRowIndex, but it doesn't seem to work.
此外,这可能不会影响任何事情,但这一切都发生在模态形式背后。
Also, this may not affect anything, but this is all happening behind a modal form.
感谢您的帮助!
推荐答案
欢迎来到MSDN论坛。
Welcome to the MSDN forum.
您可以在插入的最后添加以下代码。
You can add following code at the last of insert.
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
简单演示供您参考。
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
dt.Rows.Add(new object[] { "e", "e", "e", "e", "e" });
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
}
DataTable dt;
private void Form2_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Columns.Add("col4");
dt.Columns.Add("col5");
dt.Rows.Add(new object[] { "a", "a", "a", "a", "a" });
dt.Rows.Add(new object[] { "b", "b", "b", "b", "b" });
dt.Rows.Add(new object[] { "c", "c", "c", "c", "c" });
dt.Rows.Add(new object[] { "d", "d", "d", "d", "d" });
dataGridView1.AllowUserToAddRows = false;
dataGridView1.DataSource = dt;
}
}
希望有所帮助,
最好的问候,
Bob
这篇关于DataGridView无法滚动到新输入的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!