SuperGridControl绑定数据源,同时更新数据库

superGridControl绑定数据的方法  

 

 conn.Open();

            SqlConnection conn = new SqlConnection(SQLHELPER.connstr);

            SqlCommand com = conn.CreateCommand();

            com.CommandText = "select * from tb_visit t where pid=@id";

            com.Parameters.Add(new SqlParameter("@id", Conf.currentPatient.id));

            SqlDataAdapter da = new SqlDataAdapter(com);

            DataSet ds = new DataSet();

            da.Fill(ds);

            

 

            SqlCommandBuilder builder = new SqlCommandBuilder(da);

            da.Update(ds);

            //this.sgc_visit.PrimaryGrid.Rows.Clear();

            this.sgc_visit.PrimaryGrid.DataSource = ds.Tables[0];

 

            

            conn.Close();

 

superGridControl删除选定的某行

      private void Delete_Click(object sender, EventArgs e)

        {

            

            foreach (GridRow item in this.sgc_visit.PrimaryGrid.SelectedRows)

            {

                sgc_visit.PrimaryGrid.Rows.RemoveAt(item.Index);

                ds.Tables[0].Rows[item.Index].Delete();

                try

                {

                da.Update(ds.Tables[0]);//以数据集的表更新数据库

                    ds.Tables[0].AcceptChanges();//接受对数据的修改

                    MessageBox.Show("更新成功!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);//弹出提示更新成功

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message, "更新失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    //出现异常提示更新失败

                }

            }

        }

 

SuperGridControl中数据插入或更新到数据库

 void Update_Click(object sender, EventArgs e)

        {

            if (ds.HasChanges())//如果数据集因我们对datagridview的操作发生改变

            {

                try//捕获异常

                {

                    //DataTable dt = ds.Tables[0];

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                    {

 

                        ds.Tables[0].Rows[i]["pid"] = Conf.currentPatient.id;

                    }

 

                    da.Update(ds.Tables[0]);//以数据集的表更新数据库

                    ds.Tables[0].AcceptChanges();//接受对数据的修改

                    MessageBox.Show("更新成功!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);//弹出提示更新成功

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message, "更新失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    //出现异常提示更新失败

                }

            }

        }

猜你喜欢

转载自blog.csdn.net/sky_rabbit94/article/details/54983995