DataGridView问题,在线等 我绑定了一个表到DataGridView中,我现在要更新这个表,结果DataGridView中的数据更新了,但是库中的并没有更新,这个问题怎么解决? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你确定有把DataGridView中的数据更新提交到数据库吗? 如sqlDataAapter1.Update(dataSet1); update table set...用这种语句更新了吗? 我没有做Update(dateset);这步!就这不明白,能加我Q给我讲讲吗?15886725 你确定有把DataGridView中的数据更新提交到数据库吗? 如sqlDataAapter1.Update(dataSet1); 是用GridView自动生成的更新按钮吗?先试着自己写代码吧!我也是菜鸟,但是我感觉这样子比较好!下边是我的一点更新的代码,希望能对你有用!更新是自己手动书写的代码!GridView显示的数据时直接SQLConnect连接到数据库的![code=C#]SqlConnection MyCon = new SqlConnection("Data Source=ZStone;Initial Catalog=Test;User ID=sa;Password=amazing"); MyCon.Open(); string strName = TextBox1.Text.ToString(); string strPassWord = TextBox2.Text.ToString(); SqlCommand theDelCommand = new SqlCommand("UPDATE [user] SET admin='"+strName+"',password='"+strPassWord+"'WHERE ID=1", MyCon); theDelCommand.ExecuteNonQuery(); MyCon.Close(); Response.Redirect("Default.aspx");[code]如果代码对你有用的话,我告诉你“当爱已成往事”是我师父!如果没有用的话!那我就不给我师父在这里丢人了! 是用GridView自动生成的更新按钮吗?先试着自己写代码吧!我也是菜鸟,但是我感觉这样子比较好!下边是我的一点更新的代码,希望能对你有用!更新是自己手动书写的代码!GridView显示的数据时直接SQLConnect连接到数据库的!SqlConnection MyCon = new SqlConnection("Data Source=ZStone;Initial Catalog=Test;User ID=sa;Password=amazing"); MyCon.Open(); string strName = TextBox1.Text.ToString(); string strPassWord = TextBox2.Text.ToString(); SqlCommand theDelCommand = new SqlCommand("UPDATE [user] SET admin='"+strName+"',password='"+strPassWord+"'WHERE ID=1", MyCon); theDelCommand.ExecuteNonQuery(); MyCon.Close(); Response.Redirect("Default.aspx");如果代码对你有用的话,我告诉你“当爱已成往事”是我师父!如果没有用的话!那我就不给我师父在这里丢人了! private DataTable DT = new DataTable();private OleDbDataAdapter SDA = new OleDbDataAdapter();...//初始OleDbCommand SCD = new OleDbCommand("select * table", conn);SDA.SelectCommand = SCD;SDA.Fill(DT);dataGridView1.DataSource = DT;...//保存OleDbCommandBuilder SCB = new OleDbCommandBuilder(SDA);SDA.Update(DT);MessageBox.Show("数据更新成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); //同步gridview 与datatablethis.BindingContext[dataTable].EndCurrentEdit();//提交更新的时候,更新数据库SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommandBuilder commandBuilder;commandBuilder = new SqlCommandBuilder(sqlDataAdapter );sqlDataAdapter .Update(dataTable); 果然在这里丢人,人家问的是WinForm里的DataGridView,你拿ASP.NET的GridView来做什么?DataGridView的更新不代表后台数据集的更新,数据集的更新不代表数据库的更新,当然也可以直接更新数据库而不更新数据集,但这些都需要自己写代码,不会像ASP.NET里面那样自动生成的。通过DataGridView的编辑相关事件来对数据集或者数据库操作,记得是自己写相关代码。 如果你想在GDV中的表改变后反应到数据库中 建议你用sqlDataAdapter适配器。当你操作完GDV中的数据后,你可以 用sqlDataAdapter .Update(dataTable); 这方法就将你修改后的信息反馈到数据库中 我是自己写的代码!但是我写的代码只更新了DataGridView,并没有更新库,麻烦帮解决下,附代码如下:当点击确定按钮时执行的代码int scount = Convert.ToInt32(this.updateCount.Text.Trim().ToString()); zcount = lcount + scount;//lcount是库中有的数量scount是增加的数量 String str = "update drugs set drugsCount="+zcount+" where drugsId="+lid; if (da.insertDrugs(str)) { dgvBean(); lcount = zcount; this.updateCount.Text = ""; } else { MessageBox.Show("添加失败!!!"); this.Close(); }这个是调用的类public Boolean changePass(string pass) { try { String sql = "update login set LoginPass='" + pass + "' where id = 1"; OleDbCommand cmd = new OleDbCommand(sql, oc); oc.Open(); int count = Convert.ToInt32(cmd.ExecuteNonQuery().ToString()); if (count > 0) { return true; } else { return false; } } catch (Exception ex) { return false; } finally { oc.Close(); } } 求助!Web.Config配置文件解密 使用remoting测试的时候出现信道“tcp”已注册的问题如何解决? 请教:C#取得 Excel CheckBox对象值 象163,sina,sohu,怎样防止群发邮件工具利用他们的邮箱群发的? 实现卡巴斯基鼠标移动窗体,窗体实时显示鼠标坐标,新手了~~ 一个图片处理的问题 在线急等!!!列表设置问题。急急急!!! C#Web中页面刷新的问题。 怎么都没有人回答我的问题呢?版主干什么去了? C# 用键盘实现panel的透明度的变化 都是什么类型可以直接的返回NUll 时间选择的问题
用这种语句更新了吗?
就这不明白,能加我Q给我讲讲吗?
15886725
MyCon.Open(); string strName = TextBox1.Text.ToString();
string strPassWord = TextBox2.Text.ToString();
SqlCommand theDelCommand = new SqlCommand("UPDATE [user] SET admin='"+strName+"',password='"+strPassWord+"'WHERE ID=1", MyCon);
theDelCommand.ExecuteNonQuery();
MyCon.Close();
Response.Redirect("Default.aspx");[code]如果代码对你有用的话,我告诉你“当爱已成往事”是我师父!如果没有用的话!那我就不给我师父在这里丢人了!
MyCon.Open(); string strName = TextBox1.Text.ToString();
string strPassWord = TextBox2.Text.ToString();
SqlCommand theDelCommand = new SqlCommand("UPDATE [user] SET admin='"+strName+"',password='"+strPassWord+"'WHERE ID=1", MyCon);
theDelCommand.ExecuteNonQuery();
MyCon.Close();
Response.Redirect("Default.aspx");如果代码对你有用的话,我告诉你“当爱已成往事”是我师父!如果没有用的话!那我就不给我师父在这里丢人了!
private OleDbDataAdapter SDA = new OleDbDataAdapter();
...
//初始
OleDbCommand SCD = new OleDbCommand("select * table", conn);
SDA.SelectCommand = SCD;
SDA.Fill(DT);
dataGridView1.DataSource = DT;
...
//保存
OleDbCommandBuilder SCB = new OleDbCommandBuilder(SDA);
SDA.Update(DT);
MessageBox.Show("数据更新成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.BindingContext[dataTable].EndCurrentEdit();//提交更新的时候,更新数据库
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommandBuilder commandBuilder;
commandBuilder = new SqlCommandBuilder(sqlDataAdapter );sqlDataAdapter .Update(dataTable);
当你操作完GDV中的数据后,你可以 用sqlDataAdapter .Update(dataTable); 这方法就将你修改后的信息反馈到数据库中
当点击确定按钮时执行的代码
int scount = Convert.ToInt32(this.updateCount.Text.Trim().ToString());
zcount = lcount + scount;//lcount是库中有的数量scount是增加的数量
String str = "update drugs set drugsCount="+zcount+" where drugsId="+lid;
if (da.insertDrugs(str))
{
dgvBean();
lcount = zcount;
this.updateCount.Text = "";
}
else
{
MessageBox.Show("添加失败!!!");
this.Close();
}
这个是调用的类
public Boolean changePass(string pass)
{
try
{
String sql = "update login set LoginPass='" + pass + "' where id = 1";
OleDbCommand cmd = new OleDbCommand(sql, oc);
oc.Open();
int count = Convert.ToInt32(cmd.ExecuteNonQuery().ToString());
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
finally
{
oc.Close();
} }