如何在网格视图中选中复选框时添加金额



我有一个grid view

因为我有一个checkbox,当我check any of check box它会retrunamount,如果我click two or more checkbox it will return add both amount时检查假,则返回被检查然后返回该金额。

我试过了:

//foreach (GridViewRow row in BillPayment.Rows)
//{
//    CheckBox chk = (CheckBox)row.FindControl("Chkbill");
//    if (chk.Checked == true)
//    {
//        PaymentMode.Visible = true;
//        ButSubmit.Visible = true;
//        string s1 = BillPayment.Rows[0].Cells[3].Text;
//        txtamtt.Text = s1;
//    }
//    if (chk.Checked == false)
//    {
//        txtamtt.Text = "";
//    }
//}
//CheckBox chkTest = (CheckBox)sender;
//GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
int count = 0;
foreach (GridViewRow row in BillPayment.Rows)
{
//  CheckBox chk = (CheckBox)row.FindControl("Chkbill");
CheckBox chk = (row.Cells[0].FindControl("Chkbill") as CheckBox);
if (chk.Checked==true)
{
count++;
}
}
if (count >= 2)
{
//ddpayment.ClearSelection();
//chqgrid();
////decimal s1;
//PaymentMode.Visible = true;
//ButSubmit.Visible = true;
//con.Open();
//string strng = "select partymaster.name as Party,sum(amount) Amount from Purchase_Master" +
//             "rn inner join partymaster on partymaster.partyNo=Purchase_Master.partycode" +
//             "rn where Purchase_Master.partycode ='"+ddvendor.SelectedValue+"' and verify=1 and paid=0" +
//             "rn group by partymaster.name";
//SqlCommand cmdd = new SqlCommand(strng,con);
//    SqlDataAdapter da = new SqlDataAdapter(cmdd);
//    DataTable dt = new DataTable();
//    da.Fill(dt);
//    con.Close();
//    if (dt.Rows.Count != 0)
//    {
//        foreach (DataRow dr in dt.Rows)
//        {
//            txtamtt.Text=dt.Rows[0][1].ToString();
//        }
//    }
//else
//{
//    MessageInfo.MessageIcon = MessageIcons.ErrorIcon;
//    TMessageBox1.Show(this.Title, "No Record Found", (TMessageBox.MessageIcons)MessageInfo.MessageIcon, true);
//    return;
//}
if (count == 1)
{
ddpayment.ClearSelection();
chqgrid();
decimal s1;
PaymentMode.Visible = true;
ButSubmit.Visible = true;
foreach (GridViewRow row in BillPayment.Rows)
{
//  CheckBox chk = (CheckBox)row.FindControl("Chkbill");
CheckBox chk = (row.Cells[0].FindControl("Chkbill") as CheckBox);
if (chk.Checked == true)
{
s1 = Convert.ToDecimal(row.Cells[3].Text);
txtamtt.Text = s1.ToString();
}
}
}
if (count == 0)
{
txtamtt.Text = "";
PaymentMode.Visible = false;
Chequegrid.Visible = false;
ButSubmit.Visible = false;
}

在if(count>=2(中我遇到了问题,它将not return正确的值,请帮助我。

如果您的 (count==1( 已经返回正确的金额,那么您可以进行细微更改,以便像这样计算所有勾选的选择(请注意,我在完成计算后在循环外的文本框中显示结果

ddpayment.ClearSelection();
chqgrid();
decimal s1,temp;
PaymentMode.Visible = true;
ButSubmit.Visible = true;
temp=0;
foreach (GridViewRow row in BillPayment.Rows)
{
//  CheckBox chk = (CheckBox)row.FindControl("Chkbill");
CheckBox chk = (row.Cells[0].FindControl("Chkbill") as CheckBox);
if (chk.Checked == true)
{
s1 = Convert.ToDecimal(row.Cells[3].Text);
temp=temp+s1;
}
}
txtamtt.Text = temp.ToString();

最新更新