VS2005   Winforms中使用DataGridView控件,添加bottom键查询信息,在DataGridView里显示,修改查询出来的信息,点击更新按钮后返回数据库,如何实现这个功能。
数据在DataGridView里直接显示(Form1_Load)可以更新,查询结果显示在DataGridView里,对查询结果无法更新。
各位能帮忙给个代码吗?

解决方案 »

  1.   

    WinForm结构:
    DataSet ds = null;
    ds = //数据库中取出来的结果集
    DataGridView.DataSource = ds.Tables[0];
    Web结构:
    DataSet ds = null;
    ds = //数据库中取出来的结果集
    DataGridView.DataSource = ds.Tables[0];
    DataGridView.DataBind();
      

  2.   

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

  3.   

    给datagridview 添加一列 checkbox 选中的行即为更新行根据选中行在数据库的ID对数据表 对应行 进行更新   更新成功在重新绑定   
    即 this.dataGridView1.DataSource = ds.Tables[0];这个方法比较笨,但相信LZ会采纳!
      

  4.   

    我这样写无法更新数据,大侠们帮忙看看  
    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("更新成功!");
            }
        }
      

  5.   

    DataSet绑定到bindingsource,bindingsource绑定到DataGridView,然后就很方便更新了
      

  6.   

     dataGridView 的事件用有CellEndEdit 方法,在这个方法中获取修改的单元格的值
    value = dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
    然后写修改数据库的方法。当你在查询的时候,重新绑定一下DataGridView,DataGridView1.DataSource = dt //DataTable对象
      

  7.   

    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;
            }
          
        }
      

  8.   

    DbConn conn = null;
    DbConn 调用的是什么?
      

  9.   

    VS2005 Winforms
    没有DataGridViewBind    --!
      

  10.   

    一查询界面:datagridview绑定的是一个数据库的表,当我使用一查询按钮,把符合条件的结果在datagridview显示,对记录修改,保存后,后台数据库无法更新。应该怎么写啊
      

  11.   

    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    这句隐藏了类成员变量da,导致后面的new SqlCommandBuilder(da)得不到正确结果,改成:
    da = new SqlDataAdapter(sql, con);