说明如何使用FirstDisplayedScrollingRowIndex [英] Clarification on how to use FirstDisplayedScrollingRowIndex

查看:1338
本文介绍了说明如何使用FirstDisplayedScrollingRowIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚发布,需要澄清财产。 (注意,我知道这个问题与其他人相似,因此我试图在其他地方寻找解决方案,但找不到确切的解决方案)。



我是相当新的编程,不知道如何使一个 DataGridView 滚动到用户选择的行。我尝试使用 FirstDisplayedScrollingRowIndex ,但出现以下错误:


错误:不能隐式将类型System.Windows.Forms.DataGridViewRow转换为int


当我尝试将所选行用户到datagridview的顶部:

  dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows [i] 

以下是完整的代码:

  String searchVal = textBox1.Text; 

for(int i = 0; i< dataGridView1.RowCount; i ++)
{
if(dataGridView1.Rows [i] .Cells [0] .Value!= null&&&&DataGridView1.Rows [i] .Cells [0] .Value.ToString()。Contains(searchVal))
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows [i];
dataGridView1.Update();
}
}


解决方案

到文档, FirstDisplayedScrollingRowIndex


获取或设置DataGridView上显示的第一行的索引。 >

您已经将索引存储在 i ...尝试使用: / p>

  dataGridView1.FirstDisplayedScrollingRowIndex = i; 






为了解决您在评论中的第二个问题,如何找到第一个完全匹配(如果有的话):

  var selectedRow = dataGridView1.Rows.Cast< DataGridViewRow>() 
.FirstOrDefault(x => Convert.ToString(x.Cells [0] .Value)== searchVal);

if(selectedRow!= null)
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows [i];
dataGridView1.Update();
}


Just posted before and need clarification on a property. (Note I know this question is similar to others and hence I tried looking elsewhere for the solution but couldn't find the exact one for this situation).

I'm rather new to programming and don't know how to make a DataGridView scroll to the row selected by a user. I tried using FirstDisplayedScrollingRowIndex but am getting the following error:

Error: Cannot implicitly convert type 'System.Windows.Forms.DataGridViewRow' to 'int'

Which occurs when I try to bring the selected row by the user to the top of the datagridview:

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[i]

Here is the full code:

String searchVal = textBox1.Text;

for (int i = 0; i < dataGridView1.RowCount; i++)
{
    if (dataGridView1.Rows[i].Cells[0].Value != null && dataGridView1.Rows[i].Cells[0].Value.ToString().Contains(searchVal))
    {
        dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[i];
        dataGridView1.Update();
    }
}

解决方案

According to the documentation, FirstDisplayedScrollingRowIndex:

Gets or sets the index of the row that is the first row displayed on the DataGridView.

You already have the index stored in i ... try just using that:

dataGridView1.FirstDisplayedScrollingRowIndex = i;


To address your second question in the comments, here's how you can find the first exact match (if any):

var selectedRow = dataGridView1.Rows.Cast<DataGridViewRow>()
                   .FirstOrDefault(x => Convert.ToString(x.Cells[0].Value) == searchVal);

if (selectedRow != null)
{
    dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[i];
    dataGridView1.Update();
}

这篇关于说明如何使用FirstDisplayedScrollingRowIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆