C# 用于在gridview中检索循环复选框
我在gridview中的复选框有这个问题,我一直在网上搜索并绞尽脑汁来解决这个问题 我现在得到的是 这是我的网格视图C# 用于在gridview中检索循环复选框,c#,gridview,checkbox,C#,Gridview,Checkbox,我在gridview中的复选框有这个问题,我一直在网上搜索并绞尽脑汁来解决这个问题 我现在得到的是 这是我的网格视图 id amount($) 1 5 checkbox 2 13 checkbox 3 25 checkbox 我有一个初始值示例1000美元。每当我在gridview中选中复选框时,它都会从初始值减去。 如果我检查id=1,初始值将被扣除,并显示$995 如果我继续勾选另一个复选框,比如说id=3,那么现在
id amount($)
1 5 checkbox
2 13 checkbox
3 25 checkbox
我有一个初始值示例1000美元。每当我在gridview中选中复选框时,它都会从初始值减去。
如果我检查id=1,初始值将被扣除,并显示$995
如果我继续勾选另一个复选框,比如说id=3,那么现在我有两个复选框被勾选,金额现在将显示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循环,并且会包含第一个复选框的数量。我能解决这个问题吗?同样,对于第三个复选框,它将在第一个和第二个复选框中包含金额
或者是否有任何方法可以在选中复选框的情况下获得gridview的特定行而不使用for循环?试试这个
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
if (chk != null && chk.Checked)
{
// ...
}
}
为什么不使用实现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; }
}
我认为您可能对网格中每一行的每个复选框都使用了相同的名称Checkbox1,因此对于网格中的每一行,它都得到了Checkbox1,并按照您的代码进行操作。