zoukankan      html  css  js  c++  java
  • 15.7DataGridView直接修改数据

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace _15._7DataGridView直接修改数据
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private SqlConnection getConnection() // 连接数据库
            {
                string constr = "Server=.;user=sa;pwd=zqyo850619;database=csharpzxw";
                SqlConnection mycon = new SqlConnection(constr);
                return mycon;
            }
    
            private void gdvBind() // 绑定数据
            {
                SqlConnection mycon = getConnection();
                try
                {
                    mycon.Open();
                    SqlDataAdapter sda = new SqlDataAdapter("select*from mytable001", mycon);
                    DataTable table = new DataTable();
                    sda.Fill(table);
                    this.dataGridView1.AutoGenerateColumns = true;
                    this.dataGridView1.DataSource = table;
                    this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
                }
                catch( Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    mycon.Close();
    
                }
            }
    
            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
              
    
            }
    
            private void Form1_Load(object sender, EventArgs e) //载入时执行数据绑定方法
            {
                gdvBind();
            }
    
            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                SqlConnection mycon = getConnection();
                try
                {
                    mycon.Open();
                    string mystr1=dataGridView1.Columns[e.ColumnIndex].HeaderText+"="+"'"+dataGridView1.CurrentCell.Value.ToString()+"'"; //定义获取数据字符串
                    string mystr2="id="+ dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
    
                    string updateSql = "update mytable001 set "+mystr1+" where "+mystr2;
                    SqlCommand mycmd = new SqlCommand(updateSql, mycon);
                    mycmd.ExecuteNonQuery();
                    gdvBind();
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    mycon.Close();
    
                }
            }
        }
    }
  • 相关阅读:
    python中取列表的后半部分元素
    python中列表分片
    python中统计列表中元素出现的次数
    python中range()函数用法
    pyhton中实现列表元素顺序颠倒
    python中列表元素求交集和并集
    python中编写抽奖小游戏
    python中删除列表元素
    python中列表的去重复和取重复
    欲为Java技术大牛所需的25个学习要点
  • 原文地址:https://www.cnblogs.com/zqyo2000z/p/5402398.html
Copyright © 2011-2022 走看看