我从数据库中取出一段时间的数据,想在winform中几个不同的控件中都要使用到datatable中的数据,请问怎么一次性取出来后,分别在不同的控件事件中使用这些数据?

解决方案 »

  1.   

    声明DataTable为全局变量
    DataTable dt;
    Form1_Load()
    {}
      

  2.   

    不行,在Form1_Load中定义了 dt = oraCls.getCoilWeightEveryDay(CBX_Year.Text + CBX_Month.Text + "01", CBX_Year.Text + CBX_Month.Text + Monthdays.ToString());但是在其它函数中再次使用dt时,dt是空的。
      

  3.   

    把 datatable 当成参数传递啊
      

  4.   

    你的几个控件是在什么使用这个dt呢?初始化的时候?如果是这样的话建议放到类的构造函数里DataTable dt; 
    public yourclass()

    dt = oraCls.getCoilWeightEveryDay(CBX_Year.Text + CBX_Month.Text + "01", CBX_Year.Text + CBX_Month.Text + Monthdays.ToString());

      

  5.   

    用singleton模式就行了。public class SharedDataTable
    {
        private static SharedDataTable ds;    public DataTable SharedDT
        {
            get;
            set;
        }    public SharedDataTable()
        {
            if (this.SharedDT == null)
            {
                this.SharedDT = new DataTable();
            }
        }    public static SharedDataTable getDataSource()
        {
            if (ds == null)
            {
                ds = new SharedDataTable();
            }
            return ds;
        }
    }//使用的时候
    var dt = SharedDataTable.getDataSource();
      

  6.   

    用 dt.SharedDT 获取共享的datatable
      

  7.   

    是这样的,定义一个公共类 XXX.CS然后再里面定义一个静态的datatable  public static开始就给它赋值,以后就都可以用了  
    别忘记给分哦!^_^!!!