已有56人关注
DataGridView控件中修改数据问题
发表在C#图书答疑 2008-10-19
是否精华
版块置顶:
用的是《C#从入门到精通》
16.4直接在DataGridView控件中修改数据
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=MySchool;uid=sa;pwd=1985911");
            SqlDataAdapter sda = new SqlDataAdapter("select * from worker",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 worker";
            DataTable dtUpdate = new DataTable();
            dtUpdate = this.dbconn(strSql);
            dtUpdate.Rows.Clear();                    //清空dtUpdate表
            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;
        }
    }
}
在运行加载数据时没有问题
运行修改数据时抛出异常:对于不返回任何键列信息的SelectCommand,不支持UpdateCommand的动态SQL生成
分享到:
精彩评论 4
小科_mrkj
学分:43 LV2
2008-10-21
沙发
读者朋友:
    您好,首先触发DataGridView控件的CellClick事件,然后将Form1.cs中的代码文件替换成如下代码:
    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;
        int intindex = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection("server=.;database=db_16;uid=sa;pwd=");
            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)
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
            this.adapter = new SqlDataAdapter(strSql, conn);
            DataTable dtSelect = new DataTable();
            int rnt = this.adapter.Fill(dtSelect);
            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);
            DataTable dtShow = new DataTable();
            dtShow = (DataTable)this.dataGridView1.DataSource;
            dtUpdate.ImportRow(dtShow.Rows[intindex]);
            SqlCommandBuilder CommandBuiler;
            CommandBuiler = new SqlCommandBuilder(this.adapter);
            this.adapter.Update(dtUpdate);
            dtUpdate.AcceptChanges();
            return true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            intindex = e.RowIndex;
        }
    }
}
yeting
学分:0 LV1
2008-10-21
板凳
代码修改如下:
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;
        int intindex = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection("server=.;database=MySchool;uid=sa;pwd=1985911");
            SqlDataAdapter sda = new SqlDataAdapter("select * from worker", 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)
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
            this.adapter = new SqlDataAdapter(strSql, conn);
            DataTable dtSelect = new DataTable();
            int rnt = this.adapter.Fill(dtSelect);
            return dtSelect;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (dbUpdate())
            {
                MessageBox.Show("修改成功!");
            }
        }
        private Boolean dbUpdate()
        {
            string strSql = "select * from worker";
            DataTable dtUpdate = new DataTable();
            dtUpdate = this.dbconn(strSql);
            DataTable dtShow = new DataTable();
            dtShow = (DataTable)this.dataGridView1.DataSource;
            dtUpdate.ImportRow(dtShow.Rows[intindex]);
            SqlCommandBuilder CommandBuiler;
            CommandBuiler = new SqlCommandBuilder(this.adapter);
            this.adapter.Update(dtUpdate);
            dtUpdate.AcceptChanges();
            return true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            intindex = e.RowIndex;
        }
    }
}
在按下修改按钮时,this.adapter.Update(dtUpdate);这句话再次抛出异常对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。


请指教!!!
小科_mrkj
学分:43 LV2
2008-10-22
地板
读者朋友:
    您好,使用我给您回复的那段代码应该是好使的,我们原来的程序运行时,第一次修改好使,第二次出现的问题是“违反并发性……”,我修改之后(即我给您回复的那段代码),在我们这里是好使的。而且即使使用源程序,也没有出现你说的那个问题啊。你在
CommandBuiler = new SqlCommandBuilder(this.adapter);这条语句下面输出一下SqlCommandBuilder的更新语句,再详细看看。
zy315351965
学分:0 LV1
2008-12-10
4L
[FIELDSET][LEGEND]引自:3楼[/LEGEND]
读者朋友:
    您好,使用我给您回复的那段代码应该是好使的,我们原来的程序运行时,第一次修改好使,第二次出现的问题是“违反并发性……”,我修改之后(即我给您回复的那段代码),在我们这里是好使的。而且即使使用源程序,也没有出现你说的那个问题啊。你在
CommandBuiler = new SqlCommandBuilder(this.adapter);这条语句下面输出一下SqlCommandBuilder的更新语句,再详细看看。

[/FIELDSET]

回复:我以前也遇到这个问题,我的解决方法是在数据库中要放到数据集中的表添加主键,不知道是不是这个问题
  
首页上一页 1 下一页尾页 4 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照