【问题标题】:Combo not displaying select item and showing as an item list组合不显示选择项目并显示为项目列表
【发布时间】:2020-07-23 16:27:31
【问题描述】:
 public void DataGrid_Data()
    {
        // 2 second delay before loading DataGrid
        var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(.80) };
        timer.Start();
        timer.Tick += (sender, args) =>
        {
            timer.Stop();

            // Attemption to connect to SQL Server database and populate DataGrid with database tables. 
            try
            {
                string connectionString = ("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
                SqlConnection connection = new SqlConnection(connectionString);

                SqlCommand cmd = new SqlCommand("SELECT [hb_Disputes].[DSP_ID], [hb_disputes].[ACCOUNT], [Users].[TX_EMPLOYEE], [hb_CsrNames].[NM_USER], [hb_disputes].[CUST_NAME],[hb_disputes].[PREM_ADDR], [hb_status].[Status], [hb_disputes].[OPENED], [hb_disputes].[DEADLINE], [hb_disputes].[DATERSLVD], [hb_rpttype].[ReportType], [hb_ratetype].[RateType], [hb_Disputes].[FR_DT_FIRSTREV], [hb_Disputes].[FR_TS_LATESTUPD], [hb_Disputes].[COMMENT], [hb_Disputes].[FR_DSP_CLSF], [hb_Disputes].[FR_CUST_CNTCT], [hb_Disputes].[FR_WRK_REQ], [hb_Disputes].[FR_OPN_ERR], [hb_Disputes].[FR_SO_TP], [hb_Disputes].[FR_SO_DTLS], [hb_Disputes].[FR_SO_DT_WNTD], [hb_Disputes].[FR_SO_ISSD_BY], [hb_Disputes].[FR_CMMNT] FROM [hb_disputes]" +
                    " LEFT JOIN [Users] ON [hb_disputes].[ASSGNTO] = [Users].[KY_USER_ID] LEFT JOIN [hb_CsrNames] ON [hb_disputes].[WFMUSER] = [hb_CsrNames].[KY_USER_ID] LEFT JOIN [hb_status] ON [hb_disputes].[STATUS] = [hb_status].[STSID] LEFT JOIN [hb_rpttype] ON [hb_disputes].[RPTTYPE] = [hb_rpttype].[RPTID] LEFT JOIN [hb_ratetype] ON [hb_disputes].[REV_CLS] = [hb_ratetype].[RTID]", connection);
                connection.Open();
                DataTable dt = new DataTable();

                dt.Load(cmd.ExecuteReader());
                connection.Close();

                dtGrid.DataContext = dt;

                // Focus in on line 1 of the data grid on UserControl load 
                // So it can be shown in the key information side column

                dtGrid.Visibility = Visibility.Visible;
                dtGrid.Focus();
                dtGrid.SelectedIndex = 0;


            }
            catch
            {
                MessageBox.Show("Database connection is not available at this time. Please contact your database administrator ");
            }
        };
    }


    private void dtGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {

        // User double clicks on DataGrid Row
        // Open new Window
        // Populate selected textboxes with selected datarow
        DataGrid gd = (DataGrid)sender;
        DataRowView row_selected = gd.SelectedItem as DataRowView;

        var windowToOpen = new Window1();

        if (gd != null)
        {
            // Textboxes
            windowToOpen.txt_RowRecrd.Text = row_selected["DSP_ID"].ToString();
            windowToOpen.txt_acctnumber.Text = row_selected["ACCOUNT"].ToString();
            windowToOpen.txt_analyst.Text = row_selected["TX_EMPLOYEE"].ToString();
            windowToOpen.txt_custname.Text = row_selected["CUST_NAME"].ToString();
            windowToOpen.txt_address.Text = row_selected["PREM_ADDR"].ToString();
            windowToOpen.txt_Status.Text = row_selected["Status"].ToString();
            windowToOpen.txt_opened.Text = row_selected["OPENED"].ToString();
            windowToOpen.txt_deadline.Text = row_selected["DEADLINE"].ToString();
            windowToOpen.txt_DateResolved.Text = row_selected["DATERSLVD"].ToString();
            windowToOpen.txt_revcls.Text = row_selected["RateType"].ToString();
            windowToOpen.txt_WFMissuedBy.Text = row_selected["NM_USER"].ToString();
            windowToOpen.txt_firstreview.Text = row_selected["FR_DT_FIRSTREV"].ToString();
            windowToOpen.txt_Latestupdate.Text = row_selected["FR_TS_LATESTUPD"].ToString();
            windowToOpen.txt_reviewNotes.Text = row_selected["FR_CMMNT"].ToString();
            windowToOpen.txt_ResolutionNotes.Text = row_selected["COMMENT"].ToString();



            // ComboBoxes

            windowToOpen.cmb_UtilityRptTyp.Items.Clear();
            foreach (DataRowView row in dtGrid.SelectedItems)
            {
                windowToOpen.cmb_UtilityRptTyp.Items.Add(row.Row["ReportType"].ToString());
            }

            windowToOpen.Show();
        }
        else
        {
            return;
        }

    }

代码显示我的 Datagrid 正在由 SQL Server 数据库加载,双击事件会打开一个新窗口,并且 Datagrid 行信息填充在文本框和组合框字段中。

我正在尝试显示已选择的“实用程序报告类型”但是,我只能使用 windowToOpen.cmb_UtilityRptTyp.Items.Add(row.Row["ReportType"].ToString() 将其显示为组合框项);

我有一些后端代码填充该组合框,但在 Loaded 事件上,它也从 SQL Server 数据库中提取,但我已在此示例中禁用它。

【问题讨论】:

    标签: c# sql-server wpf combobox datagrid


    【解决方案1】:

    您应该设置 SelectedIndex 属性以将项目显示为选中状态。

    Items.Add() 返回新添加项的索引。

    这意味着,(我假设只能从网格中选择一行),以下就足够了:

    变化:

    cmb_UtilityRptTyp.Items.Add("My Report Type");
    

    收件人:

    cmb_UtilityRptTyp.SelectedIndex = cmb_UtilityRptTyp.Items.Add("My Report Type");
    

    【讨论】:

    • 我让它显示为文本(varchar),我的数据库需要一个数字。我在组合框所在的窗口中有一些用于填充值的组合框的代码。有什么方法可以使用 SelectedValuePath 或 DiplayMemeberPath 来绑定所有内容?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2011-08-30
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多