简体   繁体   English

C# 在使用多个不同信息填充下拉列表时遇到问题

[英]C# Having trouble with populating drop down list with multiple different informations

I am building a form application for ordering phones.我正在构建一个用于订购电话的表单应用程序。 I have a drop down list which needs to have addresses of different stores.我有一个下拉列表,需要有不同商店的地址。 The address is stored in a MSSQL database where state,city,street and zip code are different columns in the table store.地址存储在 MSSQL 数据库中,其中 state,city,street 和 zip 代码是表存储中的不同列。 I am having trouble with getting the drop down list to show state,city,street instead of wanted data i get System.DataRowView in the drop down list as a option.我无法让下拉列表显示 state,city,street 而不是想要的数据,我在下拉列表中选择 System.DataRowView。 I tried just requiring city or state and it works and shows the correct data.我尝试只要求 city 或 state 并且它可以工作并显示正确的数据。

Is there a way of getting all three informations(state,city,street) or should i just alter the sql table to contain this data in one column?有没有办法获取所有三个信息(州、城市、街道)或者我应该只更改 sql 表以将这些数据包含在一个列中?

Here is the code:这是代码:

            try
            {
                SqlConnection connection = getConnection();
                connection.Open();
                SqlCommand sc = new SqlCommand("select state,city,street,sotreID from store", connection);
                SqlDataReader reader;
                reader = sc.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Columns.Add("street", typeof(string));
                dt.Columns.Add("storeID", typeof(int));

                dt.Load(reader);
                prodavnicaCombo.ValueMember = "storeID";
                prodavnicaCombo.DisplayMember = "state,city,street"; //the troublsome line is here
                
                prodavnicaCombo.DataSource = dt;
                connection.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show("Exception: " + err.Message);
            }

Only one column can be used as in ValueMember & DisplayMember property.在 ValueMember 和 DisplayMember 属性中只能使用一列。 But here is wayaround you can use, instead of altering you database just concatenate your display member columns in your query and use it但是这是您可以使用的解决方法,而不是更改您的数据库,只需在查询中连接您的显示成员列并使用它

 try
            {
                SqlConnection connection = getConnection();
                connection.Open();
                SqlCommand sc = new SqlCommand("select (state + ' ' city + ' ' + street) as displayMember,sotreID from store", connection);
                SqlDataReader reader;
                reader = sc.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Columns.Add("street", typeof(string));
                dt.Columns.Add("storeID", typeof(int));

                dt.Load(reader);
                prodavnicaCombo.ValueMember = "storeID";
                prodavnicaCombo.DisplayMember = "displayMember"; //the troublsome line is here
                
                prodavnicaCombo.DataSource = dt;
                connection.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show("Exception: " + err.Message);
            }

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

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