C# WinForm DataGirlView 单元格编辑

看到上面的图,很明显就是让在DataGrilView中修改消费次数的,这就要求我们要在DataGrilView可以更新数据,那么怎么做到呢,其实很简单

首先、dataGrilView要启用编辑才可以

其次、看看下面的代码

  private void btnOk_Click(object sender, EventArgs e)
        {
            Pays objPay = new Pays();
            if (this.ValidateData())
            {
                bool fig = false;
                int count = Convert.ToInt32(dgvPayDetails.Rows.Count.ToString());

                objPay.PayID = this.txtCode.Text.Trim();
                objPay.MemberId = this.txtClient.Text.Trim();
                if (objPay.MemberId == "")
                {
                    objPay.MemberId = "0";
                }
                objPay.EmpID1 = int.Parse(this.cboEmp1.SelectedValue.ToString());
                objPay.EmpID2 = int.Parse(this.cboEmp2.SelectedValue.ToString());
                objPay.EmpID3 = int.Parse(this.cboEmp3.SelectedValue.ToString());
                objPay.Status = 1;
                objPay.Money = 0M;
                objPay.Remark = this.txtRemark.Text.Trim();
                objPay.PayDate = DateTime.Parse(this.dtpPayDate.Value.Date.ToShortDateString() + " " + DateTime.Now.ToLongTimeString());
                if (objPay.InsertCountPay() > 0)//新增消费单据
                {
                    for (int i = 0; i < count; i++)
                    {
                        //如果DataGridView是可编辑的,将数据提交,否则处于编辑状态的行无法取到
                        dgvPayDetails.EndEdit();
                        DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgvPayDetails.Rows[i].Cells["SelIndex"];
                        Boolean flag = Convert.ToBoolean(checkCell.Value);
                        if (flag == true)     //查找被选择的数据行
                        {
                            //从 DATAGRIDVIEW 中获取数据项
                            fig = true;
                            string z_zcode = dgvPayDetails.Rows[i].Cells[5].Value.ToString().Trim();
                            string mid = txtClient.Text;
                            string sid = dgvPayDetails.Rows[i].Cells[1].Value.ToString();
                            TradeCount model = new TradeCount().GetTradeCountByShopId(sid, mid);
                            model.memberId = mid;
                            model.userCount -= Convert.ToInt32(z_zcode);

                            model.UpdateTradeCount();
                            LoadTradeCount();
                        }
                    }
                    if (fig == false)
                    {
                        MessageBox.Show("请选择要消费的项目", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }


                DialogResult = DialogResult.OK;
                this.Close();

            }
          
        }

     

        private void dgvPayDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void dgvPayDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvPayDetails.EndEdit();
            DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgvPayDetails.Rows[e.RowIndex].Cells["SelIndex"];
            Boolean flag = Convert.ToBoolean(checkCell.Value);
            if (flag == true)//查找被选择的数据行
            {
                //从 DATAGRIDVIEW 中获取数据项

                dgvPayDetails.CurrentCell = dgvPayDetails.Rows[e.RowIndex].Cells[5];
                dgvPayDetails.BeginEdit(true);
          
            }

        }
View Code

注:
获得单元格的值 string z_zcode = dgvPayDetails.Rows[i].Cells[5].Value.ToString().Trim();

posted @ 2013-05-28 15:54  双魂人生  阅读(1782)  评论(0编辑  收藏  举报