for循环检索gridview中的复选框

时间:2013-08-03 18:28:37

标签: c# gridview checkbox

我的复选框(在网格视图中)有这个问题,我一直在网上搜索并破解我的大脑来解决这个问题。

我现在拥有的是,

(这是我的gridview)

id   amount($)   
1    5         checkbox 
2    13        checkbox
3    25        checkbox

我有一个初始值(例如$ 1000)。每当我检查gridview中的复选框时,它将从初始值减去。 如果我检查id = 1,初始值将被扣除并显示$ 995。

如果我继续检查另一个复选框,让我们说id = 3(现在我有2个复选框被选中),金额现在会显示$ 970。

目前,对于第一个选中的复选框,它运行良好,但后续复选框正在解决我的问题..而不是扣除25美元,它扣除30美元(25 + 5)

这是我的代码:

  protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
    foreach (GridViewRow row in GridView2.Rows)
    {
        CheckBox selectChk = (CheckBox)row.FindControl("CheckBox1");

        if (selectChk != null && selectChk.Checked)
        {
            String amount = ((Label)row.FindControl("amt")).Text;

            double amtSpend = Convert.ToDouble(usedSpace);

            double left = 100 - amtSpend;
            totalLbl.Text = left.ToString();


        }

我在想,因为它在for循环中,这就是为什么每次第二​​个复选框都被检查时,它会再次运行整个for循环,它将包含第一个复选框的数量。无论如何我能解决这个问题吗? (同样对于第3个复选框,它将包括第1个和第2个复选框中的amt)

或者是否有任何方法可以在不使用for循环的情况下选中复选框来获取gridview的特定行?

2 个答案:

答案 0 :(得分:0)

试试这个,

foreach (GridViewRow row in GridView1.Rows)
{
     CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
     if (chk != null && chk.Checked)
     {
           // ...
     }
}

答案 1 :(得分:0)

为什么不使用实现INotifyPropertyChanged的类的数据源,因此您不必遍历所有行和状态。当复选框更改状态时,setter'Checked'会被触发,因此您可以减去该setter中的数量。

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using WindowsFormsApplication9.Annotations;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            BindingList<Entity> entities = new BindingList<Entity>();
            entities.Add(new Entity {Amount = 10.0, Checked = false, Id = 1});
            entities.Add(new Entity {Amount = 900.0, Checked = false, Id = 2});
            entities.Add(new Entity {Amount = 850.0, Checked = false, Id = 3});

            this.dataGridView1.DataSource = entities;
        }
    }

    public class Entity : INotifyPropertyChanged
    {
        private bool _bChecked;
        private double _dAmount;
        private int _iId;

        public int Id
        {
            get { return this._iId; }
            set
            {
                if (value == this._iId)
                    return;

                this._iId = value;
                this.OnPropertyChanged();
            }
        }

        public double Amount
        {
            get { return this._dAmount; }
            set
            {
                if (value == this._dAmount)
                    return;

                this._dAmount = value;
                this.OnPropertyChanged();
            }
        }

        public bool Checked
        {
            get { return this._bChecked; }
            set
            {
                if (value == this._bChecked)
                    return;

                this._bChecked = value;
                this.OnPropertyChanged();
                // Change the value 10 to the value you want to subtract
                if (value) this.Amount = this._dAmount - 10;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}



[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  public sealed class NotifyPropertyChangedInvocatorAttribute : Attribute
  {
    public NotifyPropertyChangedInvocatorAttribute() { }
    public NotifyPropertyChangedInvocatorAttribute(string parameterName)
    {
      ParameterName = parameterName;
    }

    public string ParameterName { get; private set; }
  }
相关问题