[英]Only approve the row that is checked from asp gridview
i have a gridview that looks like this wherein i have several rows : name, contact#, company name etc. and i have included a check box so i can choose what row to approve or not. 我有一个看起来像这样的gridview,其中我有几行:姓名,联系方式,公司名称等,并且我包括了一个复选框,因此我可以选择要批准或不批准的行。
<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" />
what i want to happen is when i click on approve button, the only row that is checked will perform the code behind. 我想发生的是,当我单击批准按钮时,选中的唯一行将执行后面的代码。
here is my code behind: 这是我的代码背后:
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 ( I tried this, it has no errors but the Approve function is not performing) 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();
}
Add DataKeyNames="ur Primary column"
In Gridview HTML
. 在
Gridview HTML
添加DataKeyNames="ur Primary column"
。 And in code behind just loop through all the rows in GridView like below. 在后面的代码中,只需遍历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();
use this code : 使用此代码:
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);
}
}
}
and also use !ispostback
in page load where you load your grid 并在加载网格的页面加载中使用
!ispostback
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.