2013-08-03 25 views
1

我有這個問題的複選框(內部GridView控件),我一直在尋找的淨和開裂我的大腦來對付這個問題..for循環來檢索複選框中的GridView

什麼,我現在是,

(這是我的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循環,這就是爲什麼每次2號複選框檢查,它會再次運行整個for循環,這將包括第一複選框的量。無論如何,我能解決這個問題嗎? (同樣爲第三複選框,它將包括在第一和第二複選框AMT)

或者是有,我可以得到複選框GridView控件,而無需使用for循環檢查特定行的任何方法?

+0

我想你可能已經使用相同的名稱「Checkbox1」爲網格的每個行中的每個複選框,因此在網格中的每一行,它是越來越checkbox1,做按您的代碼。 – BVMR

回答

0

試試這個,

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

爲什麼不使用,所以你不要有通過所有行和狀態遍歷其實現INotifyPropertyChanged類的數據源。當複選框更改狀態時,Setter'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; } 
    }