[英]Auto complete in datagridview in C#
我一直在尋找我的問題的答案。 但是我找不到滿意的答案。 現在這是我的問題。
我在C#中有一個datagridview,它未綁定到任何數據源。 我想讓用戶在其中一列中輸入數據。 我需要做的是為用戶提供自動完成功能。
自動完成的數據來自數據庫。 我可以很好地解決這個問題,但是我的問題是建議應該取決於用戶輸入的字符。 例如,如果用戶輸入“ g”,而我的數據庫查詢返回“ garlic”,則自動完成功能應顯示該信息。
對於普通的文本框來說,這非常容易。 但是datagridview的問題在於,當用戶鍵入字符時,我無法讀取它們。在完成編輯后,將觸發單元格值更改事件。 數據庫中的數據太多,無法立即添加到自動完成源中。 因此,我必須解決要獲得用戶類型的問題並根據該問題生成源。
有什么辦法可以完成這項任務? 請幫忙。
以下是我最近使用的代碼之一,但無濟於事,它無法完成工作:
private void Form2_Load(object sender, EventArgs e)
{
string str = @"Data Source=MEDIXPC197;Initial Catalog=Inventory;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("SELECT Product Code from tblmaster", con);
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
string text = dataGridView1.Columns[4].HeaderText;
if (text.Equals("Product Code")) ;
{
TextBox auto_text = e.Control as TextBox;
if (auto_text != null)
{
auto_text.AutoCompleteMode = AutoCompleteMode.Suggest;
auto_text.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection sc = new AutoCompleteStringCollection();
add_items(sc);
auto_text.AutoCompleteCustomSource = sc;
}
}
}
public void add_items(AutoCompleteStringCollection column)
{
column.Add("test1");
編輯:
我已經使用其他代碼使其工作(請參見下文),現在我的問題是建議僅顯示我鍵入的字母是否是從數據庫中顯示的數據的第一個字母,我在輸入內容時需要一些內容,例如,我輸入1,建議將顯示“ 1,typo1,tester1tester,111”
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Left = (MdiParent.ClientRectangle.Width - Width) / 2;
//Top = (MdiParent.ClientRectangle.Height - Height) / 2;
DataGridViewTextBoxColumn dgvslno = new DataGridViewTextBoxColumn();
dgvslno.HeaderText = "Item Code";
dgvslno.Width = 40;
dataGridView1.Columns.Add(dgvslno);
DataGridViewTextBoxColumn dgvpro = new DataGridViewTextBoxColumn();
dgvpro.HeaderText = "Product Name";
dgvpro.Width = 40;
dataGridView1.Columns.Add(dgvpro);
}
public AutoCompleteStringCollection AutoCompleteLoad()
{
SqlConnection conn = new SqlConnection(@"Data Source=MEDIXPC197;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select ProductCode from tblmaster", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
AutoCompleteStringCollection mycoll = new AutoCompleteStringCollection();
while (dr.Read())
{
mycoll.Add(dr["ProductCode"].ToString());
}
return mycoll;
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int column = dataGridView1.CurrentCell.ColumnIndex;
string headerText = dataGridView1.Columns[column].HeaderText;
if (headerText.Equals("Item Code"))
{
TextBox tb = e.Control as TextBox;
if(tb != null)
{
tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
tb.AutoCompleteCustomSource = AutoCompleteLoad();
tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
else
{
TextBox tb = e.Control as TextBox;
if (tb !=null)
{
tb.AutoCompleteMode = AutoCompleteMode.None;
}
}
}
}
}
我已經使用其他代碼使其工作(請參見下文),現在我的問題是建議僅顯示我鍵入的字母是否是從數據庫中顯示的數據的第一個字母,我在輸入內容時需要一些內容,例如,我輸入1,建議將顯示“ 1,typo1,tester1tester,111”
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Left = (MdiParent.ClientRectangle.Width - Width) / 2;
//Top = (MdiParent.ClientRectangle.Height - Height) / 2;
DataGridViewTextBoxColumn dgvslno = new DataGridViewTextBoxColumn();
dgvslno.HeaderText = "Item Code";
dgvslno.Width = 40;
dataGridView1.Columns.Add(dgvslno);
DataGridViewTextBoxColumn dgvpro = new DataGridViewTextBoxColumn();
dgvpro.HeaderText = "Product Name";
dgvpro.Width = 40;
dataGridView1.Columns.Add(dgvpro);
}
public AutoCompleteStringCollection AutoCompleteLoad()
{
SqlConnection conn = new SqlConnection(@"Data Source=MEDIXPC197;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select ProductCode from tblmaster", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
AutoCompleteStringCollection mycoll = new AutoCompleteStringCollection();
while (dr.Read())
{
mycoll.Add(dr["ProductCode"].ToString());
}
return mycoll;
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int column = dataGridView1.CurrentCell.ColumnIndex;
string headerText = dataGridView1.Columns[column].HeaderText;
if (headerText.Equals("Item Code"))
{
TextBox tb = e.Control as TextBox;
if(tb != null)
{
tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
tb.AutoCompleteCustomSource = AutoCompleteLoad();
tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
else
{
TextBox tb = e.Control as TextBox;
if (tb !=null)
{
tb.AutoCompleteMode = AutoCompleteMode.None;
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.