开发者

GridView - set selected index by searching through data keys

开发者 https://www.devze.com 2022-12-23 12:52 出处:网络
I have a GridView which has DataKey[0] as productId. If i have for instance productId = 54, is there any way to search through all

I have a GridView which has DataKey[0] as productId. If i have for instance productId = 54, is there any way to search through all GridView item and set as selected the 开发者_Python百科one who has DataKEy[0] = 54?

For a drop down list i have :

ddlProducts.Items.FindByValue(lblProduct.Text.ToString())).Selected = true

Is anything similar for GridView?

Thanks in advance.


You will probably need to do this in a foreach loop:

foreach(GridViewRow myRow in GridView1.Rows)
{
    if(GridView1.DataKeys[myRow.RowIndex].Equals("keyValue"))
    {
         GridView1.SelectedIndex = myRow.RowIndex;
         break;
    }
}

You won't get very good performance from this if you have a lot of rows, though.


Matt was very close. He forgot the "Value" property:

foreach(GridViewRow myRow in GridView1.Rows)
{
    if(GridView1.DataKeys[myRow.RowIndex].Value.Equals("keyValue"))
    {
        GridView1.SelectedIndex = myRow.RowIndex;
        break;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消