标题:关于在DataGridView中直接修改数据的方法
只看楼主
beyondkgl
Rank: 1
来 自:广东广州
等 级:新手上路
帖 子:4
专家分:6
注 册:2012-3-7
结帖率:100%
已结贴  问题点数:20 回复次数:3 
关于在DataGridView中直接修改数据的方法
如题,我看了王小科,徐薇的那本C#从入门到精通,看不懂这个程序,求各位高手解释一下怎么个更新法的,谢谢了
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test03
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection conn;
        SqlDataAdapter adapter;
        private void button1_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection("server=.;database=db_16;uid=sa;pwd=123456");
            SqlDataAdapter sda = new SqlDataAdapter("select * from tb_emp",conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.RowHeadersVisible = false;
            for (int i = 0; i < dataGridView1.ColumnCount;i++ )
            {
                dataGridView1.Columns[i].Width = 84;
            }
            button1.Enabled = false;
            dataGridView1.Columns[0].ReadOnly = true;
        }
        private DataTable dbconn(string strSql)
        {
            conn.Open();
            this.adapter = new SqlDataAdapter(strSql, conn);
            DataTable dtSelect = new DataTable();
            int rnt = this.adapter.Fill(dtSelect);
            conn.Close();
            return dtSelect;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (dbUpdate())
            {
                MessageBox.Show("修改成功!");
            }
        }
        private Boolean dbUpdate()
        {
            string strSql = "select * from tb_emp";
            DataTable dtUpdate = new DataTable();
            dtUpdate = this.dbconn(strSql);
            dtUpdate.Rows.Clear();
            DataTable dtShow = new DataTable();
            dtShow = (DataTable)this.dataGridView1.DataSource;
            for (int i = 0; i < dtShow.Rows.Count; i++)
            {
                dtUpdate.ImportRow(dtShow.Rows[i]);
            }
            try
            {
                this.conn.Open();
                SqlCommandBuilder CommandBuiler;
                CommandBuiler = new SqlCommandBuilder(this.adapter);
                this.adapter.Update(dtUpdate);
                this.conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                return false;
            }
            dtUpdate.AcceptChanges();
            return true;
        }
    }
}
搜索更多相关主题的帖子: class 数据 private public 
2012-03-09 00:17
smart0721
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:106
专家分:468
注 册:2012-2-10
得分:15 
程序代码:
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test03
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection conn;
        SqlDataAdapter adapter;
        private void button1_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection("server=.;database=db_16;uid=sa;pwd=123456");//连接数据库
            SqlDataAdapter sda = new SqlDataAdapter("select * from tb_emp",conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];//绑定数据源到datagridView
            dataGridView1.RowHeadersVisible = false;//隐藏首列
            for (int i = 0; i < dataGridView1.ColumnCount;i++ )
            {
                dataGridView1.Columns[i].Width = 84;
            }
            button1.Enabled = false;
            dataGridView1.Columns[0].ReadOnly = true;
        }
        private DataTable dbconn(string strSql)//连接数据库,填充DataTable并返回       
      {
            conn.Open();
            this.adapter = new SqlDataAdapter(strSql, conn);
            DataTable dtSelect = new DataTable();
            int rnt = this.adapter.Fill(dtSelect);
            conn.Close();
            return dtSelect;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (dbUpdate())                       //调用private Bool dbUpdate()并判断

            {
                MessageBox.Show("修改成功!");
            }
        }
        private Boolean dbUpdate()
        {
            string strSql = "select * from tb_emp";
            DataTable dtUpdate = new DataTable();
            dtUpdate = this.dbconn(strSql);//调用private DataTable dbconn(string strSql)并得到dtUpdate空表
            dtUpdate.Rows.Clear();
            DataTable dtShow = new DataTable();
            dtShow = (DataTable)this.dataGridView1.DataSource;//把修改后的datagridview数据绑定到dtShow表中
            for (int i = 0; i < dtShow.Rows.Count; i++)
            {
                dtUpdate.ImportRow(dtShow.Rows[i]);//把dtShow表中的数据复制到dtUpdate空表中

            }
            try
            {
                this.conn.Open();                            //打开数据库并更新
                SqlCommandBuilder CommandBuiler;
                CommandBuiler = new SqlCommandBuilder(this.adapter);
                this.adapter.Update(dtUpdate);
                this.conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                return false;
            }
            dtUpdate.AcceptChanges();//更新数据库
            return true;
        }
    }
} 
2012-03-09 17:27
smart0721
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:106
专家分:468
注 册:2012-2-10
得分:5 
数据库中的table要设主键,不然修改不了数据,还有表中列值不能有约束(不能依赖另外一个表)
2012-03-10 10:50
bcc222
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2009-4-16
得分:0 
this.conn.Open();
                SqlCommandBuilder CommandBuiler;
                CommandBuiler = new SqlCommandBuilder(this.adapter);//最主要的就是这一句!
                this.adapter.Update(dtUpdate);
                this.conn.Close();
2012-03-15 10:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-362624-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.404747 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved