1

チェックボックス(グリッドビュー内)にこの問題があり、ネットで検索して、この問題に対抗するために頭を割っていました..

私が今持っているのは、

(これは私のグリッドビューです)

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

初期値があります (例: $1000)。グリッドビュー内のチェックボックスをオンにするたびに、初期値からマイナスになります。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ループ内にあったため、2番目のチェックボックスがチェックされるたびに、forループ全体が再度実行され、1番目のチェックボックスの量が含まれると考えていました。とにかく私はこれを解決できますか?(同様に、3 番目のチェックボックスの場合、1 番目と 2 番目のチェックボックスに amt が含まれます)

または、forループを使用せずにチェックボックスをオンにして、グリッドビューの特定の行を取得できる方法はありますか?

4

2 に答える 2

0

これを試して、

foreach (GridViewRow row in GridView1.Rows)
{
     CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
     if (chk != null && chk.Checked)
     {
           // ...
     }
}
于 2013-08-03T18:37:53.493 に答える
0

INotifyPropertyChanged を実装するクラスのデータソースを使用しないのはなぜですか。すべての行と状態を反復処理する必要はありません。チェックボックスの状態が変わると、セッター「Checked」が起動されるため、そのセッター内の金額を差し引くことができます。

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; }
  }
于 2013-08-03T18:45:03.427 に答える