2013-09-05 20 views
-1

我预先加载远程sql数据库中的一些数据,以加快应用程序在使用过程中,我有一个窗体上的几个组合框。c#静态列表用于两个组合问题

当应用程序加载时,我预加载了一个名为“用户名”的静态List值。

我将两个组合数据源都设置为“用户名”。但是,当我运行应用程序并从第一个组合中选择一个用户名时,第二个组合中也选择了相同的用户名,反之亦然?

我知道我可以预先加载用户名作为不同的静态,但这将需要额外的时间来加载应用程序,我相信这只是非常愚蠢的东西,我完全忽略!

static List<string> Usernames = new List<string>(); 
    public Form1() 
    { 
     InitializeComponent(); 

     Usernames.Add("Paul"); 
     Usernames.Add("Elaine"); 
     Usernames.Add("Elliot"); 
     Usernames.Add("Matt"); 
     Usernames.Add("Brian"); 

     comboBox1.DataSource = Usernames; 
     comboBox2.DataSource = Usernames; 

    } 

谢谢你在先进

保罗

+1

你有没有试过这个代码或不是? – Steve

+0

是的,当然。这是我尝试实现的非常简单的例子(使用大量远程在线数据库调用的大得多的应用程序)。总体目标是加速应用程序,以最大限度地减少数据库调用 – Belliez

回答

3

尝试使用BindingSource

static List<string> Usernames = new List<string>(); 
public Form1() 
{ 
    InitializeComponent(); 

    Usernames.Add("Paul"); 
    Usernames.Add("Elaine"); 
    Usernames.Add("Elliot"); 
    Usernames.Add("Matt"); 
    Usernames.Add("Brian"); 

    BindingSource bs1 = new BindingSource(); 
    bs1.DataSource = Usernames; 
    comboBox1.DataSource = bs1; 
    BindingSource bs2 = new BindingSource(); 
    bs2.DataSource = Usernames; 
    comboBox2.DataSource = bs2; 
} 

一个BindingSource的情况下保持独立的货币管理(底层数据源定位)两个连击

+0

这个效果很好 – SerenityNow

+0

这个效果很好,谢谢 - (必须等待32秒才能接受这个答案!) – Belliez

0

我知道我可以只预先加载的用户名不同静但这将需要额外的时间来加载应用程序

这不会增加显着的时间。主要的问题是每次页面加载时,都会将值添加到静态列表中。你只需要添加一次。

您有几种选择:

  1. 负载中的姓名Page小号静态构造:

    static List<string> Usernames = new List<string>(); 
    
    public static Form1() 
    { 
        Usernames.Add("Paul"); 
        Usernames.Add("Elaine"); 
        Usernames.Add("Elliot"); 
        Usernames.Add("Matt"); 
        Usernames.Add("Brian"); 
    } 
    
    public Form1() 
    { 
        InitializeComponent(); 
    
        comboBox1.DataSource = Usernames; 
        comboBox2.DataSource = Usernames; 
    
    } 
    
  2. 榜上无名非静态,每次加载它:

    List<string> Usernames = new List<string>(); 
    public Form1() 
    { 
        InitializeComponent(); 
    
        Usernames.Add("Paul"); 
        Usernames.Add("Elaine"); 
        Usernames.Add("Elliot"); 
        Usernames.Add("Matt"); 
        Usernames.Add("Brian"); 
    
        comboBox1.DataSource = Usernames; 
        comboBox2.DataSource = Usernames; 
    
    } 
    
+0

该应用程序是一个基于Windows的c#.net缺陷跟踪应用程序,它在打开和关闭ticvkets时使用许多数据库调用。由于将数据库从本地网络移动到基于Web的远程位置,由于数据库调用,它已经减慢了很多。我想预先加载所有常用数据(用户名,错误状态名称以及在应用程序等中重复使用的内容),我的速度提高了50%,但使用静态列表作为静态列表正在导致所描述的几个问题。 – Belliez