簡體   English   中英

如何將對象從組合框選定項目轉換為字符串

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

這一切都在WINDOWS FORM C#,MICROSOFT VISUAL STUDIO 2008中

我有一個以這種方式顯示的組合框:

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";
    }

我在這里遇到的問題是我不能將所選項目變成字符串:

        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 + "\'";

為了讓我查詢這些命令,我​​需要選擇的項目comboBoxDC為字符串,並且對於comboBoxPL是相同的。

回答!!!!!!!:

所以我發現了這個:

碼:

string da = comboBoxDC.Text.ToString();

string pr = comboBoxPL.Text.ToString();

Console.WriteLine(da)

Console.WriteLine(pr)

使用text.tostring輸出成功,實際上是字符串。

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

適用於我的WPF解決方案。

你需要將selecteditem轉換為特定的類,然后轉換為字符串。 在這種情況下,您已使用組合框綁定數據表,以便以這種方式進行投射。

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

您可以在SelectedItem上調用toString方法。 來自MSDN,

Object selectedItem = comboBox1.SelectedItem;

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

假設這是在WinForms中,我的猜測是當你調用comboBoxDC.SelectedItem時,結果是“System.Data.DataRowView”。 您的代碼的問題是您只設置了DisplayMember。 如果未設置ValueMember屬性,則如果數據源是DataTable(或DataView),則ComboBox選擇的默認值為DataRowView。

要獲取您要查找的數據,請修改您的代碼,如下例所示。

編輯道歉,我的方法可行,假設您的字段是文本對象。 如果它們不是(即Decimals,Ints等),則需要先將它們轉換為各自的數據類型,然后對結果調用ToString()。

如果為數據對象中的字段設置ValueMember,則它不是DataRowView,而是指定的字段。 如果您仍然將“System.Data.DataRowView”作為字符串結果,那么您的代碼會出現另一個錯誤。 請更新您的問題,我會幫您找到它。

    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.
}

現在,更改獲取文本值的代碼,如下所示:

    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作為字符串公開,但您已將其強制轉換為對象。 嘗試將作業替換為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM