我正在尝试创建一个自动完成功能,能够推荐一个项目,其中该项目包含我输入的内容。
像这样但我在文本框中实现此自动填充,而不是在dategridview的单元格中。
这是我的代码..
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
string gkey = "";
Connection.ConnectionClose();
Connection.ConnectionOpen();
int currentRow = dataGridView1.CurrentRow.Index;
//TODO fill Customer list
//string queryItem = "SELECT * FROM ITEM WHERE CODE LIKE '%" + dataGridView1.Rows[currentRow].Cells[0].Value.ToString() + "%'";
string queryItem ="";
//Console.WriteLine("asdf = " + );
try
{
gkey = dataGridView1.Rows[currentRow].Cells[0].Value.ToString();
queryItem = "SELECT * FROM ITEM WHERE CODE LIKE '*" + gkey + "*'";
}
catch (Exception ex)
{
gkey = "";
queryItem = "SELECT * FROM ITEM ";
}
Console.WriteLine("qyery = "+queryItem);
Connection.command = new OleDbCommand(queryItem, Connection.conn);
Connection.command.CommandType = CommandType.Text;
AutoCompleteStringCollection kode = new AutoCompleteStringCollection();
reader = Connection.command.ExecuteReader();
if (reader.HasRows == true)
{
while (reader.Read())
{
kode.Add(reader["code"].ToString());
}
}
else
{
MessageBox.Show("Data not Found");
}
reader.Close();
//ComboBox txtBusID = e.Control as ComboBox;
TextBox kodeTxt = e.Control as TextBox;
if (kodeTxt != null)
{
kodeTxt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
kodeTxt.AutoCompleteCustomSource = kode;
kodeTxt.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
}
如何实现像上面的图像一样的自动完成,这个自动完成可以使用" LIKE",在sql查询中类似
例如..
此图片...当我输入" SAM"时,该列表会出现,但是当我输入" AM"该列表不再是明显的。
答案 0 :(得分:0)
您必须覆盖以下方法:
dataGridView1_EditingControlShowing
以下是进一步的解释:http://csharp.net-informations.com/datagridview/autogridview.htm