在数据网格视图中添加/删除/选择组合框的值
本文关键字:选择 删除 组合 添加 数据网 数据 网格 视图 | 更新日期: 2023-09-27 18:32:18
我正在尝试自动执行各种格式和字段的文件的数据处理任务。 我创建了一个程序来确定分隔文件的分隔符,并将文件块加载到窗体上的 DataGridView 中,以便用户可以在将文件大容量加载到 SQL 表之前确认文件的某些字段。 将使用用户在数据网格的组合框中选择的一些字段名称动态创建表。
这是我的目标,但我不确定我是否正确地解决了这个问题。
在这一点上,我已经为组合框创建了一个绑定源...
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;
}
好的,现在我有了数据的视图,以及数据所有字段的组合框。 某些字段是必填字段(BindingSource 字段名称)我希望用户能够从组合框中为数据列选择适当的字段名称。 当用户从组合框中选择一个字段时,我想从 BindingSource 中删除该字段名称,因此用户无法为另一列选择相同的字段名称。 其余字段将具有默认字段名称,例如(名字,字段2,姓氏,地址1,字段5,字段6,地址2等)
组合框是我遇到问题的地方:)
我已经搜索了代码片段,并且取得了一些进展,但我可以从更好地掌握 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
}
}
我希望我已经有足够的描述性并发布了足够的代码,让任何可能的代码大师助手都能感受到我想要完成的任务。 如果需要更多信息,请告诉我。 任何想法/解决方案都非常感谢。
谢谢!
您走在正确的轨道上,但要使其正常工作,我认为每个组合框都需要自己的数据源,因此可以单独操作它们。 如果它们都共享相同的源,则它们不能具有不同的内容,这是您想要的(从组合框 A 中选择 X 应该从所有其他组合框中删除它。
在创建组合框的循环中,"克隆"数据源,以便每个数据源都有自己的数据源。