我正在嘗試爲各種格式和字段的文件自動執行數據處理任務。我創建了一個程序來確定分隔文件的分隔符,並將該文件的一部分加載到窗體上的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
}
}
我希望我已經足夠的描述,並張貼足夠的代碼提供任何可能的代碼大師的助手爲我想要實現的感覺。如果需要更多信息,請告訴我。任何想法/解決方案非常感謝。
謝謝!
對於完整的問題+1,但如果您更詳細地描述您獲得什麼行爲以及哪些行不通,這將有所幫助。 SelectedIndexChanged事件是否觸發?如果是這樣,你可以使用它來「消息」其他組合框從列表中刪除選定的元素... – 2012-04-25 16:22:49
謝謝,SelectedIndexChanged正在發射,我得到的消息框彈出窗口告訴我,我選擇了哪個字段。但是,然後我在組合框中選擇的字段消失了,另一個消息框彈出一個空值,因爲我在下一行代碼中從BindingSource中刪除值。所以就是這樣。然後,當我點擊另一列時,我得到一個處理SelectedIndex的運行時錯誤。我想我會首先使用您爲每個組合框創建不同綁定源的建議,並從這一點來看看我可以遇到什麼麻煩。再次感謝! – Caddy 2012-04-25 16:40:46