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

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

                }

            }

        }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值