使用行索引选择gridview中的行

时间:2015-09-28 10:44:15

标签: c# asp.net

  protected void select_click(object sender, GridViewCommandEventArgs e)
{
     try
    {
        DBLibrary db = new DBLibrary();
        int index = Convert.ToInt32(e.CommandArgument);
       * string FeeId = gridv1.Rows[index].Cells[1].Text;*


         if (e.CommandName == "Select")
        {


            string str = "SELECT AnuFeeMaster.FeeId ,AnuFeeMaster.StudentId, Tbl_Student.SName, AnuFeeMaster.Month, AnuFeeMaster.Year, AnuFeeMaster.FeeAmount, " +
                    " AnuFeeMaster.PaidAmount FROM  AnuFeeMaster INNER JOIN Tbl_Student ON AnuFeeMaster.StudentId = Tbl_Student.StudentId where ( AnuFeeMaster.FeeId ='" + FeeId + "')";
            SqlDataReader dr = db.ExecuteReader(str);

            while (dr.Read())
            {

             Session["name"] = dr["sname"].ToString();
             Session["id"] = dr["StudentId"].ToString();
             Session["mth"] = dr["Month"].ToString();
             Session["yr"] = dr["Year"].ToString();
             Session["tot"] = dr["FeeAmount"].ToString();


            }
        }
    }
    catch { }
}

以上是我的代码我曾经访问过的,我没有从dat获取r值数据请建议我,*标记我使用该节目我在哪里得到错误

2 个答案:

答案 0 :(得分:0)

您应该正确使用gridview的RowCommand事件。

void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Select")
{
  int index = Convert.ToInt32(e.CommandArgument);
  GridViewRow selectedRow = CustomersGridView.Rows[index];
}
}

您可以捕捉如下按钮点击事件:

protected void MyButtonClick(object sender, System.EventArgs e)
{
//Get the button that raised the event
Button btn = (Button)sender;

//Get the row that contains this button
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
} 

答案 1 :(得分:0)

创建正确的网格rowcommand事件 aspx代码

 <asp:GridView ID="Gv" runat="server" AllowPaging="true"   OnRowCommand="Gv_RowCommand" PageSize="10" EmptyDataText="No Records Found !">
    <Columns>
       <asp:TemplateField HeaderText="Action" ItemStyle-Width="20%">
            <ItemTemplate>
               <asp:LinkButton ID="lnkview" runat="server" CommandArgument='<%#Eval("Demo_Code") %>' CommandName="select" ></asp:LinkButton>
            </ItemTemplate>
       </asp:TemplateField>
       <asp:BoundField DataField="Coulmn1" HeaderText="Coulmn2" />
       <asp:BoundField DataField="Coulmn2" HeaderText="Coulmn2" />
    </Columns>
</asp:GridView>

aspx.cs代码

 protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e)
 {
        if (e.CommandName == "select")
        {

        }
}