简体   繁体   中英

How can I convert an object from a combobox selected item to a string

This is all in a WINDOWS FORM C#, MICROSOFT VISUAL STUDIO 2008

I have a combo box that is being displayed this way:

private void populateCombos()
    {
        string GetConn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\\Data\\Db\\Comp.mdb";
        string queryString = "SELECT DISTINCT DC FROM Comp";
        OleDbDataAdapter dA = new OleDbDataAdapter(queryString, GetConn1);
        DataTable dC = new DataTable();
        dA.Fill(dC);
        comboBoxDC.DataSource = dC;
        comboBoxDC.DisplayMember = "DC";

        string GetConn2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\\Data\\Db\\Comp.mdb";
        string queryString2 = "SELECT DISTINCT PL FROM Comp";
        OleDbDataAdapter dA2 = new OleDbDataAdapter(queryString2, GetConn2);
        DataTable pL = new DataTable();
        dA2.Fill(pL);
        comboBoxPL.DataSource = pL;
        comboBoxPL.DisplayMember = "PL";
    }

I am having issues here being that I cannot make the selected item into a string:

        object da = comboBoxDC.SelectedItem;
        object pr = comboBoxPL.SelectedItem;
        Console.WriteLine(da.ToString());

        Console.WriteLine(da);
        Console.WriteLine(pr);
        //Connection...

        var list = new List<dataQuery>();
        string GetConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\\Data\\Db\\Comp.mdb";
        string connectionString = GetConnectionString;
        string queryString = "SELECT DC, PL, CompID, User, Email FROM Comp WHERE DC = \'" + da + "\' AND PL = \'" + pr + "\'";

And in order for me to query these commands I need the selected item comboBoxDC to be a string and same for comboBoxPL.

ANSWER!!!!!!!:

So I found this out:

code:

string da = comboBoxDC.Text.ToString();

string pr = comboBoxPL.Text.ToString();

Console.WriteLine(da)

Console.WriteLine(pr)

output is successful with text.tostring and is actually string.

MessageBox.Show(comboBoxDC.SelectedItem.ToString());

适用于我的WPF解决方案。

you need to cast selecteditem into particular class and then to string. in this case you have binded datatable with combobox so cast in this way.

String str = ((DataRowView)comboBox1.SelectedItem)["ColumnName"].ToString();

You can call the toString method on the SelectedItem. From MSDN,

Object selectedItem = comboBox1.SelectedItem;

MessageBox.Show("Selected Item Text: " + selectedItem.ToString());

Assuming this is in WinForms, my guess is that when you call comboBoxDC.SelectedItem the result is "System.Data.DataRowView". The problem with your code is that you only set the DisplayMember. If you do not set the ValueMember property, the default value of the ComboBox selection is a DataRowView, if your data source is a DataTable (or DataView).

To get the data you are looking for, modify your code like the example below.

EDIT Apologies, my method would work, assuming that your fields are Text Objects. If they are not (ie Decimals, Ints, etc.) you will need to first cast them to their respective data types, and then call ToString() on the result.

If you set the ValueMember for a field within your data object, it will not be a DataRowView, but the field specified. If you are still getting "System.Data.DataRowView" as the string result, then there is another error with your code. Please update your question and I'll help you find it.

    private void populateCombos()
    {
    string GetConn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\\Data\\Db\\Comp.mdb";
    string queryString = "SELECT DISTINCT DC FROM Comp";
    OleDbDataAdapter dA = new OleDbDataAdapter(queryString, GetConn1);
    DataTable dC = new DataTable();
    dA.Fill(dC);
    comboBoxDC.DataSource = dC;
    comboBoxDC.DisplayMember = "DC";
    comboBoxDC.ValueMember = "DC"; //Add this line.

    string GetConn2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\\Data\\Db\\Comp.mdb";
    string queryString2 = "SELECT DISTINCT PL FROM Comp";
    OleDbDataAdapter dA2 = new OleDbDataAdapter(queryString2, GetConn2);
    DataTable pL = new DataTable();
    dA2.Fill(pL);
    comboBoxPL.DataSource = pL;
    comboBoxPL.DisplayMember = "PL";
    comboBoxPL.ValueMember = "PL"; //Add this line, too.
}

Now, change your code that gets the text value as follows:

    string sDa = comboBoxDC.SelectedValue.ToString(); //Do this if this is a string column
    string sPr = comboBoxPL.SelectedValue.ToString(); // ''   ''    ''   ''   ''

    //If your data is not strings, cast them to their respective types

    Console.WriteLine(da.ToString());

    Console.WriteLine(sDa);
    Console.WriteLine(sPr);

SelectedItem is exposed as a string but you've cast it to an object. Try replacing the assignment with:

string da = comboBoxDC.SelectedItem;
string pr = comboBoxPL.SelectedItem;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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