当用户输入文本框以编写查询时,我试图让我的文本框自动完成。这与SQL Server Management Studio的工作方式类似,并在您键入箭头或单击表名或列名时提供选项。以下是我的以下代码。
public void loadData()
{
var myConnection = new SqlConnection(DBConnectionBox.Text);
myConnection.Open();
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
string query = @"Select distinct [name] from [INFORMATION_SCHEMA.TABLES]";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
while (dr.Read())
namesCollection.Add(dr["name"].ToString());
}
dr.Close();
myConnection.Close();
ManualQueryBox.AutoCompleteMode = AutoCompleteMode.Append;
ManualQueryBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
ManualQueryBox.AutoCompleteCustomSource = namesCollection;
}
private void ManualQueryBox_KeyUp(object sender, KeyEventArgs e)
{
loadData();
}
这是我用来抓取文本框中的内容并执行它的代码。
private void ExecuteBtn_Click(object sender, EventArgs e)
{
this.ClientInfoDGV.DataSource = null;
this.ClientInfoDGV.Rows.Clear();
var myConnection = new SqlConnection(DBConnectionBox.Text);
var ManualCmd = new SqlCommand(ManualQueryBox.Text);
ManualCmd.Connection = myConnection;
ManualCmd.CommandType = CommandType.Text;
SqlDataAdapter SqlAdap = new SqlDataAdapter(ManualCmd);
DataTable MQRecord = new DataTable();
SqlAdap.Fill(MQRecord);
ClientInfoDGV.DataSource = MQRecord;
}
我之前从未做过自动填充,但环顾四周我看到了一些asp.net AJAX控件工具包,但我不完全确定这一切是如何工作的。欢迎任何帮助。
更新为自动填充代码
public void loadData()
{
var myConnection = new SqlConnection(DBConnectionBox.Text);
myConnection.Open();
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
string query = @"Select distinct [Id] from [Clients]";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
while (dr.Read())
namesCollection.Add(dr["id"].ToString());
}
dr.Close();
myConnection.Close();
ManualQueryBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
ManualQueryBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
ManualQueryBox.AutoCompleteCustomSource = namesCollection;
}
答案 0 :(得分:2)
尝试将自动完成模式更改为suggestappend:
ManualQueryBox.AutoCompleteMode=AutoCompleteMode.SuggestAppend;
我认为只要您加载一次(可能是表单加载)并指定文本框的来源,您每次都必须在加密事件中加载数据。