简体   繁体   English

如何选择DataGridView并集中于添加到它的最后一行?

[英]How can I make the DataGridView select and focus on the last row I added to it?

dataGridView1.Rows.Add(txtItemId.Text, txtItemName.Text,txtQuantity.Text, txtPrice.Text);

The above code adds a new row with the values I entered in textboxes, but the DataGridView keeps selecting the first row even if it contains more than one row. 上面的代码用我在文本框中输入的值添加了新行,但是DataGridView仍然选择第一行,即使它包含多个行也是如此。 What I want is to make DataGridView select the last row I already added. 我想要使DataGridView选择我已经添加的最后一行。 How can I do this? 我怎样才能做到这一点?

After the add line set: 在添加行集之后:

dataGridView1.Rows[dataGridView1.RowCount-1].Selected = true;

**not tested but that should work :) **未经测试,但应该可以工作:)

Edit 编辑

I already tested and in order to get the result with no bugs, set this properties like this 我已经测试过,为了获得没有错误的结果,请像这样设置此属性

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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