如何获取所选行的值

问题描述 投票:1回答:1

我试图从名为GridView的RadGrid中获取所选用户名,用户名是RadGrid上名为UserName的列。

我试过了:

GridDataItem item =(GridDataItem)GridView.MasterTableView.Items[GridView.SelectedItems[0].ItemIndex];
string lblOrdHeadName = item["UserName"].Text;

但这会在第一行引发错误:

'指数超出范围。必须是非负数且小于集合的大小。

有谁知道我能做些什么来使这项工作?

c# asp.net telerik radgrid
1个回答
0
投票

请尝试使用以下代码段。

    List<GridDataItem> Items = (from item in RadGrid1.MasterTableView.Items.Cast<GridDataItem>()
                                where item.Selected
                                select item).ToList();

    if (Items != null && Items.Count > 0)
    {
        foreach (GridDataItem item in Items)
        {
            string strUsername = item["UserName"].Text;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.