1 回答
TA贡献1111条经验 获得超0个赞
我已经使用其他代码(见下文)使它工作,现在我的问题是,如果我输入的字母是数据库中显示的数据的第一个字母,我需要一些东西,当我输入一些东西时,我需要一些东西,比如我输入了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 回答
- 0 关注
- 179 浏览
添加回答
举报