简体   繁体   English

c#在Windows窗体应用程序中使用MySQL在组合框中填充和使用值

[英]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.

相关问题 使用C#在Windows窗体应用程序的组合框项目中作为工具提示的图像 - Image as tool tip in combo box item for windows form application using C# 如何在C#中使用itextsharp从组合框中选择一个值 - How to select a value from a combo box using itextsharp in c# 使用C#组合框中的选定值传递参数 - passing parameters using selected value from combo box in c# C#Windows窗体-如何为组合框字符串分配一个双精度值,然后将其显示在文本框中? - C# Windows Form - How do I assign a double value to a combo box string to then be displayed in a text box? 如何在C#Windows应用程序中将默认值为0的文本“ SELECT”插入组合框中的第一项 - How to insert first item in combo box with text “SELECT” with default value 0 in C# windows application 使用C#Windows应用程序填充checkedListBox - Populating checkedListBox using c# windows application 使用Lambda表达式设置组合框数据源C#Windows Forms应用程序 - using lambda expression to set combo box data source c# windows Forms application 如何从Windows窗体的组合框下显示的列表中删除或隐藏组合框的先前选定值? - How to remove or hide the previously selected value of a combo box from the list that are displayed under the combo box in Windows Form? 将项目添加到组合框,同时在 c# Windows 表单中通过字符串引用组合框名称 - Add items to combo Boxes while referring to the combo box name by a string in c# Windows form 从Visual Studio Windows Form C#的组合框中选择选项后,如何使文本显示在文本框中 - How To Make Text Appear In Text Box, After Selecting Option From Combo Box In Visual Studio Windows Form C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM