问题很简单。
我给datagridview1添加了一个checkbox列,为了简单起见datagridview1绑定数据时也只绑定了一个"金额"字段,我想达到的效果是,当用户在点击某行的checkbox时,能够即刻遍历一遍该datagridview1,统计出所有打了勾的行的总金额数(未打勾的不要统计),显示在Textbox1中。应该如何编写代码呢?特请教各位高手,答对者立马给分。谢谢!
我给datagridview1添加了一个checkbox列,为了简单起见datagridview1绑定数据时也只绑定了一个"金额"字段,我想达到的效果是,当用户在点击某行的checkbox时,能够即刻遍历一遍该datagridview1,统计出所有打了勾的行的总金额数(未打勾的不要统计),显示在Textbox1中。应该如何编写代码呢?特请教各位高手,答对者立马给分。谢谢!
解决方案 »
- 怎么把很多DataGridViewColumn类型用作泛型传入类型
- 从本地数据库的增删改怎么向网络过渡?
- C#如何获取C++传的指针
- 判断字符串变量str是否为空的一下三种方法哪个性能更优:a、str=="";b、str==String.Empty;c、str.Length==0;?请问原因是什么??
- SOS:如何将多个文本文件的内容进行二进制合并!!
- 时间格式的问题,多谢帮忙!
- 设置一个Label控件上文字的字体样式和字体大小随机的代码应该怎么写呀?
- 获取gridview某行的值
- calendar日期相减?
- 请问7张盘的那个东东那里有的下
- 用C#做windows服务的小问题
- C# 取历史记录 接口方式
事先写的checkbox的话,就用onclick事件阿,然后判断checkBox名.Checked,如果现在是true说明原来没选,总额减去该checkbox对应的值就可以了。
如果现在是false说明原来选中了,就要总额减去现在的值。
遍历就不需要了,除非你做全选之类的
事件中第一步先判断你点中的是不是checkbox,如果是的话,再统计统计出所有打了勾的行的总金额数
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns["CheckBoxColName"].Index)//CheckBoxColName是CheckBox列的名字
{
DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (Convert.ToBoolean(cell.EditedFormattedValue))//如果被选中
{
//遍历Datagridiew
}
}
}
但是如果考虑使用CellValueChanged事件,效果是不是会更好呢?
因为可能点击前面的某些单元格,但并未产生值变
{
DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (Convert.ToBoolean(cell.EditedFormattedValue))
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewCheckBoxCell cells = (DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells[e.ColumnIndex];
if (Convert.ToBoolean(cells.EditedFormattedValue))
{
//取值累加
}
}
}
}
<ItemTemplate>
<asp:CheckBox ID="chkRecord" runat="server" AutoPostBack="False" onclick="check(this);" />
</ItemTemplate>
<HeaderStyle BorderWidth="1px" Font-Size="9pt" HorizontalAlign="Center" /> </asp:TemplateField>
function check(source)
{
var checkedCount =document.getElementById("ctl00_ContentPlaceHolder2_Hidd").value;
if(source.checked){
checkedCount++;
}else{
checkedCount--;
}
document.getElementById("ctl00_ContentPlaceHolder2_Hidd").value = checkedCount;
} 以上是验证GridView中第一列中的CheckBox是否有被选中,希望能对你有所启发!
//将页面上的CheckBox添加事件chkRecord_CheckedChanged//后台 //首先要确保将原来选中的金额保存在隐藏字段中 protected void chkRecord_CheckedChanged(object sender, EventArgs e)
{
bool isChecked = false;
for (int i = 0; i < this.gvInfo.Rows.Count; i++)
{
GridViewRow row = gvInfo.Rows[i];
isChecked = ((CheckBox)row.FindControl("chkRecord")).Checked;
if (isChecked)
{
// 如果该行再被选中,将该行对应的金额值累加到隐藏字段中
//最后将隐藏字段值赋值给页面上的TextBox
}
}
}完毕,这样肯定能实现。但是有一个问题是,如果页面上显示的记录太多,页面刷新会很慢!总之前台和后台实现各有利弊吧!以上仅供参考!
{
//Column1是普通列
//Column2是checkbox列
if (e.ColumnIndex == Column2.Index)
{
int Amount = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[Column2.Index].FormattedValue.ToString() == "True")
{
Amount += Convert.ToInt32(row.Cells[Column1.Index].Value);
}
}
textBox1.Text = Amount.ToString();
}
}
但是运行很混乱 根本不是楼主想要的结果
private void UpdateStatusBar()
{
int MyCount = 0;
foreach (bool IsChecked in MyCheckState.Values)
{
if (IsChecked) MyCount++;
} toolStripStatusLabel1.Text = "当前选择的记录条数: " + MyCount.ToString(System.Globalization.CultureInfo.CurrentUICulture);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dataGridView1.Rows[e.RowIndex].Cells[0].Value = (bool)dataGridView1.Rows[e.RowIndex].Cells[0].EditedFormattedValue;
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex != -1)
{
int MyFirstColumn = (int)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
MyCheckState[MyFirstColumn] = (bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
this.UpdateStatusBar();
}
}
private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
if (e.ColumnIndex == 0)
{
int MyFirstColumn = (int)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
if (MyCheckState.ContainsKey(MyFirstColumn))
{
e.Value = MyCheckState[MyFirstColumn];
}
else
e.Value = false;
}
}
private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
{
if (e.ColumnIndex == 0)
{
int MyFirstColumn = (int)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
if (!MyCheckState.ContainsKey(MyFirstColumn))
{
MyCheckState.Add(MyFirstColumn, (bool)e.Value);
}
else
MyCheckState[MyFirstColumn] = (bool)e.Value;
}
} private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (dataGridView1.CurrentCellAddress.X != 0)
{
if (e.KeyCode == Keys.Space)
{
bool checkedValue = (bool)dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells[0].Value;
dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells[0].Value = !checkedValue;
}
}
}FORM_LOad()
{
MyCheckState = new Dictionary<int, bool>();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Column1是普通列
//Column2是checkbox列
if (e.ColumnIndex == Column2.Index)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString() == "True")
Amount = Amount + Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[Column1.Index].Value);
else
Amount = Amount - Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[Column1.Index].Value);
textBox1.Text = Amount.ToString(); }
}
{
decimal Cout = 0;
if (e.ColumnIndex == dgvDetail.Columns["Col_Check"].Index)
{
foreach (DataGridViewRow dgvr in dgvDetail.Rows)
{
if (Convert.ToBoolean(dgvr.Cells["Col_Check"].EditedFormattedValue) )
{
Cout +=Convert.ToDecimal(dgvr.Cells["Col_Submit"].Value) ;
}
}
}
this.lblCout.Text = Cout.ToString(); }
这是我的实现方法,已经实现了。点击一次就要进行所有列的循环。呵呵。目前只能想到这个,如果有最优的,希望大家粘贴出来啊。