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"];
            }
    

  • 相关阅读:
    学号20145220《信息安全系统设计基础》第10周学习总结
    补发周四的博客。对之前的知识复习了一遍,因为要闭卷考试。
    jsp中文乱码
    redis做成windows服务
    poi导出excel打开文件时部分内容有问题
    mysql删除数据表时:Cannot delete or update a parent row: a foreign key constraint fails
    Oracle使用foreach批量操作数据
    Oracle数据库clob类型转String类型
    MAVEN配置多个仓库
    idea中maven项目明明有jar包还是爆红
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/2156510.html
Copyright © 2011-2022 走看看