c# - c#在DataGridview中显示选定的行
问题描述
我的 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);
解决方案
要滚动到选定的行,请使用:
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);
}
}
推荐阅读
- django - 为包含对象的 django 模型提供服务
- reactjs - 我如何使用 Promise 对象
- python - 把我的for循环变成python中的一个函数
- c# - Viewbag 没有将回发的值绑定到控制器?
- spring-boot - 如何在 Spring Boot 应用程序中为嵌入式 tomcat 启用 jmx
- python - 刷新的对象是从数据库事务缓冲区返回还是从 SQLAlchemy 中的内存表示返回?
- php - Laravel 应用程序的配置何时初始化?或者当 DB::connection 设置时?
- linux - 预期值:CLOSED,实际值:“CLOSED”
- r - 传递向量变量时 sum 函数不起作用
- vtt - 如何添加一种以上的字幕语言?