winform中gridview自动完成的文本框事件

时间:2015-02-24 16:07:48

标签: c# sql winforms datagridview

我一直在网上查找用于文本框的正确事件,以自动生成像google.com这样的建议,因此我不必输入整个单元格值来查找我的内容。我正在寻找。目前,我有一个按钮,当点击它时,它会根据我的要求过滤网格,但不仅仅是使用文本框进行过滤。我知道那里有Keypress,textchanged等等。我尝试了不同的事件来查看datagridview是否按照我的类型进行过滤。没有成功。按键事件不会在我键入时进行过滤。 textchanged事件不允许我输入文本框。那我该怎么做呢?

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SampleARDUserGroup
{
    public partial class FirstPage : Form
    {
        SqlConnection con;
        SqlDataAdapter adap;
        DataSet ds;
        SqlCommandBuilder cmbl;
        public FirstPage()
        {
            InitializeComponent();
        }

        private void FirstPage_Load(object sender, EventArgs e)
        {


            try
            {
                con = new SqlConnection();
                con.ConnectionString = "My Data Source";
                con.Open();
                adap = new SqlDataAdapter("select RowID as 'ID',UserID as 'User ID', FirstName as 'First Name', LastName as 'Last Name', email as 'E-mail' from JoshTestTable", con);
                ds = new System.Data.DataSet();
                adap.Fill(ds, "User_Details");
                DGV1.DataSource = ds.Tables[0];
                DGV1.Columns[0].Visible = false;
                DGV1.Columns[2].Visible = false;
                DGV1.Columns[3].Visible = false;
                DGV1.Columns[4].Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            con = new SqlConnection();
            con.ConnectionString = "My Data Source";
            SqlDataAdapter sda = new SqlDataAdapter("Select UserID,FirstName,LastName,Email FROM JoshTestTable where UserID like '%" + txtSearch.Text.Trim() + "%' OR FirstName like '%" + txtSearch.Text.Trim() + "%' OR FirstName like '%" + txtSearch.Text.Trim() + "%' OR LastName like '%" + txtSearch.Text.Trim() + "%' OR Email like '%" + txtSearch.Text.Trim() + "%'", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            DGV1.DataSource = dt;
            DGV1.Columns[1].Visible = false;
            DGV1.Columns[2].Visible = false;
            DGV1.Columns[3].Visible = false;

        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AddDeleteUsers f2 = new AddDeleteUsers();
            f2.ShowDialog(); 
        }

        private void AddUsersToGroupsLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AddUsersToGroups f3 = new AddUsersToGroups();
            f3.ShowDialog(); 
        }

        private void AddGroupsToUsersLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AddGroupsToUsers f4 = new AddGroupsToUsers();
            f4.ShowDialog(); 
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();

            SqlConnection con = new SqlConnection();
            con.ConnectionString = "My Data Source";
            con.Open();
            String query = "select UserID from JoshTestTable  where UserID like '%" + txtSearch.Text + "%'";
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader rr = cmd.ExecuteReader();
            rr.Read();
            while (rr.Read())
                namesCollection.Add(rr["UserID"].ToString());
            rr.Close();
            txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtSearch.AutoCompleteCustomSource = namesCollection;
        }      
    }
}

1 个答案:

答案 0 :(得分:0)

因为显然,where事件中查询中的txtSearch_TextChanged条件应该是那样的

String query = 
    "select UserID from JoshTestTable where UserID like '%" +
    txtSearch.Text.Trim() + "%' OR FirstName like '%" +
    txtSearch.Text.Trim() + "%' OR FirstName like '%" +
    txtSearch.Text.Trim() + "%' OR LastName like '%" +
    txtSearch.Text.Trim() + "%' OR Email like '%" +
    txtSearch.Text.Trim() + "%'";

作为您的btnSearch_Click事件。或者只是您可以从单个点查询数据库,如:

private void txtSearch_TextChanged(object sender, EventArgs e)
{
    btnSearch_Click(null, null);
}

更新

您必须使用键入的内容填充自定义来源。例如,我在我的示例中使用了Last Name字段,它对我有用:

private void txtSearch_TextChanged(object sender, EventArgs e)
{
    var con = new SqlConnection("{your connection string}");
    con.Open();
    String query = "select LastName from JoshTestTable where LastName like '%" + txtSearch.Text.Trim() + "%'";
    var cmd = new SqlCommand(query, con);
    var rr = cmd.ExecuteReader();
    var namesCollection = new AutoCompleteStringCollection();
    while (rr.Read())
        namesCollection.Add(rr["LastName"].ToString());
    rr.Close();
    con.Close();
    txtSearch.AutoCompleteCustomSource = namesCollection;
    txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
    txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
}