| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 517 人关注过本帖
标题:DGV修改保存问题!
只看楼主 加入收藏
ainiak110
Rank: 2
等 级:论坛游民
帖 子:16
专家分:20
注 册:2012-10-31
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
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)
        {
          
        }
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册


我现在在保存修改上难住了。求大大帮忙。。。。。 附上代码...
搜索更多相关主题的帖子: color 
2012-11-23 13:28
smart0721
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:106
专家分:468
注 册:2012-2-10
收藏
得分:20 
一个问题:
if (chkByName.Checked){this.byName();}
else if (chkByName.Checked && txtByName.Text == "")
 {
    MessageBox.Show("请输入书名", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    txtByName.Focus();
     return;
 }
你这样写第二种情况你永远执行不到:
可以这样写
if(chkByName.Checked)
{
 if (txtByName.Text == null||txtByName.Text.Trim()=="")
 {
    MessageBox.Show("请输入书名", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    txtByName.Focus();
     return;
 }
 this.byName();
}
至于修改,你是支持多行一起修改还是只允许一次修改一条数据?那些数据允许修改,那些数据不允许修改!(我想数据表主键如图书ID应该不可以改的)
我想应该先判断DGV里面哪些数据已被修改,并找到被修改数据的相应主键 对修改的数据通过主键查找Update到数据库!
2012-11-24 00:05
跳过去
Rank: 8Rank: 8
等 级:贵宾
威 望:20
帖 子:282
专家分:976
注 册:2012-8-13
收藏
得分:0 
我看到代码就表示 蛋疼了

光棍中.....
2012-11-24 17:52
快速回复:DGV修改保存问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028783 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved