我正在尝试为我的商店创建一个桌面应用程序。
我的应用程序当前正在使用AutoComplete项目,如此屏幕截图显示:
现在我想将该单元格更改为使用LIKE
函数自动完成。
例如:我的项目列表包含
["book", "brush", "pen", "samsung note 1", "samsung note 2", "samsung note 3", "samsung note 4"]
当我输入"注意"时,自动填充功能会建议或显示一个列表供我选择该项目。
但该列表仅包含" samsung note 1"," samsung note 2"," samsung note 3"," samsung note 4&#34 ;,因为这个项目有"注意"。
这是我的代码,没有LIKE
功能的自动填充功能
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
Connection.ConnectionClose();
Connection.ConnectionOpen();
//TODO fill Autocomplete list
string queryItem = "SELECT * FROM ITEM";
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;
}
}
e.Control.KeyPress -= new KeyPressEventHandler(AllowNumber_KeyPress);
if (((DataGridView)sender).CurrentCell.ColumnIndex == 0) //Assuming 0 is the index of the ComboBox Column you want to show
{
TextBox code = e.Control as TextBox;
code.CharacterCasing = CharacterCasing.Upper;
if (code != null)
{
code.Leave -= new EventHandler(cb_IndexChanged);
// now attach the event handler
code.Leave += new EventHandler(cb_IndexChanged);
}
}
}
我希望更改自动填充功能,以便能够使用" LIKE "。
在Query中使用LIKE进行更新。
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
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() + "%'";
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;
}
}
}
并收到此错误
System.NullReferenceException:未将对象引用设置为实例 一个对象。在 inventory.Market.SalesOrder.dataGridView1_EditingControlShowing(对象 sender,DataGridViewEditingControlShowingEventArgs e)in C:\用户\ VPC2 \文档\库存\库存\市场\ SalesOrder.cs:行 922在 System.Windows.Forms.DataGridView.OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs E)
我发现错误来自dataGridView1.Rows[currentRow].Cells[0].Value.ToString()
看起来我在输入datagridview的单元格时无法获得价值。
或者在课堂private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
我正在努力创造这样的东西。https://www.codeproject.com/Articles/251110/AutoComplete-TextBox-with-substing-search-similar
但不是在textBox而是在datagridview的单元格中