如何取DataGridViewCheckBoxCell值

来源:互联网 发布:手机淘宝扫一扫 编辑:程序博客网 时间:2024/04/30 06:34

今到遇到这样的应用,主档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"));            }        }



0 0