C# 如何从另一个类向combobox添加项

C# 如何从另一个类向combobox添加项,c#,winforms,C#,Winforms,如何将项目添加到我的组合框中(格式为1),但将项目添加到该组合框的函数位于另一个类中 public void comboBox1_Categories_Load() { SqlConnection con = new SqlConnection(connection_string); string select_string = "SELECT * FROM dbo.Categories"; SqlCommand cmd = new SqlCommand(select_

如何将项目添加到我的组合框中(格式为1),但将项目添加到该组合框的函数位于另一个类中

public void comboBox1_Categories_Load()
{
    SqlConnection con = new SqlConnection(connection_string);

    string select_string = "SELECT * FROM dbo.Categories";
    SqlCommand cmd = new SqlCommand(select_string, con);

    SqlDataReader myReader;
    con.Open();

    myReader = cmd.ExecuteReader();

    while (myReader.Read())
    {
        comboBox1.Items.Add(myReader[1]); 
    }

    myReader.Close();
    con.Close();
}

你可以这样做:

public static OtherClass()
{
    public void RetrieveData(ComboBox comboBox)
    {
        SqlConnection con = new SqlConnection(connection_string);

        string select_string = "SELECT * FROM dbo.Categories";
        SqlCommand cmd = new SqlCommand(select_string, con);

        SqlDataReader myReader;
        con.Open();

myReader = cmd.ExecuteReader();

while (myReader.Read())
{
    comboBox.Items.Add(myReader[1]); 
}

myReader.Close();
con.Close();
    }
}
//然后是你的表格所在的班级

public void comboBox1_Categories_Load()
{
    OtherClass.RetrieveData(comboBox1);
}
表格:

商务舱:

class BusinessLayer
{
    private DataLayer _dataLayer;

    public BusinessLayer()
    {
        _dataLayer = new DataLayer();
    }

    internal List<string> GetCategories()
    {
        return _dataLayer.RetrieveCatagories();
    }
}
class业务层
{
专用数据层_数据层;
公共业务层()
{
_数据层=新数据层();
}
内部列表GetCategories()
{
返回_dataLayer.RetrieveCatagories();
}
}
数据层(您可以重构并提取与其他方法的连接):

类数据层
{
public const string ConnectionString=“我的连接字符串”;
内部列表检索类别()
{
列表项=新列表();
使用(SqlConnection con=newsqlconnection(ConnectionString))
{
string select_string=“select*FROM dbo.Categories”;
SqlCommand cmd=newsqlcommand(选择字符串,con);
con.Open();
SqlDataReader myReader=cmd.ExecuteReader();
while(myReader.Read())
{
添加(myReader[1].ToString());
}
myReader.Close();
}
退货项目;
}
}

尝试以下操作
comboBox1.Items.Add(myReader[1],myReader[1].ToString())
此外,您可能希望使用(){}将sql对象包装在
周围,以利用对象的自动处理功能,或者您可以在while循环
((idisposable)myReader.Dispose()之外对
myReader
对象执行此操作请显示调用事件| |方法的代码
comboBox 1_Categories_Load()
在询问问题时在此处显示所有相关代码您必须在函数中传递对comboBox的引用。更好的方法是让函数将项目列表返回到Form1,然后将它们加载到那里的组合框中。我尽量避免在只涉及向其中加载数据的情况下传递控件引用。我同意@leeharison的观点,返回一个项列表,并在
列表上调用表单的加载函数
AddRange
。项
property我对此没有代码,因为目前没有任何代码可用。我想做的是将使用此函数(在“数据库”类中)的项添加到comboBox1(在类“Form1”中),并在“Form1”中调用此函数。@SwDevMan81感谢您的回答。我不想在另一个类中使用组件。现在我做了一个方法,返回一个列表,使用起来容易多了。
class BusinessLayer
{
    private DataLayer _dataLayer;

    public BusinessLayer()
    {
        _dataLayer = new DataLayer();
    }

    internal List<string> GetCategories()
    {
        return _dataLayer.RetrieveCatagories();
    }
}
class DataLayer
{
    public const string ConnectionString = "my connection string";

    internal List<string> RetrieveCatagories()
    {
        List<string> items = new List<string>();

        using (SqlConnection con = new SqlConnection(ConnectionString))
        {
            string select_string = "SELECT * FROM dbo.Categories";
            SqlCommand cmd = new SqlCommand(select_string, con);

            con.Open();

            SqlDataReader myReader = cmd.ExecuteReader();

            while (myReader.Read())
            {
                items.Add(myReader[1].ToString());
            }

            myReader.Close();
        }

        return items;
    }


}