简体   繁体   English

在GridView中显示选定的行

[英]Show selected row in gridview

I have a data entry form with a RadGridView . 我有一个带有RadGridView的数据输入表单。 The data is indexed alphabetically in the main field of the row. 数据在该行的主字段中按字母顺序索引。 When I add a new row and press update , off course it goes to its place in the row order. 当我添加新行并按update ,它当然会转到行顺序中的位置。 But I don't only want that the new row will be selected, but i also want that my grid window will be shown. 但是我不仅希望选择新行,而且还希望显示网格窗口。 When I was using the default GridView in C# this code worked perfectly: 当我在C#中使用默认的GridView ,此代码运行良好:

private void btnUpdate_Click(object sender, EventArgs e)
{
    int id = 0;            
    id = Convert.ToInt32(lblId.Text);               
    foreach (var dgr in gridViewCompanies.Rows)
    {
        if (dgr.Cells[0].Value.Equals(tbl.ID))
        {
            dgr.IsSelected = true;
            dgr.Cells[0].IsSelected = true;
            gridViewCompanies.FirstRowIndex = dgr.Index;
            break;
        }
    }
 }

(I just copied this related part of the code) (我只是复制了这段代码的相关部分)

But now that I am using RadGridView (for some other reason) that last line gridViewCompanies.FirstRowIndex = dgr.Index; 但是现在我正在使用RadGridView (出于某些其他原因),最后一行gridViewCompanies.FirstRowIndex = dgr.Index; seems not to be working. 似乎没有用。 That means my new row is selected but the view won't show it. 这意味着我的新行选中,但视图不会显示它。 I have to scroll down to find the newly added row. 我必须向下滚动才能找到新添加的行。

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        int id = 0;            
        id = Convert.ToInt32(lblId.Text);               
        foreach (var dgr in gridViewCompanies.Rows)
        {
            if (dgr.Cells[0].Value.Equals(tbl.ID))
            {
                dgr.IsSelected = true;
                dgr.Cells[0].IsSelected = true;

                this.radGridView.TableElement.VScrollBar.Value = dgr.Index;
                break;
            }
        }
     }

Try this code 试试这个代码

This is what finally worked: 这是最终有效的方法:

private void btnUpdate_Click(object sender, EventArgs e)
    {
        int id = 0;            
        id = Convert.ToInt32(lblId.Text);               
        foreach (var dgr in gridViewCompanies.Rows)
        {
            if (dgr.Cells[0].Value.Equals(tbl.ID))
            {
                dgr.IsSelected = true;
                dgr.Cells[0].IsSelected = true;GridTableElement tableElement = this.gridViewCompanies.CurrentView as GridTableElement;
                        tableElement.ScrollToRow(dgr.Index);

                break;
            }
        }
     }

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

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