仅批准从asp gridview检查的行

时间:2016-06-11 05:13:30

标签: c# asp.net gridview checkbox

我有一个看起来像这样的网格视图,其中我有几行:名称,联系人#,公司名称等,我已经包含一个复选框,以便我可以选择要批准的行。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    CssClass="table table-hover table-striped" EnableViewState="False"
    onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="chkRow" runat="server" />
        </ItemTemplate>
    </asp:TemplateField>

           <asp:BoundField DataField="Username" HeaderText="Username" 
            SortExpression="Username" />

        <asp:BoundField DataField="LastName" HeaderText="LastName" 
            SortExpression="LastName" />

        <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
            SortExpression="FirstName" />

        <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
            SortExpression="CompanyName" />

        <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" 
            SortExpression="EmailAddress" />

        <asp:BoundField DataField="CompanyAddress" HeaderText="CompanyAddress" 
            SortExpression="CompanyAddress" />

        <asp:BoundField DataField="IncomeRange" HeaderText="IncomeRange" 
            SortExpression="IncomeRange" />

        <asp:BoundField DataField="CreditRequest" HeaderText="CreditRequest" 
            SortExpression="CreditRequest" />

        <asp:BoundField DataField="ContactNumber" HeaderText="ContactNumber" SortExpression="ContactNumber" />

        <%--<asp:TemplateField>
                <ItemTemplate>--%>


                <%--</ItemTemplate>
            </asp:TemplateField>--%>

           <asp:CheckBoxField />
           <asp:CheckBoxField />

    </Columns>

</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Approve" onclick="Button1_Click" />

我想要发生的是当我点击批准按钮时,唯一检查的行将执行后面的代码。

这是我的代码背后:

protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
        {
            scn.Open();
            SqlCommand cmd = new SqlCommand(@"UPDATE UserData SET CreditRequest = CAST(REPLACE(c.CreditRequest, ',', '') as int) FROM CreditRequests c INNER JOIN Userdata u on c.username=u.username Where c.Username=@Username", scn);
            cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = DSession["New"];

            cmd.ExecuteNonQuery();
        }
    }

UPDATE(我试过这个,它没有错误但是Approve功能没有执行)

protected void Approve(string Username)
    {
        using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
        {
            scn.Open();
            SqlCommand cmd = new SqlCommand(@"UPDATE UserData SET CreditRequest = CAST(REPLACE(c.CreditRequest, ',', '') as int) FROM CreditRequests c INNER JOIN Userdata u on c.username=u.username Where c.Username=@Username", scn);
            cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = Session["New"];

            cmd.ExecuteNonQuery();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow grow in GridView1.Rows)
        {
            //Searching CheckBox("chkDel") in an individual row of Grid  
            CheckBox chkdel = (CheckBox)grow.FindControl("chkDel");
            //If CheckBox is checked than delete the record with particular empid  
            if (chkdel.Checked)
            {
                string Username = grow.Cells[1].Text;
                Approve(Username);
            }
        }
        //Displaying the Data in GridView  
        bindgrid(); 
    }

2 个答案:

答案 0 :(得分:0)

DataKeyNames="ur Primary column"中添加Gridview HTML。 在后面的代码中,只需循环遍历GridView中的所有行,如下所示。

//Loop through all the rows in gridview
foreach (GridViewRow grv in grdEmp.Rows)
{
//Finiding checkbox control in gridview for particular row
CheckBox chk = (CheckBox)grv.FindControl("chkDel");

if (chk.Checked)
{
//get EmpId from DatakeyNames from gridview
int empid = Convert.ToInt32(grdEmp.DataKeys[grv.RowIndex].Value);

cmd = new SqlCommand("Update_Sp", con); //Put ur Inline query here
cmd.Parameters.Add("@Username", SqlDbType.Int).Value = Username; //use as per ur requirments
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
}
grdEmp.EditIndex = -1; 
BindEmpGrid();

答案 1 :(得分:0)

使用此代码:

foreach (GridViewRow row in GridView1.Rows)
{
 if (row.RowType == DataControlRowType.DataRow)
   {
    System.Web.UI.WebControls.CheckBox chkrow = (row.Cells[0].FindControl("chkrow") as System.Web.UI.WebControls.CheckBox);
    if (chkrow.Checked)
     {
      string Username = dgvshow.Rows[row.RowIndex].Cells[1].Text.ToString()
      Approve(Username);

     }
  }
}

并在加载网格的页面加载中使用!ispostback