DGV修改保存问题!
程序代码:
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Configuration; namespace WindowsFormsApplication2 { public partial class Form4 : Form { protected SqlConnection con; protected SqlCommand cmd; protected SqlDataAdapter dr; protected DataSet ds; public Form4() { InitializeComponent(); } private void Form4_Load(object sender, EventArgs e) { try { con = new SqlConnection("server=.;database=BookStore;uid=sa;pwd=saa"); cmd = new SqlCommand(); cmd.Connection = con; con.Open(); this.InitData(); } catch (Exception exp) { MessageBox.Show("数据库无法访问:" + exp.Message); this.Close(); } finally { if (con != null && con.State != ConnectionState.Closed) con.Close(); } } private void Sea_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); if (!chkByName.Checked && !chkByPuslish.Checked && !chkByAuthor.Checked) { MessageBox.Show("至少输入一个查询条件","温馨提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); return; } if (chkByName.Checked) { this.byName(); } else if (chkByName.Checked && txtByName.Text == "") { MessageBox.Show("请输入书名", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtByName.Focus(); return; } if (chkByPuslish.Checked) { this.bypublish(); } else if (chkByPuslish.Checked && cmbPublish.Text == "") { MessageBox.Show("请输入出版社名", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); cmbPublish.Focus(); return; } if (chkByAuthor.Checked) { this.byAuthor(); } else if (chkByAuthor.Checked && txtAuthor.Text == "") { MessageBox.Show("请输入作者", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtAuthor.Focus(); return; } } //条件查询 private void Res_Click(object sender, EventArgs e) { chkByAuthor.Checked = false; chkByName.Checked = false; chkByPuslish.Checked = false; txtAuthor.Text = ""; txtByName.Text = ""; cmbPublish.Text = ""; cmbBookCode.Text = ""; DataTable dt = (DataTable)dataGridView1.DataSource; dt.Rows.Clear(); dataGridView1.DataSource = dt; } //重置 private void All_Click(object sender, EventArgs e) { dr = new SqlDataAdapter("select * from Book", con); ds = new DataSet(); dr.Fill(ds, "Book"); dataGridView1.DataSource = ds.Tables["Book"]; try { if (ds.HasChanges()) { SqlCommandBuilder cb = new SqlCommandBuilder(dr); dr.Update(ds); dataGridView1.Update(); MessageBox.Show("修改成功", "提示"); } } catch { MessageBox.Show("修改失败", "提示"); } } //所有 private void btnLocate_Click(object sender, EventArgs e) { int row = dataGridView1.Rows.Count;//得到总行数 int cell = dataGridView1.Rows[1].Cells.Count;//得到总列数 for (int i = 0; i < row; i++)//得到总行数并在之内循环 { for (int j = 0; j < cell; j++)//得到总列数并在之内循环 { if (cmbBookCode.Text== dataGridView1.Rows[i].Cells[j].Value.ToString()) { //对比TexBox中的值是否与dataGridView中的值相同(上面这句) this.dataGridView1.CurrentCell = this.dataGridView1[j, i];//定位到相同的单元格 return;//返回 } } } } //快速定位 private void InitData() { cmd = new SqlCommand("select distinct Press from Book order by Press", con); SqlDataReader reader = cmd.ExecuteReader(); cmbPublish.Items.Clear(); while (reader.Read()) { cmbPublish.Items.Add(reader[0]); } reader.Close(); = "select distinct Barcode from Book order by Barcode"; reader = cmd.ExecuteReader(); cmbBookCode.Items.Clear(); while (reader.Read()) { cmbBookCode.Items.Add(reader[0]); } reader.Close(); } private void byName() { ds = new DataSet(); dr = new SqlDataAdapter("select * from Book where Name like '%"+txtByName.Text+"%'",con); dr.Fill(ds, "Book"); dataGridView1.DataSource = ds.Tables["Book"]; }//按书名查询 private void bypublish() { ds = new DataSet(); dr = new SqlDataAdapter("select * from Book where Press like '%" + cmbPublish.Text + "%'", con); dr.Fill(ds, "Book"); dataGridView1.DataSource = ds.Tables["Book"]; }//按出版社查询 private void byAuthor() { ds = new DataSet(); dr = new SqlDataAdapter("select * from Book where Author like '%" + txtAuthor.Text + "%'", con); dr.Fill(ds, "Book"); dataGridView1.DataSource = ds.Tables["Book"]; }//按作者查询 private void button3_Click(object sender, EventArgs e) { if (MessageBox.Show("真的要退出系统吗??", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) { this.Close(); this.Dispose(); } else { } } private void button1_Click(object sender, EventArgs e) { } } }
我现在在保存修改上难住了。求大大帮忙。。。。。 附上代码...