Winform 查询信息在dataGridView里显示再修改返回数据库如何实现

caicaicen 2010-10-11 03:57:15
VS2005 Winforms中使用DataGridView控件,添加bottom键查询信息,在DataGridView里显示,修改查询出来的信息,点击更新按钮后返回数据库,如何实现这个功能。
数据在DataGridView里直接显示(Form1_Load)可以更新,查询结果显示在DataGridView里,对查询结果无法更新。
各位能帮忙给个代码吗?
...全文
908 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
seneinei 2011-01-07
  • 打赏
  • 举报
回复
请问大侠怎么解决的 我也愈大这样的问题 能给个代码吗 我急着交作业 谢谢
caicaicen 2010-10-18
  • 打赏
  • 举报
回复
自己解决了,鼓捣了几天,--!
caicaicen 2010-10-17
  • 打赏
  • 举报
回复
这个问题大家都没遇到过吗?
caicaicen 2010-10-16
  • 打赏
  • 举报
回复
没用啊 --!
jshi123 2010-10-15
  • 打赏
  • 举报
回复
SqlDataAdapter da = new SqlDataAdapter(sql, con);
这句隐藏了类成员变量da,导致后面的new SqlCommandBuilder(da)得不到正确结果,改成:
da = new SqlDataAdapter(sql, con);
caicaicen 2010-10-13
  • 打赏
  • 举报
回复
一查询界面:datagridview绑定的是一个数据库的表,当我使用一查询按钮,把符合条件的结果在datagridview显示,对记录修改,保存后,后台数据库无法更新。应该怎么写啊
caicaicen 2010-10-13
  • 打赏
  • 举报
回复
我这样写无法更新数据,大侠们帮忙看看
public partial class ZHCX : Form
{
public ZHCX()
{
InitializeComponent();
}
static string strConn = "Data Source=.;Initial Catalog=puds;User ID=sa;password=";

SqlConnection con = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter();
DataTable DT = new DataTable();


private void button2_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
if (txtCondition.Text.Trim() == "")
{
MessageBox.Show("请输入查询条件");
return;
}
if (cbxConditionB.Text.Trim() == "")
{
MessageBox.Show("请选择查询条件");
return;
}

con.Open();
string Condition = txtCondition.Text.ToString();
string itype = cbxConditionB.SelectedItem.ToString();
string sql = "select * from Family ";
switch (itype)
{
case "姓名"://一级分类
sql += " Fname like '%" + Condition + "%'";
break;
}
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
con.Close();
}

private void button1_Click(object sender, EventArgs e)
{

try
{
SqlCommandBuilder SCB = new SqlCommandBuilder(da);
da.Update(DT);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功!");
}
}
caicaicen 2010-10-13
  • 打赏
  • 举报
回复
VS2005 Winforms
没有DataGridViewBind --!
liangzhl 2010-10-13
  • 打赏
  • 举报
回复
重新绑定一下就OK了
caicaicen 2010-10-13
  • 打赏
  • 举报
回复
DbConn conn = null;
DbConn 调用的是什么?
caicaicen 2010-10-13
  • 打赏
  • 举报
回复
谢了,我试试
zhang523012313 2010-10-13
  • 打赏
  • 举报
回复
LZ我给个完整的例子给你,我自己做的,可以更新,可以结贴给分啦
public partial class HmdFrom : Form
{
DbConn conn = null;
int strid; //记录黑名单信息行id
SqlDataAdapter Adapter; //初始化Adapter对象
DataSet ds = new DataSet(); //绑定协议单位信息DataSet
String mm = "";
public HmdFrom()
{
InitializeComponent();
}

private void HmdFrom_Load(object sender, EventArgs e)
{
button1.Enabled = false;
String sql = "select * from hmd where 1=1";
show(sql);
}

private void show(String sql)
{
try
{
conn = new DbConn();
ds.Clear();
Adapter = conn.DataGridViewBind(sql);
Adapter.Fill(ds);
DataTable dataTable = ds.Tables[0];

dataGridView1.DataSource = dataTable.DefaultView;

//设置dataGridView2不同的样式
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.SkyBlue;
this.dataGridView1.AllowUserToAddRows = true;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
strid = e.RowIndex;
}
//刷新datagridview1里的数据
private void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count != 0)
{
show(mm);
}
}
//保存黑名单里的数据
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
try
{
if (this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString() != "" && this.dataGridView1.SelectedRows[0].Cells[1].Value != null)
{
try
{
SqlCommandBuilder commBuilder = new SqlCommandBuilder();
commBuilder.DataAdapter = Adapter;
Adapter.Update(ds.Tables[0]);
MessageBox.Show("黑名单信息保存成功!");
button4_Click(sender, e);
button1_Click(sender, e);

}
catch (SqlException)
{
MessageBox.Show("id重复!");
}
}
else
{
MessageBox.Show("姓名必须填写");
}
}
catch (SqlException)
{
MessageBox.Show("添加错误!");
}
}

private void button3_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows[0].Cells[0].Value != null && dataGridView1.CurrentRow.Cells[0].Value.ToString() != "" && dataGridView1.CurrentRow.Cells[0].Value != null)
{
if (MessageBox.Show("确定删除吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
try
{
String Sql = "delete from hmd where id=" + dataGridView1.CurrentRow.Cells[0].Value;
dataGridView1.Rows.RemoveAt(strid);
int result = conn.ExecuteNonQuery(Sql);
if (result >= 1)
{
MessageBox.Show("删除成功!");
button4_Click(sender, e);
button1_Click(sender, e);
}
else
{
MessageBox.Show("删除失败!");
}
}
catch (Exception)
{
MessageBox.Show("请选中有效的行!");
}
}
}
}

private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells["zjlx"].Value = "身份证";
}

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show("请输入正确的数据格式!");
}
//查询,默认为空的查询条件是数据全部显示
private void button4_Click(object sender, EventArgs e)
{
getsql();
}

//查询内容的构造函数
public void getsql()
{
String strSql = "select * from hmd where 1=1";

String dm = textBox1.Text;
if (textBox1.Text == "" && textBox1.Text == null)
{
strSql += " and id=" + 1;
}
if (textBox1.Text != "" && textBox1.Text != null)
{
strSql += "and zjhm='" + dm + "'";
}
strSql += " order by id";
show(strSql);
mm = strSql;
}

}
swalp 2010-10-13
  • 打赏
  • 举报
回复
dataGridView 的事件用有CellEndEdit 方法,在这个方法中获取修改的单元格的值
value = dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
然后写修改数据库的方法。当你在查询的时候,重新绑定一下DataGridView,DataGridView1.DataSource = dt //DataTable对象
ctn171 2010-10-13
  • 打赏
  • 举报
回复
DataSet绑定到bindingsource,bindingsource绑定到DataGridView,然后就很方便更新了
caicaicen 2010-10-12
  • 打赏
  • 举报
回复
错了,没采纳,给个聪明点的方法
kj289907795 2010-10-11
  • 打赏
  • 举报
回复
给datagridview 添加一列 checkbox

选中的行即为更新行

根据选中行在数据库的ID

对数据表 对应行 进行更新

更新成功在重新绑定
即 this.dataGridView1.DataSource = ds.Tables[0];

这个方法比较笨,但相信LZ会采纳!
wuyq11 2010-10-11
  • 打赏
  • 举报
回复
DataSet ds = new DataSet();
SqlDataAdapter sda;

SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds);
this.dataGridView1.DataSource = ds.Tables[0];


kj289907795 2010-10-11
  • 打赏
  • 举报
回复
关注~学习~
lucky_yyx 2010-10-11
  • 打赏
  • 举报
回复
WinForm结构:
DataSet ds = null;
ds = //数据库中取出来的结果集
DataGridView.DataSource = ds.Tables[0];
Web结构:
DataSet ds = null;
ds = //数据库中取出来的结果集
DataGridView.DataSource = ds.Tables[0];
DataGridView.DataBind();
caicaicen 2010-10-11
  • 打赏
  • 举报
回复
怎么绑定?
加载更多回复(1)

110,499

社区成员

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

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

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