2012-04-25 73 views
3

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

这是我的目标,但我不确定我是否正确接近问题。

在这一点上,我创建了一个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; 
    } 

好的,所以现在我有一个数据视图,并为数据的所有领域的组合框。某些字段是强制性的(BindingSource字段名称)我希望用户能够从组合框中为列数据选择适当的字段名称。当用户从组合框中选择一个字段时,我想从BindingSource中删除该字段名称,因此用户不能为另一列选择相同的字段名称。其余字段将有默认的字段名称如(名字,字段2,名字,地址1字段5,字段6 1,地址等)

组合框就是我

我搜索过的代码片断有问题:)我正在取得一些进展,但我可以使用一些对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 
     }   
    } 

我希望我已经足够的描述,并张贴足够的代码提供任何可能的代码大师的助手为我想要实现的感觉。如果需要更多信息,请告诉我。任何想法/解决方案非常感谢。

谢谢!

+0

对于完整的问题+1,但如果您更详细地描述您获得什么行为以及哪些行不通,这将有所帮助。 SelectedIndexChanged事件是否触发?如果是这样,你可以使用它来“消息”其他组合框从列表中删除选定的元素... – 2012-04-25 16:22:49

+0

谢谢,SelectedIndexChanged正在发射,我得到的消息框弹出窗口告诉我,我选择了哪个字段。但是,然后我在组合框中选择的字段消失了,另一个消息框弹出一个空值,因为我在下一行代码中从BindingSource中删除值。所以就是这样。然后,当我点击另一列时,我得到一个处理SelectedIndex的运行时错误。我想我会首先使用您为每个组合框创建不同绑定源的建议,并从这一点来看看我可以遇到什么麻烦。再次感谢! – Caddy 2012-04-25 16:40:46

回答

1

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

在你创建组合框的循环中,克隆“数据源,以便他们各自拥有自己的数据源。

+0

谢谢戴夫,这听起来像个好主意。我会努力解决这个问题,并在我的问题中添加更多关于失败的描述。感谢这样一个迅速的反应,如果我不是一个noob谁他们不允许投票,我会投你一票:) – Caddy 2012-04-25 16:34:02

+0

嘿戴夫,如果你还在身边,你会碰巧有一个工作的例子克隆数据源?我一直无法解决这个问题,而且我坚持了这一点。 – Caddy 2012-05-11 13:14:09

+0

在用于创建列的循环中创建多个数据源。你可以将它们添加到一个通用的'Dictionary '集合中,以便稍后可以解决它们。使用循环计数器作为键。 – 2012-05-11 13:31:41

相关问题