我现在用了三个关联的dataGrid,主datagrid的数据源是动态查询结果,SqlCommand CM=new SqlCommand("QU_inform_user",CN);
CM.CommandType=CommandType.StoredProcedure;
CM.Parameters.Add("@Phone",SqlDbType.VarChar,20);
CM.Parameters.Add("@bPhone",SqlDbType.VarChar,20);
CM.Parameters.Add("@Address",SqlDbType.VarChar,200);
CM.Parameters[0].Value="";
CM.Parameters[1].Value="";
CM.Parameters[2].Value="";
adp=new SqlDataAdapter(CM);
dsAll=new DataSet();
adp.Fill(dsAll,"qUser");
通过用户输入不同的值,显示不同的结果,请问每次查询后,我该如保更新datagrid显示内容,是不是每次都要完整的重新绑定一遍?

解决方案 »

  1.   

    不必
    绑定一次就可以了
    之后只要改变dataset的内容就会反映到datagrid
      

  2.   

    是winform
    怎么更新啊!请写出代码?
      

  3.   

    SqlCommand CM=new SqlCommand("QU_inform_user",CN);
    CM.CommandType=CommandType.StoredProcedure;
    CM.Parameters.Add("@Phone",SqlDbType.VarChar,20);
    CM.Parameters.Add("@bPhone",SqlDbType.VarChar,20);
    CM.Parameters.Add("@Address",SqlDbType.VarChar,200);
    CM.Parameters[0].Value=phone;//变量
    CM.Parameters[1].Value=bphone;;//变量
    CM.Parameters[2].Value=address;;//变量
    adp=new SqlDataAdapter(CM);
    dsAll=new DataSet();
    adp.Fill(dsAll,"qUser");

    adpBespeak=new SqlDataAdapter("select * from telRecord",CN);
    adpBespeak.Fill(dsAll,"Bespeak");

    string SqlProduct="select UserProduct.*,Model,ModelAlias from UserProduct ";
    SqlProduct+="inner join MachineModel on UserProduct.ModelID=MachineModel.ModelID ";
    adpMachine=new SqlDataAdapter(SqlProduct,CN);
    adpMachine.Fill(dsAll,"Product");

    adpRepair=new SqlDataAdapter("select * from Repair",CN);
    adpRepair.Fill(dsAll,"Repair");dtBespeak=dsAll.Tables["Bespeak"];
    dtMachine=dsAll.Tables["Product"];
    dtRepair=dsAll.Tables["Repair"];
    //style datagrid
    AddCustomerDataTableStyle();
    //AddBespeakDataTableStyle();
    AddProductDataTableStyle();
    AddRepairDataTableStyle();


    //set up PrimaryKeys
    dt.PrimaryKey=new DataColumn[]
    {dt.Columns["UserID"]}; dtBespeak.PrimaryKey=new DataColumn[]
    {dtBespeak.Columns["TableNum"]}; dtMachine.PrimaryKey=new DataColumn[]
    {dtMachine.Columns["MachineID"]}; dtRepair.PrimaryKey=new DataColumn[]
    {dtRepair.Columns["TableNum"]};
    //
    //set up relations
    dsAll.Relations.Add("dtMachine_dtBespeak",
    dtMachine.Columns["MachineID"],
    dtBespeak.Columns["MachineID"]);

    dsAll.Relations.Add("dtUser_dtMachine",
    dt.Columns["UserID"],
    dtMachine.Columns["UserID"]);

    dsAll.Relations.Add("dtBespeak_dtRepair",
    dtBespeak.Columns["TableNum"],
    dtRepair.Columns["TableNum"]);this.dgUserinform.DataSource=dsAll;
    this.dgUserinform.DataMember="qUser";
    CurrencyManager cm=(CurrencyManager)this.BindingContext[dgUserinform.DataSource,dgUserinform.DataMember];
    ((DataView)cm.List).AllowNew=false;
    ((DataView)cm.List).AllowDelete=false;
    ((DataView)cm.List).AllowEdit=false;

    this.dg_Bespeak.DataSource=null;
    this.dg_Bespeak.DataSource=dsAll;
    this.dg_Bespeak.DataMember="qUser.dtUser_dtMachine.dtMachine_dtBespeak";

    this.dgProduct.DataSource=null;
    this.dgProduct.DataSource=dsAll;
    this.dgProduct.DataMember="qUser.dtUser_dtMachine";

    this.dgRepair.DataSource=null;
    this.dgRepair.DataSource=dsAll;
    this.dgRepair.DataMember="qUser.dtUser_dtMachine.dtMachine_dtBespeak.dtBespeak_dtRepair"; CurrencyManager cmBespeak=(CurrencyManager)this.BindingContext[dg_Bespeak.DataSource,dg_Bespeak.DataMember];
    ((DataView)cmBespeak.List).AllowNew=false;
    ((DataView)cmBespeak.List).AllowDelete=false;
    ((DataView)cmBespeak.List).AllowEdit=false;
    CurrencyManager cmProduct=(CurrencyManager)this.BindingContext[dgProduct.DataSource,dgProduct.DataMember];
    ((DataView)cmProduct.List).AllowNew=false;
    ((DataView)cmProduct.List).AllowDelete=false;
    ((DataView)cmProduct.List).AllowEdit=false; CurrencyManager cmRepair=(CurrencyManager)this.BindingContext[dgRepair.DataSource,dgRepair.DataMember];
    ((DataView)cmRepair.List).AllowNew=false;
    ((DataView)cmRepair.List).AllowDelete=false;
    ((DataView)cmRepair.List).AllowEdit=false;当三个变量改变时,我该怎么写更新程序?
      

  4.   

    可以绑定到dataview,你的数据就可以及时更新了.