1
<asp:GridView ID="gvWorkshop" runat="server" 
              AllowCustomPaging="True" AllowPaging="True"
              BorderStyle="Solid" BorderWidth="1px" 
              AllowSorting="True" AutoGenerateColumns="False"
              DataKeyNames="ID, ctrycode" OnRowCommand="gvWorkshop_onRowCommand">
        <Columns>
            <asp:ButtonField CommandName="Select" Text="Select" />
            <asp:BoundField DataField="paddress" HeaderText="Address" />
        </Columns>
 </asp:GridView>

代码背后

 protected void gvWorkshop_onRowCommand(object sender, GridViewCommandEventArgs e)
 {
   int index;
   string ID;
   GridViewRow row;
   if(e.CommandName == "Edit")
   {
      index = Convert.ToInt32(e.CommandArgument);
      row = gvWorkshop.Rows[index];
      ID = gvWorkshop.DataKeys[grdrow.RowIndex]["workshopID"].ToString();
   }
 }

我得到错误索引超出范围,因为 gridview 是空的。有人知道出了什么问题吗?为什么gridview变成空的。请帮忙。

4

1 回答 1

1

这一行的问题

ID = gvWorkshop.DataKeys[grdrow.RowIndex]["workshopID"].ToString();

您提到“grdrow”代替“row”。请改成

 ID = gvWorkshop.DataKeys[row.RowIndex]["workshopID"].ToString();
于 2013-08-15T09:25:16.703 回答