簡體   English   中英

WinForms組合框顯示ValueMember而不是DisplayMember

[英]WinForms combobox displays ValueMember instead of DisplayMember

我正在嘗試從數據庫返回的數據表中填充一個下拉列表。 選擇之后,我將使用BatchNumber(int)運行查詢,但是我希望組合框顯示LastRun(格式化的DateTime)。 我已經驗證了列名是否匹配。

由於某些原因,正在顯示BatchNumber。 當打印出cboLastRunTime.DisplayMember時,即使我將其設置為LastRun,也將在其中顯示BatchNumber。 我已經閱讀了許多有關該主題的文章,並嘗試了數據綁定和其他技術。 沒有任何效果。 如何顯示DisplayMember?

我對Windows Forms很陌生。 如果您需要更多信息,請告訴我。

    private void GetLastRunTimes()
    {
        _dataTable = _process.GetLastRunTimes(); //retrieves data from DB
        cboxLastRunTimes.DataSource = _dataTable;
        cboxLastRunTimes.DisplayMember = "LastRun";
        cboxLastRunTimes.ValueMember = "BatchNumber";
    }

編輯:這是我在組合框中使用值的地方:

    private void btnGetPastResults_Click(object sender, EventArgs e)
    {
        try
        {
            int resultId = Convert.ToInt32(cboxLastRunTimes.SelectedValue);
            GetPreviousReconciliationInfo(resultId);
            if (_resultsDT != null)
                LoadDataGrid(); 
            //other UI changes
        }
        catch (Exception ex)
        {
            //error handling
        }
     }

    private void GetPreviousReconciliationInfo(int batchNumber)
    {
        _resultsDT = _process.Reconcile_GetPreviousReconciliationInfo(batchNumber); //retrieves data from DB
    }

當數據庫中的列名稱為LastRun時,它在從SQLClient的途中被轉換為LastReconciledTime。 將DisplayMember的值更改為LastReconciledTime可以修復它。

感謝@Fabio提醒您再次檢查!

暫無
暫無

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

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