简体   繁体   English

如何从GridView中获取选定的行值

[英]How to get selected row value from gridview

When clicked a button, I am trying to get the selected row value from a gridview and do something with that value but i am getting this error: 当单击一个按钮时,我试图从gridview中获取选定的行值,并对该值进行某些操作,但出现此错误:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

and the code is failing at this line: 并且代码在此行失败:

string category = gvDetail.Rows[rowIndex].Cells[0].Text;

here is my code: 这是我的代码:

 <asp:GridView ID="gvDetail" runat="server" CssClass="table table-hover table-bordered table-responsive" ForeColor="#333333" GridLines="None"
                CellPadding="4" PageSize="200" OnRowCommand="GridViews_RowCommand"
                AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" EnableViewState="False">
                <AlternatingRowStyle BackColor="#F3F3F3" />
                <Columns>

                  <asp:BoundField DataField="Category" HeaderText="Category" />
                  <asp:BoundField DataField="name" HeaderText="Name" />               
                  <asp:BoundField DataField="volume_paid" DataFormatString="{0:C0}" HeaderText="Spend" />

                  <asp:ButtonField HeaderText="Spend Metrics" ButtonType="Image" ImageUrl="~/img/mail.png" ControlStyle-Width="30px" ControlStyle-Height="30px" CommandName="Select"  />
                  <asp:ButtonField HeaderText="Request Call/Meeting" ButtonType="Image" ImageUrl="~/img/mail.png" ControlStyle-Width="30px" ControlStyle-Height="30px" CommandName="Select1"  />

                </Columns>
                <HeaderStyle CssClass="GridviewScrollHeader" BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <RowStyle CssClass="GridviewScrollItem" ForeColor="#000066" />
              </asp:GridView>

code behind: 后面的代码:

 protected void GridViews_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ToString());

      SqlDataAdapter da = new SqlDataAdapter(@"select min(month_key) as minKey, max(month_key) as maxKey from myTable", con);
      DataTable dtSETS = new DataTable();

      da.Fill(dtSETS);

      if (dtSETS.Rows.Count > 0)
      {
        DataRow dtSETS_row = dtSETS.Rows[0];

        int minKey = dtSETS_row.Field<int>("minKey");
        int maxKey = dtSETS_row.Field<int>("maxKey");


        if (e.CommandName == "Select")
        {
          ///Determine the RowIndex of the Row whose Button was clicked.
          int rowIndex = Convert.ToInt32(e.CommandArgument);
          string category = gvDetail.Rows[rowIndex].Cells[0].Text;
          string name = gvDetail.Rows[rowIndex].Cells[1].Text;         
          string volumepaid = gvDetail.Rows[rowIndex].Cells[2].Text;

         // do something...

        }

        if (e.CommandName == "Select1")
        {
          // do something...

        }


      }
    }

the issue was that i had the enableviewstate set to false, so what i had to do is to set it to true. 问题是我将enableviewstate设置为false,所以我要做的就是将其设置为true。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM