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

时间:2018-05-03 04:49:39

标签: asp.net sql-server

我有grid view

因为我checkbox时我check any of check box retrun amount,如果我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" +
            //             "\r\n inner join partymaster on partymaster.partyNo=Purchase_Master.partycode" +
            //             "\r\n where Purchase_Master.partycode ='"+ddvendor.SelectedValue+"' and verify=1 and paid=0" +
            //             "\r\n 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正确值,请帮助我。

2 个答案:

答案 0 :(得分:2)

如果你的(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();

答案 1 :(得分:2)

首先clear if(count>=2)然后尝试这个:

            Decimal Amount=0;
            foreach (GridViewRow row in BillPayment.Rows)
            {
                CheckBox chk = (row.Cells[0].FindControl("Chkbill") as CheckBox);
                if (chk.Checked == true)
                {   
                    decimal Rate = Convert.ToDecimal(row.Cells[3].Text);
                    Decimal Total = Rate;
                    Amount = Amount + Total;
                    txtamtt.Text = Amount.ToString();
                }
            }

尝试avoid更多comment lines,并尝试reduce your coding size

希望这会对你有所帮助:)。

详细了解foreach link