怎么将datagridview中的被修改过的数据更新到数据库中,求c#代码,谢谢!! 20
3个回答
展开全部
namespace DategridviewToSQL
{
public partial class Form1 : Form
{
private DataTable DT = new DataTable();
private SqlDataAdapter SDA = new SqlDataAdapter();
private Boolean isUpdate = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
load();
}
private void load()
{
SqlConnection conn = new SqlConnection(@"server = (local)\SQL2005;Integrated Security = true;" + "DataBase = test1");
SqlCommand SCD = new SqlCommand("select * from aaa ", conn);
SDA.SelectCommand = SCD;
SDA.Fill(DT);
dataGridView1.DataSource = DT;
}
private void button1_Click(object sender, EventArgs e)
{
if (isUpdate)
{
try
{
SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
SDA.Update(DT);
isUpdate = false;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功! ");
}
else
{
MessageBox.Show("没有更新内容! ");
}
for (int i = 0; i < DT.Rows.Count; i++)
for (int j = 0; j < DT.Columns.Count; j++ )
{
dataGridView1[j, i].Style.BackColor = Color.White;
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
isUpdate = true;
dataGridView1[e.ColumnIndex,e.RowIndex].Style.BackColor = Color.Blue;
}
{
public partial class Form1 : Form
{
private DataTable DT = new DataTable();
private SqlDataAdapter SDA = new SqlDataAdapter();
private Boolean isUpdate = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
load();
}
private void load()
{
SqlConnection conn = new SqlConnection(@"server = (local)\SQL2005;Integrated Security = true;" + "DataBase = test1");
SqlCommand SCD = new SqlCommand("select * from aaa ", conn);
SDA.SelectCommand = SCD;
SDA.Fill(DT);
dataGridView1.DataSource = DT;
}
private void button1_Click(object sender, EventArgs e)
{
if (isUpdate)
{
try
{
SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
SDA.Update(DT);
isUpdate = false;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功! ");
}
else
{
MessageBox.Show("没有更新内容! ");
}
for (int i = 0; i < DT.Rows.Count; i++)
for (int j = 0; j < DT.Columns.Count; j++ )
{
dataGridView1[j, i].Style.BackColor = Color.White;
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
isUpdate = true;
dataGridView1[e.ColumnIndex,e.RowIndex].Style.BackColor = Color.Blue;
}
参考资料: http://zhidao.baidu.com/question/163380008.html?fr=qrl&cid=869&index=4
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DataTable dt = ds.Tables["cs"];
sda.FillSchema(dt, SchemaType.Mapped);
DataRow dr = dt.Rows.Find(textBox2.Text);
dr["staffnumber"] = textBox2.Text.Trim();
dr["name"] = textBox3.Text.Trim();
dr["password"] = textBox5.Text.Trim();
dr["sex"] = textBox4.Text.Trim();
dr["age"] = textBox7.Text.Trim();
dr["tel"] = textBox8.Text.Trim();
dr["IDnumber"] = textBox9.Text.Trim();
dr["major"] = textBox10.Text.Trim();
dr["address"] = textBox11.Text.Trim();
dr["position"] = textBox6.Text.Trim();
SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(sda);
sda.Update(dt);
就这样...一定可以的..这是我运行过的代码
sda.FillSchema(dt, SchemaType.Mapped);
DataRow dr = dt.Rows.Find(textBox2.Text);
dr["staffnumber"] = textBox2.Text.Trim();
dr["name"] = textBox3.Text.Trim();
dr["password"] = textBox5.Text.Trim();
dr["sex"] = textBox4.Text.Trim();
dr["age"] = textBox7.Text.Trim();
dr["tel"] = textBox8.Text.Trim();
dr["IDnumber"] = textBox9.Text.Trim();
dr["major"] = textBox10.Text.Trim();
dr["address"] = textBox11.Text.Trim();
dr["position"] = textBox6.Text.Trim();
SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(sda);
sda.Update(dt);
就这样...一定可以的..这是我运行过的代码
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Adapter.AcceptChanges()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询