DataGridViewComboBoxColumn未显示DataSource

时间:2018-02-04 08:48:29

标签: c# winforms datagridview binding datasource

我的代码:

private void InitGrid(DataTable dataTable, ref DataGridView grid, bool resetColumns)
{
    if (grid.Columns.Count > 0)
    {
        if (resetColumns)
        {
            grid.Columns.Clear();
            grid.DataSource = null;
        }
        else
        {
            return;
        }
    }
    DataGridViewColumn column;
    grid.AutoGenerateColumns = false;
    // apply entire data from table to grid.
    grid.DataSource = dataTable;

    // set column properties according to their type (combo, text, etc.)
    foreach (DataColumn col in dataTable.Columns)
    {
        if (m_ColumnTypes[col.ColumnName].Equals("COMBO"))
        {
            // get query number to load specific column's data
            string adminQuery = Server.LoadData("633", new List<string>() { col.ColumnName, m_TableFullName }).Rows[0][0].ToString();
            column = new DataGridViewComboBoxColumn();
            string queryParam = Controller.MainController.User;
            switch (col.ColumnName.ToUpper())
            {
                case "FIELD1NAME":
                    (column as DataGridViewComboBoxColumn).DisplayMember = col.ColumnName;
                    (column as DataGridViewComboBoxColumn).ValueMember = "field1id";
                    break;
                case "FIELD2NAME":
                    (column as DataGridViewComboBoxColumn).DisplayMember = col.ColumnName;
                    (column as DataGridViewComboBoxColumn).ValueMember = "field2id";
                    queryParam = string.Concat("select column_value from table(fn_mgr_workspaces('", Controller.MainController.User, "'))");
                    break;
                case "PRIORITY":
                    (column as DataGridViewComboBoxColumn).DisplayMember = col.ColumnName;
                    (column as DataGridViewComboBoxColumn).ValueMember = col.ColumnName;
                    queryParam = "10";    
                    break;
            }
            (column as DataGridViewComboBoxColumn).DataSource = Server.LoadData(adminQuery, new List<string>() { queryParam });
        }
        else
        {
            column = new DataGridViewTextBoxColumn() { AutoSizeMode = m_ColumnTypes[col.ColumnName].Equals("TAGS") ? DataGridViewAutoSizeColumnMode.Fill : DataGridViewAutoSizeColumnMode.AllCells, ReadOnly = true };
        }

        column.Name = col.ColumnName.ToUpper();
        column.HeaderText = m_ColumnNameTranslations[col.ColumnName.ToUpper()];
        column.DataPropertyName = column is DataGridViewComboBoxColumn ? (column as DataGridViewComboBoxColumn).ValueMember : col.ColumnName;

        grid.Columns.Add(column);
    }

    // after setting columns. going over every row and trying to filter out the datasource of field2. this is where my problem is
    foreach (DataGridViewRow dgvr in grid.Rows)
    {
        if (!dgvr.IsNewRow)
        {
            string wsId = dgvr.Cells["filed1id"].Value.ToString();
            DataTable filteredData = ((dgvr.Cells["field2name"] as DataGridViewComboBoxCell).DataSource as DataTable).Select("thefieldname = " + wsId).CopyToDataTable();
            DataView dv = filteredData.DefaultView;
            dv.Sort = "field2name";
            (dgvr.Cells["field2name"] as DataGridViewComboBoxCell).DataSource = null;
            (dgvr.Cells["field2name"] as DataGridViewComboBoxCell).DisplayMember = "field2name";
            (dgvr.Cells["field2name"] as DataGridViewComboBoxCell).ValueMember = "field2id";
            (dgvr.Cells["field2name"] as DataGridViewComboBoxCell).DataSource = dv.ToTable();
        }
    }
}

(出于安全目的,字段名称和一些对象名称被加扰)

我的问题:

作为ComboboxColumn的field2的DataSource不会更改。它总是向我显示整个数据,而不是我尝试为其设置的过滤数据。 即使我将其取消并将其重新设置为过滤后的数据,它也无法正常工作。

我想要实现的是,field2列中的DataSource应该依赖于grid1每行的field1数据。

我尝试为其他列设置数据源,而不是为field2设置数据源,然后在遍历每一行并在field2上设置过滤后的数据源但它完全为空,就好像为每一行设置的数据源都没有&#39完全没有工作。

我做错了什么。我该怎么办呢?

1 个答案:

答案 0 :(得分:1)

DataGridView上的EditControlShowing事件将允许您完成此操作。

为此方法添加事件处理程序。

private void grid_EditingControlShowing(object sender, 
    DataGridViewEditingControlShowingEventArgs e)
{
    if ( grid.CurrentCell.ColumnIndex == dgvr.Columns["field2name"].Index )
    {

         // Get Combobox
         ComboBox combo = e.Control as ComboBox;

         // Get Other Column's Value
         string wsId = grid.Rows[grid.CurrentCell.RowIndex].Cells["filed1id"].Value.ToString();

         // Get Filtered Data Based Off Of Other Column's Value
         DataTable filteredData = (combo.DataSource as DataTable).Select("thefieldname = " + wsId).CopyToDataTable();
         DataView dv = filteredData.DefaultView;
         dv.Sort = "field2name";


         // Rebind
         combo.DataSource = null;
         combo.DisplayMember = "field2name";
         combo.ValueMember = "field2id";
         combo.DataSource = dv.ToTable();
    }
}