开发者

GridView - set selected index by searching through data keys

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜