我想用数据库中的数据自动完成一个数据网格单元,并在选择项目后显示从数据库到该数据网格的多个行项目(具有那些标题)。目前,我只能从数据库中搜索填充了数据网格的唯一单元格的项目。我怎样才能达到我的解释? 我当前的代码如下:
//auto complete item name on datagrid
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
try
{
if (dataGridView1.CurrentCell.ColumnIndex==0)
{
SqlDataReader dreader;
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
AutoCompleteStringCollection autocomplete = new AutoCompleteStringCollection();
cmd.CommandText = "SELECT ItemName,ItemCode,Units,MainGroup,SubGroup from StockItems";
conn.Open();
dreader = cmd.ExecuteReader();
if (dreader.HasRows == true)
{
while (dreader.Read())
autocomplete.Add(dreader["ItemName"].ToString());
}
else
{
MessageBox.Show("Data not Found");
}
dreader.Close();
TextBox txtBusID = e.Control as TextBox;
if (txtBusID != null)
{
txtBusID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtBusID.AutoCompleteCustomSource = autocomplete;
txtBusID.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}