数据源为access,两个(或两个以上)表join出一个dataset表绑到一个datagrid上,用户双击其中一条记录的时候弹出一新窗口,在这窗口里可对这条记录进行修改或是删除(删除还差只是修改问题比较严重),mssql里用视图可以修改,但在access里行不行还不清楚,不是懒,现在是怎么用c#调access里的视图还不清楚,也没找到相关资料。不知道这路子行不。
哪位老大搞过这winform的东东,指点一下呀。有相关代码哪更好了。
 

解决方案 »

  1.   

    修改还是通过操作DataTable,然后提交的数据库,至于数据库是哪一种,在sql语句和DataAdapter那一层会有所区别,下面是以前写的代码,你看看,
    //在另一个窗口中修改当前选定的DataGrid的行using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    //主窗体代码
    namespace Zhzuo
    {
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.DataGrid dataGrid1;
    private DataSet ds;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    InitializeComponent();
    ds = new DataSet("MyDataSet");
    InitData(ds);
    this.dataGrid1.DataSource = this.ds;
    this.dataGrid1.DataMember = this.ds.Tables[0].TableName;
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.button1 = new System.Windows.Forms.Button();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(16, 20);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(340, 160);
    this.dataGrid1.TabIndex = 0;
    this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick);
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(264, 196);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "修改";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(372, 229);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.dataGrid1);
    this.Name = "Form1";
    this.Text = "Form1";
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false); }
    #endregion private void InitData(DataSet ds)
    {
    DataTable dt = new DataTable("TabeHost");
    DataColumn dc = new DataColumn("IpAddress",typeof(string));
    dt.Columns.Add(dc);
    dc = new DataColumn("UserName",typeof(string));
    dt.Columns.Add(dc);
    dc = new DataColumn("Password",typeof(string));
    dt.Columns.Add(dc);
    ds.Tables.Add(dt);
        
    DataRow dr = dt.NewRow();
    dr["IpAddress"] = "192.192.132.229";
    dr["UserName"] = "zhzuo";
    dr["Password"] = "zhengzuo";
    dt.Rows.Add(dr); dr = dt.NewRow();
    dr["IpAddress"] = "192.192.132.230";
    dr["UserName"] = "11";
    dr["Password"] = "12";
    dt.Rows.Add(dr); dr = dt.NewRow();
    dr["IpAddress"] = "192.192.132.231";
    dr["UserName"] = "123";
    dr["Password"] = "12";
    dt.Rows.Add(dr); dr = dt.NewRow();
    dr["IpAddress"] = "192.192.132.232";
    dr["UserName"] = "22";
    dr["Password"] = "788";
    dt.Rows.Add(dr);
    } private void button1_Click(object sender, System.EventArgs e)
    {
    DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current;
    Form2 form2 = new Form2(drv);
    form2.ShowDialog(); }
    private void dataGrid1_DoubleClick(object sender, System.EventArgs e)
    {
    button1_Click(button1,EventArgs.Empty);
    }
    }
    }
    //子窗体代码
    =======================
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace Zhzuo
    {
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private DataRowView drv;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form2(DataRowView dr)
    {
    InitializeComponent();
    this.drv = dr;
    this.textBox1.Text = (string)drv["IpAddress"];
    this.textBox2.Text = (string)drv["UserName"];
    this.textBox3.Text = (string)drv["Password"];
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(28, 16);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 3;
    this.textBox1.Text = "textBox1";
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(28, 56);
    this.textBox2.Name = "textBox2";
    this.textBox2.TabIndex = 4;
    this.textBox2.Text = "textBox2";
    // 
    // textBox3
    // 
    this.textBox3.Location = new System.Drawing.Point(28, 92);
    this.textBox3.Name = "textBox3";
    this.textBox3.TabIndex = 5;
    this.textBox3.Text = "textBox3";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(152, 152);
    this.button1.Name = "button1";
    this.button1.TabIndex = 6;
    this.button1.Text = "保存";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(236, 152);
    this.button2.Name = "button2";
    this.button2.TabIndex = 7;
    this.button2.Text = "退出";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(324, 201);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox3);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false); }
    #endregion private void button1_Click(object sender, System.EventArgs e)
    {
    drv["IpAddress"] = this.textBox1.Text;
    drv["UserName"] = this.textBox2.Text;
    drv["Password"] = this.textBox3.Text;
    } private void button2_Click(object sender, System.EventArgs e)
    {
    this.Close();
    }
    }
    }
      

  2.   

    当然,如果修改数据的字段比较多,那
    public Form2(DataRowView dr)
    {
    InitializeComponent();
    this.drv = dr;
    this.textBox1.Text = (string)drv["IpAddress"];
    this.textBox2.Text = (string)drv["UserName"];
    this.textBox3.Text = (string)drv["Password"];
    }

    this.textBox3.Text = (string)drv["Password"];这种语句会比较多,觉得不爽,那在构造函数中传递DataTable的引用,
    然后把textBox绑定到DataTable表上的一些字段中,比如:
    textBox1.DataBindings.Add("Text",dt,"customerID");
    这样,修改就直接反映到DataTable上了,
    如果是新增那就把使用DataTable.NewRow()后把新行加入到DataTable,在通过BindingManagerBase.Position定位到该新行在进行编辑。
      

  3.   

    点击一条datagrid的记录,出来更新界面,,,这条记录是两个或是三表join出来的,这个修改的界面里要更新是两个或是三个表里的东东,,每个表都有一个自动编号。是这样的。这样的多的表join出来一个dataset里的表更新,我就有些蒙了,,一个表读进dataset里在绑到datagrid的更新我搞得有点明白了这么晚了还在线,,:)
      

  4.   

    然后把textBox绑定到DataTable表上的一些字段中,比如:呵呵,是的,,,我现在的作法是更新的窗体上标签,和文本框都没有,,,通过读当前行的视图dataview把字段名绑给标签,字段的值给文本框然后通过oledbcommandbuilder,,OleDbDataAdapter .update(a,a)来更新,,,,,不用写sql,更新多个表的时候,,我也想用这种方法,,一直没有搞定。。这种办法一老永一呀,,,,怎么改,程序基本不动啊
      

  5.   

    我觉得应该是有些困难,毕竟作为access的视图来讲是不能直接更新数据的呀,但是可行的办法是把access里的表读到DataTable里,使用DataTable做关系组成一个新的DataTable作为DataGrid的数据源
      

  6.   

    selectStr = "SELECT Customer.*, Employee.* FROM Customer INNER JOIN Employee ON Customer.CustomerID=Employee.CustomerID;";数据源是这样的,,,,读到了dataset里的一个表里,现在是要更新数据源
      

  7.   

    我的做法就是多个表分开来更新,更好控制.无论几个表join出来的,在内存就一个DataTable,在更新的时候分别编写各个实际表的更新sql语句。
    不过根据表的关系需要注意更新表的顺序,
    Commandbuilder类是比较方便,但是在多个表更新的情况下我建议不要使用,那样会很难控制,而且在效率显得比较重要的时候,也要避免。
    多表更新,
    http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx
      

  8.   

    开了这个贴子,我就是想问一问,更新多个关联表的时候能不能偷懒,,,,不写sql语句。
    后台数据库的设计不断更新,,,项目不断的赶工,这两项之间经常冲突,oledbcommandbuilder能使冲突减少一些,,,唉,这个办法还是行不通啊。。只能写sql了,,在次感谢秋枫.