C#DataGridView直接更改Sql数据

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




实现效果:

实现效果

数据库表结构:(一定要设置主键)

数据库表结构

Form1.cs主要代码:

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;
using System.Configuration;
namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection conn;
        SqlDataAdapter sda;
        private void Form1_Load(object sender, EventArgs e)
        {
            string testDB = ConfigurationManager.ConnectionStrings["testDB"].ConnectionString;
            conn = new SqlConnection(testDB);
            sda = new SqlDataAdapter("select * from T_StuInfo",conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            for (int i=0;i<dataGridView1.ColumnCount;i++)
            {
                dataGridView1.Columns[i].Width = 84;
            }
        }
        private DataTable dbconn(string strSql)
        {
            conn.Open();
            sda = new SqlDataAdapter(strSql,conn);
            DataTable stSelect = new DataTable();
            int rnt = sda.Fill(stSelect);
            conn.Close();
            return stSelect;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (dbUpdate())
            {
                MessageBox.Show("修改成功");
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }
        private Boolean dbUpdate()
        {
            string strSql = "select * from T_StuInfo";
            DataTable stUpdate = new DataTable();
            stUpdate = this.dbconn(strSql);
            stUpdate.Rows.Clear();
            DataTable dtShow = new DataTable();
            dtShow = (DataTable)dataGridView1.DataSource;
            for (int i =0;i<dtShow.Rows.Count;i++)
            {
                stUpdate.ImportRow(dtShow.Rows[i]);
            }
            try
            {
                conn.Open();
                sda = new SqlDataAdapter("select * from T_StuInfo",conn);
                DataTable ds = new DataTable();
                SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(sda);
                sda.UpdateCommand = CommandBuilder.GetUpdateCommand();
                ds = (DataTable)dataGridView1.DataSource;
                sda.Update(ds);
                stUpdate.AcceptChanges();
                conn.Close();
            }
            catch
            {
                return false;
            }
            stUpdate.AcceptChanges();
            return true;
        }
    }
}

GitHub源码下载:https://github.com/kiritobin/DgvDirectFixSql

原文链接:https://www.idaobin.com/archives/1059.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

60 ÷ 10 =