這裏我已經做了一個函數,從哪個表中完成自動完成的表名,需要自動完成的字段的名稱以及需要作爲目標的組合框。
試試下面的代碼:
public void AutoCompleteTextBox(string tableName, string fieldName, ComboBox combToAutoComp)
{
AutoCompleteStringCollection txtCollection = new AutoCompleteStringCollection();
DataTable dtAutoComp = Dal.ExecuteDataSetBySelect("Stored_Procedure", fieldName, tableName);
if (dtAutoComp.Rows.Count >= 0)
{
for (int count = 0; count < dtAutoComp.Rows.Count; count++)
{
txtCollection.Add(dtAutoComp.Rows[count][fieldName].ToString());
}
}
combToAutoComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
combToAutoComp.AutoCompleteSource = AutoCompleteSource.CustomSource;
combToAutoComp.AutoCompleteCustomSource = txtCollection;
}
這裏Dal.ExecuteDataSetBySelect
是我實現,其中我創建連接,命令和DataAdapter來調用存儲過程。你可以用你自己的實現替換它。欲瞭解更多請參考this link
thx好友,工作充分。雖然我決定經過更多的努力,採取WPF陡峭的學習曲線,並從頭開始使用WPF的項目 - 祝我好運:) – Sargola 2010-04-07 21:34:32
所有最好:) – HotTester 2010-04-08 07:35:25