在C#

时间:2018-12-13 03:00:01

标签: c# datagridview

我一直在寻找我的问题的答案。但是我找不到满意的答案。现在这是我的问题。

我在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 个答案:

答案 0 :(得分: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;
                }
            }
        }
    }
}