[英]c# populating and using the value from a combo box with MySQL in a Windows Form Application
I have a combo box in a C# Windows form application that is being populated from a mysql database query using the following code. 我在C#Windows窗体应用程序中有一个组合框,该组合框正在使用以下代码从mysql数据库查询中填充。
MySqlDataAdapter sqlquery = new MySqlDataAdapter("SELECT users.username AS username, users.Id AS id FROM users", conn);
DataTable populateDreturnCBox = new DataTable("users");
sqlquery.Fill(populateDreturnCBox);
dreturnCbox1.DataSource = populateDreturnCBox;
dreturnCbox1.ValueMember = populateDreturnCBox.Columns[1].ColumnName;
dreturnCbox1.DisplayMember = populateDreturnCBox.Columns[0].ColumnName;
This does appears to work but the issue is that when I pass the SelectedValue.ToString() into a string variable, as shown below, I get a string with System.Data.DataRowView displayed instead of the string value selected. 这看起来确实可行,但是问题是当我将SelectedValue.ToString()传递给字符串变量时,如下所示,我得到的字符串显示了System.Data.DataRowView而不是所选的字符串值。
string val = dreturnCbox1.SelectedItem.ToString();
MessageBox.Show(val);
I realise that I am trying to access an array of data values but am not sure how to pass the value. 我意识到我正在尝试访问数据值数组,但不确定如何传递值。 Do I need to create a for loop to find the index position in order to access the data in the combo box, or am I barking up the wrong tree with this?
我是否需要创建一个for循环来查找索引位置以便访问组合框中的数据,还是我为此而树错了脚?
Thanks in advance. 提前致谢。
这样的事情怎么样:
string val = ((DataRowView)dreturnCbox1.SelectedItem).Row[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.