如何在radgrid中查找所选行的列值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在radgrid中查找所选行的列值相关的知识,希望对你有一定的参考价值。

我正在使用RadGridrowcontextmenu。当我右键单击RadGrid行并选择“编辑选项”时,它将打开一个弹出窗口。

我需要的是当我右键单击Radgrid行时,我只想获得该特定选定行的3列值。例如,当我右键单击选定的Radgrid行时,我想获得所选行的Customer nameHeadend nameSite name值。

这是我的代码:

<MasterTableView GridLines = "None" DataKeyNames="orderId" CommandItemDisplay="Top"
                            EditMode="EditForms">
    <Columns>
        <telerik:GridClientSelectColumn UniqueName = "ClientSelectColumn" HeaderStyle-Width="3%"
            ItemStyle-Width="3%">
            <HeaderStyle Width = "3%" ></ HeaderStyle >
            <ItemStyle Width="3%"></ItemStyle>
        </telerik:GridClientSelectColumn>
        <telerik:GridBoundColumn UniqueName = "sId" HeaderText="sId" DataField="sId" ReadOnly="true"
            Visible="false">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn UniqueName = "orderId" HeaderText="orderId" Visible="false"
            ReadOnly="true" DataField="orderId">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn UniqueName = "Customer Name" HeaderText="Customer Name" DataField="Customer Name"
            ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn UniqueName = "Market Name" HeaderText="Market Name" DataField="Market Name"
            ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn UniqueName = "LOB" HeaderText="LOB" DataField="LOB" ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn UniqueName = "Headend Name" HeaderText="Headend Name" DataField="Headend Name"
            ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn UniqueName = "Project Name" HeaderText="Project Name" DataField="Project Name"
            ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridTemplateColumn UniqueName = "Site Name" HeaderText="Site Name" DataField="SiteName"
            ReadOnly="true">
            <ItemTemplate>
                <asp:LinkButton ID = "lnkSiteName" runat="server" Text='<%# Eval( "Site Name" ) %>'
                    ForeColor="Blue" CommandName="sitename"></asp:LinkButton>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridBoundColumn UniqueName = "Task Status" HeaderText="Task Status" DataField="Task Status"
            ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn UniqueName = "Clarify Account Nbr" HeaderText="Clarify Account Nbr"
            DataField="Clarify Account Nbr">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn UniqueName = "Site ID" HeaderText="Site ID" DataField="Site ID">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn UniqueName = "Quote ID" HeaderText="Quote ID" DataField="Quote ID">
        </telerik:GridBoundColumn>
        <telerik:GridCheckBoxColumn UniqueName = "EDP Created?" HeaderText="EDP Created?" DataField="EDP Created?">
        </telerik:GridCheckBoxColumn>
        <telerik:GridEditCommandColumn UniqueName = "EditCommandColumn" Display="false">
        </telerik:GridEditCommandColumn>
    </Columns>
</Mastertableview>
protected void RadMenu1_ItemClick(object sender, RadMenuEventArgs e)
{
    int radGridClickedRowIndex;

    radGridClickedRowIndex = Convert.ToInt32(Request.Form["radGridClickedRowIndex"]);

    switch (e.Item.Text)
    {
        case "Edit Task":
            string OrdID = rg200.MasterTableView.DataKeyValues[radGridClickedRowIndex]["orderId"].ToString();
            lblOrdHeadName.Text = rg200.MasterTableView.NamingContainer.FindControl("Headend Name").ToString();

            Session["orderId"] = OrdID;
            ifrmPopups.Style.Add("height", "409px");
            StringBuilder Script = new StringBuilder();
            Script.Append("document.getElementById('ctl00_PagePlaceholder_WinEditForm_C_ifrmPopups');openpopups()");
            RadAjaxManager1.ResponseScripts.Add(Script.ToString());
            rg200.Rebind();
            break;
    }
}

如果您看到上面的代码,我可以获得该选定列的orderId值。但是当我试图获得Headend Name值时,我收到了一个错误。如何获取所选行的Headend Name值?

答案

请尝试以下方法。它对我有用。

GridDataItem item =(GridDataItem)rg200.MasterTableView.Items[rg200.SelectedItems[0].ItemIndex];

lblOrdHeadName.Text = item["HeadendName"].Text;
另一答案

您可以使用以下代码循环选定的项目并将ID值存储到数组中,从而将其传递给方法。同时将网格的EnablePostBackOnRowClick属性设置为true

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)   
{   
    string[] array = new string[10];   
    int i = 0;   
    if (e.CommandName == "RowClick")   
    {   
        foreach (GridDataItem item in RadGrid1.SelectedItems)   
        {   
            array[i] = item.GetDataKeyValue("EmpID").ToString();  // storing to array   
            i++;   
        }   
    }   
}  
另一答案

尝试使用此代码从右键单击的行中获取Headend Name值:

lblOrdHeadName.Text = (rg200.MasterTableView.FindItemByKeyValue("orderId") as GridDataItem)["Headend Name"].Text;
另一答案

我找到了解决方案:

GridDataItem item = (GridDataItem)rg200.MasterTableView.Items[radGridClickedRowIndex];

lblOrdHeadName.Text = item["HeadendName"].Text;

以上是关于如何在radgrid中查找所选行的列值的主要内容,如果未能解决你的问题,请参考以下文章

如何读取表格视图中所选行的背景颜色?

如何在 UIPICKERVIEW (iOS) 中获取所选行的索引号

如何在 Swift 3 的 UIPickerView 中获取所选行的字符串值?

Python - 每列中所选行的标准偏差

iOS:在 didSelectRowAtIndexPath 方法中解析所选行的 url

React - 在表格行上触发点击事件并在另一个页面中显示所选行的详细信息