简体   繁体   English

用另一种形式的值填充组合框

[英]populate combobox with values from another form

I might ask a stupid question, but I can't find it on the internet. 我可能会问一个愚蠢的问题,但我无法在互联网上找到它。

I have a combobox and I would like to retrieve some data from another form where the info is typed and saved by the user. 我有一个组合框,我想从另一种形式的信息中检索一些数据,该信息由用户键入并保存。 If anyone is willing to help, I'll be greatful. 如果有人愿意帮助我,我将不胜感激。 Thanks 谢谢

I used this code 我用了这段代码

 string cs = "Data Source=CODRINMA\\CODRINMA;Initial Catalog=BusManager; Trusted_Connection=True;";
            string select = "SELECT * FROM TipAutocar";

            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(select, con);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    cmbTip.Items.Add(dr["Model", "Type"]);
                }
                con.Close();

And it worked..but is this possible to get for eg "Model" + "Type"? 和它的工作..但是这可能获得例如“模型” +“类型”?

And I get this error -> 我得到这个错误->

Error 1 No overload for method 'this' takes 2 arguments 错误1方法'this'没有重载需要2个参数

If I understood your question correctly, you want to add different column values to your ComboBox concatenated. 如果我正确理解了您的问题,则希望将不同的列值添加到串联的ComboBox So, it's as simple as that: 因此,就这么简单:

while (dr.Read())
    {
      cmbTip.Items.Add(dr["Model"].ToString() + " " + dr["Type"].ToString());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM