c# - 在 DatagridView 中添加/删除/选择 ComboBox 的值

标签 c# datagridview datagridviewcombobox

我正在尝试为各种格式和字段的文件自动执行数据处理任务。我创建了一个程序来确定分隔文件的分隔符,并将文件的一部分加载到表单上的 DataGridView 中,以便用户可以在文件批量加载到 SQL 表之前确认文件的某些字段.该表将使用用户在数据网格的组合框中选择的一些字段名称即时创建。

这是我的目标,但我不确定我是否正确地解决了这个问题。

此时,我已经为组合框创建了一个 BindingSource ...

BindingSource bindingSource = new BindingSource();

这里我展示了选中文件的DataGridView,为数据文件中的每个字段添加了一列

    private void ShowDataGridView(string file, string delimiter, string[] fieldNames, string[] fieldLengths)
    {
        StreamReader fileReader = new StreamReader(file);
        if (bindingSource.Count == 0)
        {
            bindingSource.Add("FIRSTNAME");
            bindingSource.Add("LASTNAME");
            bindingSource.Add("ADDRESS1");
            bindingSource.Add("ADDRESS2");
            bindingSource.Add("CITY");
            bindingSource.Add("STATE");
            bindingSource.Add("ZIP");
            bindingSource.Add("COMPANY");
            bindingSource.Add("EMAIL");
            bindingSource.Add("");
        }           
        dataGridView1.Rows.Clear();
        dataGridView1.Columns.Clear();
        int count = 0;
        for (int i = 0; i < 17; i++)  //read 17 lines into datagridview for field confirmation, 17 lines just so happens to fill my datagridview nicely, last row will be combobox for field name selection
        {
            string[] fields = StringFunctions.Split(fileReader.ReadLine(), delimiter, Convert.ToString("\""));
            count = fields.Count();
            if (i == 0)
            {
               // Adding Column Header to DataGridView
                for (int x = 0; x < count; x++)
                {
                    DataGridViewTextBoxColumn columnDataGridTextBox = new DataGridViewTextBoxColumn();
                    columnDataGridTextBox.Name = fieldNames[x];
                    columnDataGridTextBox.HeaderText = fieldNames[x];
                    dataGridView1.Columns.Add(columnDataGridTextBox);
                }
            }
            dataGridView1.Rows.Add(fields);
        }

        for (int x = 0; x < count; x++)
        {
            DataGridViewComboBoxCell combobox = new DataGridViewComboBoxCell();             
            combobox.DataSource = bindingSource;
            dataGridView1[x, 16] = combobox;  //remember 17 rows added, combobox will be last row in datagridview
            combobox = null;
        }
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

        fileReader.Close();
        fileReader = null;
    }

好的,现在我有了一个数据 View ,以及数据所有字段的组合框。某些字段是强制性的,(BindingSource 字段名称)我希望用户能够从组合框中为数据列选择适当的字段名称。当用户从组合框中选择了一个字段时,我想从 BindingSource 中删除该字段名称,这样用户就不能为另一列选择相同的字段名称。其余字段将具有默认字段名称,例如(FirstName、Field2、LastName、Address1、Field5、Field6、Address2 等)

组合框是我遇到问题的地方:)

我已经搜索了代码片段并且取得了一些进展,但是我可以使用一些更了解 datagridview 事件以及如何处理它们的人的建议。我真的不知道我在做什么,只是往墙上扔东西,看它是否粘住。以下是我到目前为止尝试过的...

InitializeComponent();
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(DataGridViewEditingControlShowing);

private void DataGridViewEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        //here we will add the combo box's selected event changed 
        ComboBox cmbBox; 
        if (dataGridView1.CurrentCell is DataGridViewComboBoxCell)
        { 
            cmbBox = e.Control as ComboBox; 
            if (cmbBox == null)
                return; 
            cmbBox.SelectedIndexChanged += cmbBox_SelectedIndexChanged; 
        }
    }

    //This will display value of Select values of Combo Box 
    //which is DataGridView
    void cmbBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox cmbBox = (ComboBox)sender;
        if (cmbBox.SelectedValue != null)
        {
            MessageBox.Show(cmbBox.SelectedValue.ToString());  //testing
            bindingSource.Remove(cmbBox.SelectedValue);   //this removes it from the current combobox as well, no good.  Also run time error when clicking into a different combobox
        }          
    }

我希望我的描述足够多,发布的代码也足够多,可以让任何可能的代码大师 helper 了解我正在努力完成的事情。如果需要更多信息,请告诉我。非常感谢任何想法/解决方案。

谢谢!

最佳答案

您走在正确的轨道上,但为了实现这一点,我认为每个组合框都需要自己的数据源,以便可以单独操作它们。如果它们都共享相同的源,则它们不能有不同的内容,这就是您想要的(从组合框 A 中选择 X 应该将其从所有其他组合框中删除。)

在创建组合框的循环中,“克隆”数据源,使它们各自拥有自己的数据源。

关于c# - 在 DatagridView 中添加/删除/选择 ComboBox 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10319674/

相关文章:

c# - 支持移除项的线程安全集合

c# - 恢复 datagridview 控件中的单元格样式更改?

c# - Datagridview 在更改列组合框值时执行代码

c# - 如何在二维整数数组中找到最常见的整数?

c# - 将 Oracle Sql 查询中的 sql 变量值返回给 .NET 代码

c# - .Net Core ControllerBase Action Method JSON 序列化设置

winforms - DataGridViewCheckBoxColumn : how to update bound DataSource on property changed instead of on validation

c# - 如何在动态添加现在数据之前清除 dataGridView 行以前的数据....?

vb.net - 如何使 DataGridViewComboBoxColumn 的 ComboBox 接受用户新项目?

data-binding - 将组合框列数据绑定(bind)到每行的数据 GridView (不是整个列)