0

这是我的FormLoad-event 上的代码

SqlDataReader dReader;
SqlConnection conn = new SqlConnection(MyClass.GlobalConn());
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;

if (radioName.checked==true)
{           
    cmd.CommandText = "select RTRIM(Person_name) from MyTable order by Person_name";
}
else
{
    cmd.CommandText = "select RTRIM(Person_number) from MyTable order by Person_number";
}

dReader = cmd.ExecuteReader();

if (dReader.HasRows == true)
{
    while (dReader.Read())
        namesCollection.Add(dReader[0].ToString());
}
else
{
    MessageBox.Show("Data not found");
}

dReader.Close();

tPT.AutoCompleteMode = AutoCompleteMode.Suggest;
tPT.AutoCompleteSource = AutoCompleteSource.CustomSource;
tPT.AutoCompleteCustomSource = namesCollection;
conn.Close();  

我想要两个选项来在输入文本框时填充自动完成文本框:

  1. 按名字
  2. 按编号

但是它不起作用。我已经在我的事件中尝试过这个TextChanged,但没有运气。有人可以帮助我吗?

4

1 回答 1

0

而不是使用可选的 AutoCompleteSource 将所有数据更新到一个源中。这不会有任何问题,因为该人不必选择选项,而是他将通过决定他的选项来键入。

SqlDataReader dReader;
SqlConnection conn = new SqlConnection(MyClass.GlobalConn());
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text; 
cmd.CommandText = "select RTRIM(Person_name) from MyTable order by Person_name";
dReader = cmd.ExecuteReader();
cmd.CommandText = "select RTRIM(Person_number) from MyTable order by Person_number";
dReader = cmd.ExecuteReader();
if (dReader.HasRows == true)
{
    while (dReader.Read())
        namesCollection.Add(dReader[0].ToString());
}
else
{
    MessageBox.Show("Data not found");
}

dReader.Close();

tPT.AutoCompleteMode = AutoCompleteMode.Suggest;
tPT.AutoCompleteSource = AutoCompleteSource.CustomSource;
tPT.AutoCompleteCustomSource = namesCollection;
conn.Close();

或者,如果您想使用较旧的选项,

namesCollection.RemoveAll();
SqlDataReader dReader;
SqlConnection conn = new SqlConnection(MyClass.GlobalConn());
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text; 
namesCollection.RemoveAll();
if (radioName.checked==true)
{
    cmd.CommandText = "select RTRIM(Person_name) from MyTable order by Person_name";
}
else
{
    cmd.CommandText = "select RTRIM(Person_number) from MyTable order by Person_number";
}
dReader = cmd.ExecuteReader();

if (dReader.HasRows == true)
{
    while (dReader.Read())
        namesCollection.Add(dReader[0].ToString());
}
else
{
    MessageBox.Show("Data not found");
}

dReader.Close();

tPT.AutoCompleteMode = AutoCompleteMode.Suggest;
tPT.AutoCompleteSource = AutoCompleteSource.CustomSource;
tPT.AutoCompleteCustomSource = namesCollection;
conn.Close();

注意

namesCollection.RemoveAll();

于 2014-03-20T06:05:07.837 回答