DataGridViewComboBoxCell 已填充但不会显示内容 [英] DataGridViewComboBoxCell populated but will not display content

查看:46
本文介绍了DataGridViewComboBoxCell 已填充但不会显示内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我设置为 ComboBoxcolumn 的 datagridview 列之一动态设置自定义下拉列表.当我运行我的应用程序时,我看到我的单元格中有值.但是,我没有看到 UI 中弹出任何值.

I am trying to dynamically set a custom dropdownlist for one of my datagridview columns which I set as a ComboBoxcolumn. When I run my application I see that there are values in my cell. However, I see no values pop up in the UI.

private void CustomComboBoxColumns(string filter)
    {
        DataGridViewComboBoxColumn ComboBoxColumn = new DataGridViewComboBoxColumn();
        DataTable dt;
        ComboBoxColumn.HeaderText = "category";
        ComboBoxColumn.DataPropertyName = "category";
        ComboBoxColumn.ReadOnly = false;
        ComboBoxColumn.MaxDropDownItems = 100;
        ComboBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
        ComboBoxColumn.FlatStyle = FlatStyle.Flat;
        ComboBoxColumn.ValueMember = "category";
        ComboBoxColumn.DisplayMember = "category";
        _iprDataGridView.Columns.Insert(16, ComboBoxColumn);
        for (int i = 0; i < _iprDataGridView.Rows.Count; i++)
        {
            dt = GetDataForCategory(filter);
            ((DataGridViewComboBoxCell)_iprDataGridView.Rows[i].Cells[16]).DataSource = dt;
        }
    }

    private DataTable GetDataForCategory(string filter)
    {
        DbConnection db = new DbConnection();
        string connString = db.BuildConnectionString();
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        string query = "Select category from cd_category where category like '%" + filter + "%' order by category";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            SqlDataAdapter da = new SqlDataAdapter(query, conn);
            da.Fill(ds, "category");
            dt = ds.Tables["category"];
            da.Dispose();
            conn.Close();
        }
        return dt;
    }

当我附加调试器并检查 ((DataGridViewComboBoxCell)_iprDataGridView.Rows[i].Cells[16]).DataSource = dt;我的项目集合中有值,它只是不会显示它.

When I attach my debugger and check ((DataGridViewComboBoxCell)_iprDataGridView.Rows[i].Cells[16]).DataSource = dt; there are values in my item collection it just will not display it.

这里有更多关于如何设置 datagridview 的代码.

Here is more code on how the datagridview is set up.

_iprDataGridView.DataSource = _dbHelper.Select(queryBuild.ToString(), parameters, dbConnection);

            if (_iprDataGridView.DataSource == null) return;
            // In order to display and match the PIR table values, that are requried to be in
            // a comboBox menu, we need to remove the columns that have been populated and then
            // re-add them as comboBoxs
            ConfigureDataGridView();
            ConfigureComboBoxColumns();
            ConfigureTextBoxColumns();

我在考虑在 ConfigureTextBoxColumns() 之后我将创建我的方法,该方法将遍历 datagridview 并创建一个新的类别组合框列并根据另一个列值插入一个新的组合框单元.

I am thinking after the ConfigureTextBoxColumns() I will create my method that will iterate through the datagridview and create a new category comboboxcolumn and insert a new comboboxcell depending on another columns value.

这是如何为 datagridview 生成列的代码

Here is the code for how the columns are generated for the datagridview

private void ConfigureComboBoxColumns()
    {
        DataGridViewHelper dataGridViewHelper = new DataGridViewHelper();
        // Columns being converted to comboboxcolumns must be removed, created, and re-inserted
        dataGridViewHelper.RemoveColumns(ref _iprDataGridView,
                                         Columns.severity.ToString(),
                                         Columns.phase.ToString(),
                                         Columns.app.ToString(),
                                         Columns.project.ToString(),
                                         Columns.acceptancePeriod.ToString(),
                                         Columns.classConfirmed.ToString(),
                                         Columns.oncall.ToString(),
                                         Columns.warranty.ToString(),
                                         Columns.release.ToString(),
                                         Columns.category.ToString(),
                                         Columns.ticketType.ToString());

        DataGridViewComboBoxColumn comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.severity.ToString());
        comboBoxColumn.ValueType = typeof(int);
        string query = IPRDetailsConstants.SELECT_SEVERITY + IPRDetailsConstants.SEVERITY_COL;
        dataGridViewHelper.SetComboBoxChoicesDataSource(ref comboBoxColumn, Columns.severity.ToString(), query, dbConnection);
        _iprDataGridView.Columns.Insert((int)Columns.severity, comboBoxColumn);

        comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.phase.ToString());
        query = IPRDetailsConstants.SELECT_PHASE + IPRDetailsConstants.PHASE_COL;
        dataGridViewHelper.SetComboBoxChoicesDataSource(ref comboBoxColumn, Columns.phase.ToString(), query, dbConnection);
        _iprDataGridView.Columns.Insert((int)Columns.phase, comboBoxColumn);

        comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.app.ToString());
        query = IPRDetailsConstants.SELECT_APP + IPRDetailsConstants.APP_COL;
        dataGridViewHelper.SetComboBoxChoicesDataSource(ref comboBoxColumn, Columns.app.ToString(), query, dbConnection);
        _iprDataGridView.Columns.Insert((int)Columns.app, comboBoxColumn);

        comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.project.ToString());
        bool activeProjectsOnly = bool.Parse(ConfigurationManager.AppSettings.Get("ActiveProjectsOnly"));
        query = IPRDetailsConstants.SELECT_PROJECT + (activeProjectsOnly ? IPRDetailsConstants.PROJECT_COL_ACTIVE : IPRDetailsConstants.PROJECT_COL);
        dataGridViewHelper.SetComboBoxChoicesDataSource(ref comboBoxColumn, Columns.project.ToString(), query, dbConnection);
        _iprDataGridView.Columns.Insert((int)Columns.project, comboBoxColumn);

        comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.acceptancePeriod.ToString());
        comboBoxColumn.HeaderText = Resources.IprDetails_ConfigureComboBoxColumns_ap;
        comboBoxColumn.HeaderCell.ToolTipText = "acceptancePeriod";
        dataGridViewHelper.SetComboBoxChoicesStatic(ref comboBoxColumn, Columns.acceptancePeriod.ToString(), Resources.Y, Resources.N, Resources.S, Resources.X);
        _iprDataGridView.Columns.Insert((int)Columns.acceptancePeriod, comboBoxColumn);

        comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.classConfirmed.ToString());
        comboBoxColumn.HeaderText = Resources.IprDetails_ConfigureComboBoxColumns_cc;
        comboBoxColumn.HeaderCell.ToolTipText = "classConfirmed";
        dataGridViewHelper.SetComboBoxChoicesStatic(ref comboBoxColumn, Columns.classConfirmed.ToString(), Resources.Y, Resources.N, Resources.X);
        _iprDataGridView.Columns.Insert((int)Columns.classConfirmed, comboBoxColumn);

        comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.oncall.ToString());
        comboBoxColumn.HeaderText = Resources.IprDetails_ConfigureComboBoxColumns_oc;
        comboBoxColumn.HeaderCell.ToolTipText = "onCall";
        dataGridViewHelper.SetComboBoxChoicesStatic(ref comboBoxColumn, Columns.oncall.ToString(), Resources.Y, Resources.N, Resources.X);
        _iprDataGridView.Columns.Insert((int)Columns.oncall, comboBoxColumn);

        comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.warranty.ToString());
        comboBoxColumn.HeaderText = Resources.IprDetails_ConfigureComboBoxColumns_w;
        comboBoxColumn.HeaderCell.ToolTipText = "warranty";
        dataGridViewHelper.SetComboBoxChoicesStatic(ref comboBoxColumn, Columns.warranty.ToString(), Resources.Y, Resources.N, Resources.X);
        _iprDataGridView.Columns.Insert((int)Columns.warranty, comboBoxColumn);

        comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.ticketType.ToString());
        comboBoxColumn.HeaderText = Resources.IprDetails_ConfigureComboBoxColumns_tt;
        comboBoxColumn.HeaderCell.ToolTipText = "ticketType";
        dataGridViewHelper.SetComboBoxChoicesStatic(ref comboBoxColumn, Columns.ticketType.ToString(), "I", "P", "R", "K", "Q");
        _iprDataGridView.Columns.Insert((int)Columns.ticketType, comboBoxColumn);

        comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.release.ToString());
        bool activeReleasesOnly = bool.Parse(ConfigurationManager.AppSettings.Get("ActiveReleasesOnly"));
        query = IPRDetailsConstants.SELECT_RELEASE + (activeReleasesOnly ? IPRDetailsConstants.RELEASE_COL_ACTIVE : IPRDetailsConstants.RELEASE_COL);
        dataGridViewHelper.SetComboBoxChoicesDataSource(ref comboBoxColumn, Columns.release.ToString(), query, dbConnection);
        _iprDataGridView.Columns.Insert((int)Columns.release, comboBoxColumn);

        /*
        comboBoxColumn = dataGridViewHelper.CreateComboBoxColumn(Columns.category.ToString());
        query = IPRDetailsConstants.SELECT_CATEGORY + IPRDetailsConstants.CATEGORY_COL;
        dataGridViewHelper.SetComboBoxChoicesDataSource(ref comboBoxColumn, Columns.category.ToString(), query, dbConnection);
        _iprDataGridView.Columns.Insert((int)Columns.category, comboBoxColumn);
        */

    }

我注释掉了他们的类别列以插入我自己的类别.我正在更新这个不是我开发的应用程序.

I commented out their column for categories to insert my own. I am updating this application which was not developed by me.

推荐答案

您似乎正在尝试将数据绑定到控件.设置源后,您必须绑定它.请参阅我所做的编辑.此链接提供了更多信息和示例https://support.microsoft.com/en-us/help/307860/asp-net-data-binding-overview

It looks like you are trying to bind your data to the control. After you set the source you have to bind it. See the edit I made. This link offers more information, and an example https://support.microsoft.com/en-us/help/307860/asp-net-data-binding-overview

    private void CustomComboBoxColumns(string filter)
        {
            DataGridViewComboBoxColumn ComboBoxColumn = new DataGridViewComboBoxColumn();
            DataTable dt;
            ComboBoxColumn.HeaderText = "category";
            ComboBoxColumn.DataPropertyName = "category";
            ComboBoxColumn.ReadOnly = false;
            ComboBoxColumn.MaxDropDownItems = 100;
            ComboBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            ComboBoxColumn.FlatStyle = FlatStyle.Flat;
            ComboBoxColumn.ValueMember = "category";
            ComboBoxColumn.DisplayMember = "category";
            _iprDataGridView.Columns.Insert(16, ComboBoxColumn);
            for (int i = 0; i < _iprDataGridView.Rows.Count; i++)
            {
                dt = GetDataForCategory(filter);
             ((DataGridViewComboBoxCell)_iprDataGridView.Rows[i].Cells[16]).DataSource = dt;
             ((DataGridViewComboBoxCell)_iprDataGridView.Rows[i].Cells[16]).DataBind(); //this edit
            }
        }

我也强烈推荐学习使用参数化查询来防止 sql 注入

这篇关于DataGridViewComboBoxCell 已填充但不会显示内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆