zoukankan      html  css  js  c++  java
  • datagridview自动保存修改数据

    使用到的对象:
    1、	DataGridView: dataGridView1
    2、	BindingNavigator: bindingNavigator1(自带添加按钮btnAdd、删除按钮btnDelete)
    3、	ToolStripButton: btnCancelEdit(添加到bindingNavigator1之中)
    使用到的事件:
    /// <summary>
            /// 单元格的值改编后,执行更新、或插入操作;
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                //如果关键字段"type",说明是在编辑新行的其它字段的值,不需要做如何操作;
                string typeTemp = dataGridView1.Rows[e.RowIndex].Cells["type"].FormattedValue.ToString();
                if (typeTemp == null || typeTemp.Trim().Length == 0) return;
                //
                string sqlStr = "select count(*) from Coupler where type='"+
                    dataGridView1[0, e.RowIndex].FormattedValue.ToString() + "' ";
                if (ClsDataBaseOperator.execteCount(sqlStr) < 1)
                {
                    sqlStr = "insert into Coupler(type) values('" +
                        dataGridView1[0, e.RowIndex].FormattedValue.ToString() + "')";
                    ClsDataBaseOperator.executeGetLines(sqlStr);
                }
                else
                {
                    sqlStr = "update Coupler set type='" + dataGridView1[0, e.RowIndex].FormattedValue.ToString() +
                    "', PHS1900=" + dataGridView1.Rows[e.RowIndex].Cells["PHS1900"].FormattedValue.ToString() +
                    ", DCS1800=" + dataGridView1.Rows[e.RowIndex].Cells["DCS1800"].FormattedValue.ToString() +
                    ", GSM900=" + dataGridView1.Rows[e.RowIndex].Cells["GSM900"].FormattedValue.ToString() +
                    ", CDMA800=" + dataGridView1.Rows[e.RowIndex].Cells["CDMA800"].FormattedValue.ToString() +
                    ", other=" + dataGridView1.Rows[e.RowIndex].Cells["other"].FormattedValue.ToString() +
                    ", IsDefault=" + dataGridView1.Rows[e.RowIndex].Cells["IsDefault"].FormattedValue.ToString() + 
                    " where type='" + dataGridView1[0, e.RowIndex].FormattedValue.ToString() + "'";
                    ClsDataBaseOperator.executeGetLines(sqlStr);
                }
            }
            /// <summary>
            /// 当有单元格进入编辑状态时,需要打开“撤销编辑”按钮的可点击状态;
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
            {
                this.btnCancelEdit.Enabled = true;
            }
            /// <summary>
            /// 删除一条记录,删除表格的的当前行,并更新数据库;
            /// btnDelete为navigator自带的按钮,需要添加下面事件;
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnDelete_Click(object sender, EventArgs e)
            {
                dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                string sqlStr = "delete from Coupler where type='" +
                    dataGridView1.CurrentRow.Cells["type"].FormattedValue.ToString() + "' ";
            }
            /// <summary>
            /// 撤销修改
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnEdit_Click(object sender, EventArgs e)
            {
                dataGridView1.CancelEdit();
                dataGridView1.EndEdit();
            }
            /// <summary>
            /// 添加新的一行;
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnAdd_Click(object sender, EventArgs e)
            {
                dataGridView1.CurrentRow.Cells["PHS1900"].Value = "0";
                dataGridView1.CurrentRow.Cells["DCS1800"].Value = "0";
                dataGridView1.CurrentRow.Cells["GSM900"].Value = "0";
                dataGridView1.CurrentRow.Cells["CDMA800"].Value = "0";
                dataGridView1.CurrentRow.Cells["other"].Value = "0";
                dataGridView1.CurrentRow.Cells["type"].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.CurrentRow.Cells["type"];
            }
    

  • 相关阅读:
    蒟蒻Orion还要学的东西!
    一些技巧 && 常数优化 && 出现の错误 [绝赞更新中!]
    [US Open 2004][luogu2342] 叠积木 [带权并查集]
    省选算法学习·一些数列相关的数学知识 [数学]
    [BJOI2019] 奥术神杖 [取log+AC自动机+dp]
    [BJOI2019] 删数 [dp转贪心结论+线段树]
    [2018国家集训队][UOJ449] 喂鸽子 [dp+组合数学]
    [2018集训队作业][UOJ424] count [笛卡尔树+括号序列+折线法+组合数学]
    [2018集训队作业][UOJ450] 复读机 [DP+泰勒展开+单位根反演]
    [SDOI2014][BZOJ3533] 向量集 [线段树+凸包]
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/2156510.html
Copyright © 2011-2022 走看看