dataGridView更新数据

dsfcj 2008-12-09 05:58:20
大家好! 我现在在dataGridView中修改、添加和删除数据,请问我要怎么做才能将更改的数据返回到数据库中?以下是部分代码。愿赐教!
string connstring = @"server=localhost;integrated security=true;database=ps";
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
DataSet ds = new DataSet();
try
{
string sql = @"select * from worker_table where worker_id='" + this.textBox1.Text.Trim() + "'";
SqlDataAdapter sdal = new SqlDataAdapter(sql, conn);
sdal.Fill(ds, "tb1");



BindingSource bs = new BindingSource();
bs.DataMember = "tb1";
bs.DataSource = ds;
this.dataGridView1.DataSource = bs;
}
...全文
683 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
随风v5 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dsfcj 的回复:]
public partial class 修改资料 : Form
    {  private DataTable DT = new DataTable();
        private SqlDataAdapter SDA = new SqlDataAdapter();

        public 修改资料()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
         
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("确定要保存嘛?", "确认保存", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {

              try
                    {
                        SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
                        SDA.Update(DT);
                        MessageBox.Show("更新成功!");
                    }
              catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;

                    }
            }

        }

        private void 修改资料_Load(object sender, EventArgs e)
        {

            string connstring = @"server=localhost;integrated security=true;database=ps";
            string sql = @"select * from worker_table";
            SqlConnection conn = new SqlConnection(connstring);
            SqlCommand cmd = new SqlCommand(sql, conn);

            SDA.SelectCommand = cmd;
            SDA.Fill(DT);
            dataGridView1.DataSource = DT;
        } 
         
    }
大家帮我看看这个错在什么地方?每次执行都会弹出一个错误报道框,谢谢!!
[/Quote]

没有啊。哪里错了??
surewy 2008-12-10
  • 打赏
  • 举报
回复
mark
535cheng110 2008-12-10
  • 打赏
  • 举报
回复
SDA.SelectCommand = cmd;
不能单独设置select,
把下面2行,换成1行
SqlCommand cmd = new SqlCommand(sql, conn);
SDA.SelectCommand = cmd;
↓↓↓↓
SDA = new SqlDataAdapter(sql, conn);

SelectCommand和UpdateCommand就都可以用了
535cheng110 2008-12-10
  • 打赏
  • 举报
回复
mark
sikezx 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xsnakek 的回复:]
让dataGridView和dateset绑定,然后在后台 用
DataRowVersion.Current
DataRowVersion.Original
来取出修改的值,
只需要拼写sql文就可以了
[/Quote]
flyjimi 2008-12-09
  • 打赏
  • 举报
回复
你的worker_table有没有主键?
dsfcj 2008-12-09
  • 打赏
  • 举报
回复
public partial class 修改资料 : Form
{ private DataTable DT = new DataTable();
private SqlDataAdapter SDA = new SqlDataAdapter();

public 修改资料()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{

if (MessageBox.Show("确定要保存嘛?", "确认保存", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{

try
{
SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
SDA.Update(DT);
MessageBox.Show("更新成功!");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;

}
}

}

private void 修改资料_Load(object sender, EventArgs e)
{

string connstring = @"server=localhost;integrated security=true;database=ps";
string sql = @"select * from worker_table";
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(sql, conn);

SDA.SelectCommand = cmd;
SDA.Fill(DT);
dataGridView1.DataSource = DT;
}

}
大家帮我看看这个错在什么地方?每次执行都会弹出一个错误报道框,谢谢!!
xsnakek 2008-12-09
  • 打赏
  • 举报
回复
让dataGridView和dateset绑定,然后在后台 用
DataRowVersion.Current
DataRowVersion.Original
来取出修改的值,
只需要拼写sql文就可以了
liningln0 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 grearo 的回复:]
定义:
CellEndEdit
UserAddedRow
UserDeletedRow
事件
[/Quote]

正确..

在这几个事件中加入你的数据库操作就可以了,从这几个事件传递的参数中可以得到改变的数据.
dsfcj 2008-12-09
  • 打赏
  • 举报
回复
不好 意思 可不可以讲的详细一点
我不是很理解你的意思
xsnakek 2008-12-09
  • 打赏
  • 举报
回复
dataGridView中自带修改纪录flg和新旧数据,你只需要把dataGridView传到后台,
就可以在后台判断操作了.
grearo 2008-12-09
  • 打赏
  • 举报
回复
定义:
CellEndEdit
UserAddedRow
UserDeletedRow
事件

110,547

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧