当前位置:首页 > Windows程序 > 正文

如何取DataGridViewCheckBoxCell值

2021-03-29 Windows程序

标签:datagridviewcheckbox

今到遇到这样的应用,主档datagridview要通过点选Checkbox列,实时计算Checkbox=true的所有明细金额合计,

技术分享


网上有提到使用EditedFormattedValue和FormattedValue的值的判断,但使用起来还是麻烦,,要做判断,于是想到用点选后马上结束编辑状态,结果还变好用的,上代码,以备后用:

/// <summary> /// 选择内容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { //取当前datagridview DataGridView dgv = (DataGridView)sender; //这句很关键,结束编辑状态 dgv.EndEdit(); //选择列判断 if (dgv.Columns[e.ColumnIndex].DataPropertyName.ToLower() == "selcol") { string idlist = "", pk = "id"; DataGridViewCheckBoxCell dgchk=null; foreach (DataGridViewRow item in dgv.Rows) { dgchk = (DataGridViewCheckBoxCell)item.Cells["selcol"]; //判断选择列是否已被选 if ((bool)item.Cells["selcol"].Value) { idlist += idlist == "" ? item.Cells[pk].Value.ToString() : "," + item.Cells[pk].Value.ToString(); } } //计算选择的明细金额 float selprice = Util.ShowVoiceMoney(idlist,pk, "iMoney", "PU_arrivalVouchs"); lb3.Text = string.Format("已选金额:{0}", selprice.ToString("F2")); lb_rate.Text = string.Format("已选金额比例:{0}%", AllPrice == 0 ? "0.00" : (selprice / AllPrice * 100).ToString("F2")); } }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69881.html