2012-04-25 63 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

相關問題