2011-08-29 77 views
1

我已經將我的DataGridView綁定正確地綁定到了我的數據表中,但是我試圖讓自動完成功能爲其中一個文本框列正常工作。自動完成正在工作,但我試圖將其限制在datagridview上只有一列。目前它要麼爲每一列都做,要麼根本沒有。有任何想法嗎?下面的代碼。在DataGridView Winform中的文本框自動完成

private void dataGridDetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     if (dataGridDetail.CurrentCell.ColumnIndex == 2) 
     { 
      var source = new AutoCompleteStringCollection(); 
      String[] stringArray = Array.ConvertAll<DataRow, String>(products.Select(), delegate(DataRow row) { return (String)row["code"]; }); 
      source.AddRange(stringArray); 

      TextBox prodCode = e.Control as TextBox; 
      if (prodCode != null) 
      { 
       prodCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 
       prodCode.AutoCompleteCustomSource = source; 
       prodCode.AutoCompleteSource = AutoCompleteSource.CustomSource; 

      } 
     } 
    } 

回答

2

您的if條件只是檢查用戶當前是否選擇了第三列。

是否要使所有欄可編輯?或者只是當前選定行中的一個單元格?編輯如何通過表單上的另一個按鈕觸發?在這種情況下,當編輯變爲活動狀態時,可以選擇任何單元格?

您需要索引到正確的列並將其設置爲啓用自動填充。

+0

是,通過自動完成使整個第三列可編輯。據我瞭解上面的代碼...每當一個單元格正在編輯datagridview觸發編輯控件顯示。然後我檢查確保只有第三列收到自動完成。 – user526549

+0

不,您檢查用戶是否已經點擊第三列數據網格中的單元格。 – kmcc049

+1

好吧解決了它。感謝您指點我正確的方向。我會在今天晚些時候發佈我的解決方案。 – user526549

5

嘗試釋放prodCode.AutoCompleteCustomSource,如果沒有正確的列:

private void dataGridDetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    if (dataGridDetail.CurrentCell.ColumnIndex == 2) 
    { 
     var source = new AutoCompleteStringCollection(); 
     String[] stringArray = Array.ConvertAll<DataRow, String>(products.Select(), delegate(DataRow row) { return (String)row["code"]; }); 
     source.AddRange(stringArray); 

     TextBox prodCode = e.Control as TextBox; 
     if (prodCode != null) 
     { 
      prodCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 
      prodCode.AutoCompleteCustomSource = source; 
      prodCode.AutoCompleteSource = AutoCompleteSource.CustomSource; 

     } 
    }else 
      prodCode.AutoCompleteCustomSource = null; 
} 
+0

prodCode超出範圍請修復它 – Hisham

0

Abuleen的建議是最好的!我做只是有點改善,因爲else語句行會拋出類型的錯誤變量不會在目前的情況下,從他的代碼存在

因此:

private void dataGridDetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    if(dataGridDetail.EditingControl.GetType()==typeof(DataGridViewTextBoxEditingControl)) 
    {  
     TextBox prodCode = e.Control as TextBox;   
     if (dataGridDetail.CurrentCell.ColumnIndex == 2) 
     {       
     var source = new AutoCompleteStringCollection(); 
     String[] stringArray = Array.ConvertAll<DataRow, String>(products.Select(), delegate(DataRow row) { return (String)row["code"]; }); 
     source.AddRange(stringArray); 

     TextBox prodCode = e.Control as TextBox; 
     if (prodCode != null) 
     { 
      prodCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 
      prodCode.AutoCompleteCustomSource = source; 
      prodCode.AutoCompleteSource = AutoCompleteSource.CustomSource; 

     } 
     } 
     else { prodCode.AutoCompleteCustomSource = null; } 
    } 
}