简体   繁体   English

Gridview中所选项目的总数

[英]Total of Selected Items in Gridview

I have a Gridview 我有一个Gridview

在此输入图像描述

I have Click Button and two labels. 我有点击按钮和两个标签。 (Risk_label and MV_label) (Risk_label和MV_label)

Risk and MV column has price value. 风险和MV列具有价格价值。

My Checkbox column names are NameCheckBoxField1 and NameCheckBoxField2 我的Checkbox列名是NameCheckBoxField1NameCheckBoxField2

How can i calculate only "Which i selected in Gridview" Risk total and MV total in my labels? 我怎样才能只计算“我在Gridview中选择哪个”风险总额和MV总额?

Example; 例;

Risk (Checked rows) --> 10, 20, 30 ---> Risk_label = 60
MV (Checked rows) --> 20, 30, 40 ---> MV_label = 90

EDİT : I try this code; EDİT :我试试这个代码;

double sum = 0;
foreach (GridViewRow gvr in GridView1.Rows)
{
    CheckBox cb = (CheckBox)gvr.FindControl("NameCheckBoxField1");
    if (cb.Checked)
    {
        double amount = Convert.ToDouble(gvr.Cells[1].Text);
        sum += amount;
    }
}
Risk_label.Text = sum.ToString(); 

But i getting an error. 但我得到一个错误。

I think jQuery would be the best option for you. 我认为jQuery对你来说是最好的选择。 1. Add classes for Risk_Label and MV_Label 2. On checkbox click get the value of Risk_Label and MV_Label of the same row using the classes. 1.为Risk_Label和MV_Label添加类2.在复选框上单击使用类获取同一行的Risk_Label和MV_Label的值。 3. If it is checked then add value other wise subtract values. 3.如果已选中,则添加其他值减去值。

    $('.mygrid :checkbox').click(function () {

                            var amt = parseFloat($(this).parents('tr').find('.ClassRisk_label').text());
                            //One Fee Case
                            if (!isNaN(amt)) {
                                if ($(this).is(':checked')) {
                                    totalAmt = totalAmt + amt;
                                }
                                else {
                                    totalAmt = totalAmt - amt;
                                }
                            }
//Show the value...
});
protected void CalculateButton_Click(object sender, EventArgs e)
        {
            Int32 totalMVValue = 0, totalRiskValue = 0;

            // Iterate through the Products.Rows property
            foreach (GridViewRow row in GridView1.Rows)
            {
                // Access the CheckBox
                CheckBox mvSelectorCheckBox = (CheckBox)row.FindControl("MVSelector");
                if (mvSelectorCheckBox != null && mvSelectorCheckBox.Checked)
                {
                    // First, get the primaryId for the selected row
                    int mvValue=
                        Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                    totalMVValue += mvValue;
                }

                CheckBox riskSelectorCheckBox = (CheckBox)row.FindControl("RiskSelector");
                if (riskSelectorCheckBox != null && riskSelectorCheckBox.Checked)
                {
                    // First, get the primaryId for the selected row
                    int riskValue =
                        Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                    totalRiskValue += riskValue;
                }
            }           
        }

Looking at your last comment: 看看你的最后评论:

 <asp:templatefield headertext=""> <itemtemplate> <asp:CheckBox DataField="NameCheckBoxField1" Checked="True" runat="server"></asp:CheckBox> </itemtemplate> </asp:templatefield>

Is DataField assigned NameCheckBoxField1? DataField是否分配了NameCheckBoxField1? Or is it a typo? 或者这是一个错字? It should be ID="NameCheckBoxField1" 它应该是ID="NameCheckBoxField1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM