两个类
类1 LinkDataBase
using System;
using System.Data ;
using System.Data.SqlClient ;namespace WindowsApplication48
{ public class LinkDataBase
{
private string strSQL;
private string connectionStr="WorkStation ID=localhost;Integrated Security=SSPI;DataBase=jxcbook";

private SqlConnection myConnection;
private SqlCommandBuilder SqlCmdBld;
private DataSet ds=new DataSet();
private SqlDataAdapter da; public LinkDataBase()
{
} public DataSet SelectDataBase(string tempStrSQL,string tempTableName)
{
this.strSQL =tempStrSQL;
this.myConnection =new  SqlConnection (this.connectionStr );
this.da =new SqlDataAdapter(this.strSQL ,this.myConnection );
this.ds.Clear ();
this.da.Fill(ds,tempTableName);
return ds;
}
}
}
类2 是窗口类 上面有一个dataGrid和一个按钮
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication48
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1; private DataSet ds=new DataSet();
private LinkDataBase Link=new LinkDataBase ();
private string sendTableName="商品清单cc";
private string sendStrSQL="SELECT 货号, 条码, 拼音编码, 品名, 单位, 进货价, 销售价1, 销售价2, 规格, 产地, 类别, 最低售价 FROM 商品清单";
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
this.ds =Link.SelectDataBase(sendStrSQL,sendTableName);
this.dataGrid1.DataSource =ds.Tables[0]; }
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}
                  //设计器生成的代码......
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
string strSpellWord=this.textBox1.Text.Trim ();
string tempStrSQL="Select * from 商品清单 where 拼音编码 like '" +strSpellWord +"%'";
this.Link.SelectDataBase (tempStrSQL,sendTableName);
}
}
}问题: button1在Click时 其中的this.Link.SelectDataBase (tempStrSQL,sendTableName);
为什么能影响到dataGrid1中内容?
dataGrid1在构造函数时绑定了ds,但是
this.Link.SelectDataBase (tempStrSQL,sendTableName);怎么能影响到ds呢??小弟谢谢了!!!!!!!!!

解决方案 »

  1.   

    看看你的LinkDataBase的定义就明白了public class LinkDataBase
    {
    private string strSQL;
    private string connectionStr="WorkStation ID=localhost;Integrated Security=SSPI;DataBase=jxcbook";

    private SqlConnection myConnection;
    private SqlCommandBuilder SqlCmdBld;
    private DataSet ds=new DataSet();  //这个ds一直没变然后Form1中
    public Form1()
    {
    InitializeComponent();
    this.ds =Link.SelectDataBase(sendStrSQL,sendTableName);Form中的ds 跟这个Link 中的ds成为一个了
      

  2.   

    使用DataGrid.DataSource实际上是一种mvc模式应用。
    this.dataGrid1.DataSource =ds.Tables[0];
    的意思是引用Link中的ds中的Table[0]
    再执行一遍this.Link.SelectDataBase (tempStrSQL,sendTableName);,因为表名没有改变,只是改变了Link.ds.Table[0]的内容,引用该数据的视图DataGrid的显示当然就会反应Table【0】
    中的变化了。
      

  3.   

    你把这个函数改成这样就看出不同了public DataSet SelectDataBase(string tempStrSQL,string tempTableName)
    {
    this.strSQL =tempStrSQL;
    this.myConnection =new  SqlConnection (this.connectionStr );
    this.da =new SqlDataAdapter(this.strSQL ,this.myConnection );
                               DataSet data=new DataSet;
    this.da.Fill(data,tempTableName);
    return data;
    }
      

  4.   

    to charles_y(难得糊涂) 
    这两个怎么会是一个呢,他们都是私有变量。
    to
     Beta4(一会就好)
    是不是说Form中的ds实质上指向了Link中的ds,而dataGrid1绑定了Form中ds,Link.SelectDataBase会影响 Link中的ds,也就影响了Form中的ds(如果只是引用的话也谈不上影响了)。 
    如果是这样的话 Link中的ds是私有的,外部能直接引用么??