DataGridView 非绑定连sql
填充,删除,添加,修改的程序例子有吗, 我搜了半天,没有全的。谢谢

解决方案 »

  1.   

    我这有份DataGridView操作合集,要的话可以发给你!留个方式
    要是不好你也别怪我!
      

  2.   

    http://www.cnblogs.com/gentlewolf/archive/2007/09/07/886053.html
      

  3.   

    填充直接用DataTable赋给他啊
    至于删除某行:if (this.DataGridView1.CurrentRow != null)
                {
                    if (!DataGridView1.Rows[this.DataGridView1.CurrentRow.Index].IsNewRow)
                    {
                        this.DataGridView1.Rows.Remove(DataGridView1.Rows[this.DataGridView1.CurrentRow.Index]);
                        BigMessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }            }添加和修改某行信息:
    直接在下列事件中处理就行了,保存时把DataGridView1里的信息保存在起来就好了private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {}
      

  4.   

    楼主去微软下官方的例子:
    http://windowsclient.net/downloads/folders/applications/default.aspx
      

  5.   

    OleDbConnection olecon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\zhengshuangliang\\全代码自移测试\\RADotNet\\Pages\\USM\\jingyou.mdb;");
                olecon.Open();
                string comm = "select * from [jingyou]";
                OleDbDataAdapter odda = new OleDbDataAdapter(comm, olecon);
                DataSet ds = new DataSet();
                odda.Fill(ds, "good");
                dataGridView1.DataSource = ds.Tables["good"];
                olecon.Close();
      

  6.   

    如果是sql数据库的话把所有以OleDb开头都换成Sql,再修改下连接字符串就可以了
    我的是access数据库
      

  7.   

    谢谢各位!已经实现了, 修改,删除,增加。  
    代码大概如下: 
    还有一个问题,其中 dataGridView1.CancelEdit(); 在修改错误的时候,他没有起作用。 有谁了解的吗?
    删除的功能放哪里好点,我是放在RowHeaderMouseDoubleClick的事件里面了,有什么比较好的解决方式吗,不想单独在外面放一个按钮。        string ConnectionString= "***********";
            string TbName = null;        DataSet Ds = null;
                SqlConnection Con = new SqlConnection(ConnectionString);
                SqlCommand Cmd = new SqlCommand();
                SqlDataAdapter Sda = new SqlDataAdapter();        private void GridViewBind()
            {
                try
                {
                    string str = "SELECT * FROM " + TbName ;
                    
                Ds = new DataSet();
                Sda.SelectCommand = new SqlCommand(str , Con);
                Sda.Fill(Ds, tbName);                dataGridView1.DataSource = Ds;//Ds.Tables[tbName]
                    dataGridView1.DataMember = tbName;
                    //dataGridView1.Columns[0].ReadOnly = true;            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                try
                {              SqlCommandBuilder SCB = new SqlCommandBuilder(Sda);
                  return Sda.Update(Ds, tbName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    dataGridView1.CancelEdit();
                }
            }        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (MessageBox.Show("\n确定要删除你选择的这行数据吗?\n", "删除行", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                                      SqlCommandBuilder SCB = new SqlCommandBuilder(Sda);
                                 return Sda.Update(Ds, tbName);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
      

  8.   

    上面删除那块 return  得去了。                              return Sda.Update(Ds, tbName);
     
      

  9.   

    其实就2句:
    SqlCommandBuilder SCB = new SqlCommandBuilder(Sda);
    Sda.Update(Ds, tbName);一开始一直出错,我还以为是语句的问题。
     
    “对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。”
    这个错误其实就是你的表没有设主键,设上就可以更新了。 ok散分。