简体   繁体   English

将DataGridView中的ComboBox的值匹配到另一个单元格

[英]Match the value of ComboBox inside a DataGridView to another cell

I have a DataGridView with a ComboBox column. 我有一个带有ComboBox列的DataGridView The ComboBox gets filled with a specific list of names and they are identical for every row in the DataGridView . ComboBox充满了特定的名称列表,并且对于DataGridView每一行都是相同的。

After I fill my DataGridView , I would like to do the following: 填充DataGridView ,我想执行以下操作:

I want every Combo's value to be set to the corresponding row's “Table Columns” value, if exists. 我希望将每个Combo的值设置为相应行的“表列”值(如果存在)。

在此输入图像描述

Ie in the picture above I want the value of the first Combo to be “id” (if it contains an item named “id”), the second to be “firstname”, etc. 即在上图中,我希望第一个Combo的值为“ id”(如果它包含名为“ id”的项),第二个为“ firstname”,依此类推。

If the value is not found, it should not select any value in the ComboBox . 如果找不到该值,则不应在ComboBox选择任何值。

Update : My current code that is not working 更新 :我当前的代码不起作用

private void Mappings_Load(object sender, EventArgs e)
{       
    dgv.DataSource = tableColumns.Select(x => new { Value = x }).ToList();
    dgv.Columns[0].HeaderText = "Table Columns";
    DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn();
    comboColumn.HeaderText = "File Columns";
    foreach (var item in fileColumns)
        comboColumn.Items.Add(item);
    dgv.Columns.Add(comboColumn);
    foreach(DataGridViewRow row in dgv.Rows)
    {
        string tableColumnValue = row.Cells[0].Value.ToString();
        row.Cells[0].Value = tableColumnValue;           
    }
}

A while ago I did a similar thing: 前一阵子我做了类似的事情:

  1. Recognize which fields are of type enum 识别哪些字段属于enum类型
  2. For the fiels of type enum , show a ComboBox in the cell of the DataGridView (containing all the possible values of the enum ) instead of the simple text field. 对于enum类型的字段,请在DataGridView的单元格中显示一个ComboBox (包含enum所有可能值),而不是简单的文本字段。

I think you only need to change certain parts of the code to transform it to what you are trying to achieve: 我认为您只需要更改代码的某些部分即可将其转换为您要实现的目标:

public partial class TableView<CollectionType, ItemType> : Form
{

    public TableView(CollectionType elements)
    {

        InitializeComponent();

        // custom:
        this.DataGridView.DataSource = elements;
        AdaptColumnsToColumnValueType();

    }

    private void AdaptColumnsToColumnValueType()
    {
        for (int columnIndex = 0; columnIndex < this.DataGridView.Columns.Count; columnIndex++)
        {
            var column = (DataGridViewColumn)this.DataGridView.Columns[columnIndex];
            if (column.ValueType.IsEnum)
            {
                ReplaceColumnInDatagridView(
                    oldColumn: column,
                    newColumn: CreateComboBoxWithEnums(column));
            }
        }
    }

    private void ReplaceColumnInDatagridView(DataGridViewColumn oldColumn, DataGridViewColumn newColumn)
    {
        int columnIndex = oldColumn.Index;
        this.DataGridView.Columns.Remove(oldColumn);
        this.DataGridView.Columns.Insert(columnIndex, newColumn);
    }

    private DataGridViewComboBoxColumn CreateComboBoxWithEnums(DataGridViewColumn replacedEnumColumn)
    {
        var comboboxColumn = new DataGridViewComboBoxColumn();
        comboboxColumn.DataSource = Enum.GetValues(replacedEnumColumn.ValueType);
        comboboxColumn.DataPropertyName = replacedEnumColumn.DataPropertyName;
        comboboxColumn.Name = replacedEnumColumn.Name;
        return comboboxColumn;
    }

}

you can use the DataGridViews RowsAdded event. 您可以使用DataGridViews行属性事件。

Edit: cmb is your ComboBoxColumn 编辑:cmb是您的ComboBoxColumn

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) {
   //Check if comboboxcolumn contains the value                
   if(cmb.Items.Contains(dataGridView1.Rows[e.RowIndex].Cells[0].Value)) {
   //Set the value
     dataGridView1.Rows[e.RowIndex].Cells["cmb"].Value = dataGridView1.Rows[e.RowIndex].Cells[0].Value;
         }
    }

Trace the change .. you almost did it 跟踪更改.. 您几乎做到了

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] tableColumns = new string[] { "A", "B", "C", "D" };

        string[] fileColumns = new string[] { "A", "B", "C", "X" };

        dgv.DataSource = tableColumns.Select(x => new { Value = x }).ToList();

        dgv.Columns[0].HeaderText = "Table Columns";
        DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn();
        comboColumn.HeaderText = "File Columns";
        foreach (var item in fileColumns)
            comboColumn.Items.Add(item);
        dgv.Columns.Add(comboColumn);

        foreach (DataGridViewRow row in dgv.Rows)
        {
            string tableColumnValue = row.Cells[0].Value.ToString();

            //Change is here
            if (fileColumns.Any(i => i == tableColumnValue))
            {
                row.Cells[1].Value = tableColumnValue;
            }
            //end change
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM