我試圖做這個錯誤specified cast is not valid
autocomplete
對於datagridviewtextboxcell
..我附上了代碼快照以及stack trace
。我在這裏錯過了什麼?在DataGridView Winform中進行文本框自動完成時出錯
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox prodCode = e.Control as TextBox;
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
var source = new AutoCompleteStringCollection();
String[] stringArray = newDB.CustomerTbls.Where(s => s.Status == "Active" & s.Type == "Customer").Select(s => s.Name).ToArray<string>();
source.AddRange(stringArray);
if (prodCode != null)
{
prodCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
prodCode.AutoCompleteCustomSource = source;
prodCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
else
prodCode.AutoCompleteCustomSource = null;
}
你的問題類似於這個http://stackoverflow.com/q/7225290/122005。你有沒有嘗試移動控件實例TextBox prodCode = e.Control作爲TextBox;在if條件中,在檢查之前它不是null? – chridam
@chridam:代碼與您指定的鏈接相同。我試着移動控件實例TextBox prodCode = e.Control作爲TextBox;在if條件內,但它仍然給出相同的錯誤。任何其他的消化? –