无法在按钮单击时访问RadGrid的选定行值

时间:2014-04-02 19:16:56

标签: c# asp.net telerik telerik-grid radgrid

这是我的aspx页面:

<telerik:RadGrid runat="server" AutoGenerateColumns="False" ID="AutoPayRadGrid" AllowFilteringByColumn="False" AllowPaging="True" AllowSorting="False" ShowGroupPanel="False" PageSize="10"
OnNeedDataSource="AutoPay_NeedDataSource" AllowMultiRowSelection="True" >
    <ClientSettings EnableRowHoverStyle="true">
        <Selecting AllowRowSelect="True" />
        <ClientEvents OnRowMouseOver="RowMouseOver" />
    </ClientSettings>
    <SelectedItemStyle BackColor="gray" BorderColor="Purple" BorderStyle="Dashed" BorderWidth="1px" />
    <PagerStyle Mode="NextPrevAndNumeric" PageSizeControlType="None" Position="Bottom"></PagerStyle>
    <GroupingSettings GroupContinuesFormatString="" GroupContinuedFormatString=""></GroupingSettings>
    <MasterTableView TableLayout="Fixed">
        <Columns>
            <telerik:GridClientSelectColumn UniqueName="CheckboxColumn"/>
            <telerik:GridBoundColumn HeaderText="CustomeID" DataField="customerid" UniqueName="customerid"/>
            <telerik:GridBoundColumn HeaderText="Country" DataField="maincountry" UniqueName="maincountry"/>
            <telerik:GridNumericColumn HeaderText="Amount" DataField="Amount" UniqueName="Amount"/>
        </Columns>
    </MasterTableView>
    <ClientSettings AllowDragToGroup="true"></ClientSettings>
</telerik:RadGrid>
<telerik:RadToolTip ID="RadToolTip1" runat="server" ShowEvent="FromCode" AutoCloseDelay="0"/>
<br />

<asp:Button runat="server" ID="btnvendorbills" Text="Generate Vendor Bills" OnClick="btnvendorbills_Click" />

这是我背后的代码:

protected void btnvendorbills_Click(object sender, EventArgs e)
{
    foreach (GridItem item in AutoPayRadGrid.MasterTableView.Items)
    {
        CheckBox chk = (CheckBox)item["CheckboxColumn"].Controls[0];

        if (chk.Checked)
        {
            //code
        }
    }
}

但它在编译时给了我这个错误:

  

无法将带有[]的索引应用于类型为&#39; Telerik.Web.UI.GridItem&#39;的表达式。

在这一行

CheckBox chk = (CheckBox)item["CheckboxColumn"].Controls[0];

即使我尝试访问单元格值,如:

string country = item["maincountry"].Text;

它会抛出相同的错误。

1 个答案:

答案 0 :(得分:1)

 foreach (GridDataItem in AutoPayRadGrid.MasterTableView.Items)
        {
            CheckBox chk = (CheckBox)item["CheckboxColumn"].Controls[0];

            if (chk.Checked)
            {
                //code
            }
        }

而不是GridItem,它应该是GridDataItem。