Auto-Complete textBox1; Mapped to SQL Server to Update dataGridView1 and listBox1

Locked Auto-Complete textBox1; Mapped to SQL Server to Update dataGridView1 and listBox1

  • 4 марта 2012 г. 16:45
     
     

    I found some sample code online that is supposed to auto-complete a textBox1 and (I think) filter a dataGridView1.  Here is the code (modified slightly):

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

    namespace WindowsFormsApplication2

    {

        publicpartialclassForm1: Form

        {

            publicForm1()

            {

                InitializeComponent();

            }

     

            //private void Form1_Load(object sender, EventArgs e)

            //{

            //    // TODO: This line of code loads data into the 'northwindDataSet.Customers' table. You can move, or remove it, as needed.

            //    this.customersTableAdapter.Fill(this.northwindDataSet.Customers);

     

            //}

     

     

                DataTabledt = newDataTable();

                boolIsBound = false;

                privatevoidForm1_Load(objectsender, EventArgse)

                {

                    Form1f1 = newForm1();

                    f1.MdiParent = this;

                    f1.Show();

     

                    Form1f2 = newForm1();

                    f2.MdiParent = this;

                    f2.Show();

     

                    dt.Columns.Add("CustomerId", typeof(String));

                    dt.Columns.Add("CompanyName", typeof(string));

                    dt.Rows.Add(1, "ContactName");

                    dt.Rows.Add(2, "ContactTitle");

                    dt.Rows.Add(3, "Address");

                    dt.Rows.Add(4, "City");

                    dt.Rows.Add(5, "Region");

                    dt.Rows.Add(6, "PostalCode");

                    dt.Rows.Add(7, "Country");

                    dt.Rows.Add(8, "Phone");

                    dt.Rows.Add(9, "Fax");

     

                    listBox1.DataSource = dt;

                    listBox1.DisplayMember = "Name";

                    listBox1.ValueMember = "Id";

     

                    dataGridView1.DataSource = dt;

     

                    IsBound = true;

                }

                privatevoidtextBox1_TextChanged(objectsender, EventArgse)

                {

                    DataTableclone = dt.Clone();

                    foreach(DataRowdr indt.Select("Name like '"+ textBox1.Text + "%'"))

                    {

                        clone.ImportRow(dr);

                    }

                    IsBound = false;

                    listBox1.DataSource = clone;

                    listBox1.DisplayMember = "CompanyName";

                    listBox1.ValueMember = "CustomerId";

     

                    dataGridView1.DataSource = clone;

     

                    IsBound = true;

                }

     

                privatevoidlistBox1_SelectedIndexChanged(objectsender, EventArgse)

                {

                    if(IsBound)

                    {

                        intvalue = Convert.ToInt32(listBox1.SelectedValue);

                        listBox1.DataSource = dt;

                        listBox1.DisplayMember = "CompanyName";

                        listBox1.ValueMember = "CustomerId";

                        listBox1.SelectedValue = value;

                    }

                }

     

                privatevoiddataGridView1_RowHeaderMouseClick(objectsender, DataGridViewCellMouseEventArgse)

                {

                    if(IsBound)

                    {

                        intvalue = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);

                        dataGridView1.DataSource = dt;

                        dataGridView1.ClearSelection();

                        foreach(DataGridViewRowdgvr indataGridView1.Rows)

                        {

                            if(Convert.ToInt32(dgvr.Cells[0].Value) == value)

                            {

                                dgvr.Selected = true;

                            }

                        }

                    }

                }

     

                privatevoidbutton1_Click(objectsender, EventArgse)

                {

                }

        }

    }

     

    All I did was click the arrow on the dataGridView1 and mapped to the DataSource.  I did the same for the listBox1. 

    Now, when I type in the textBox1, nothing happens.  I tried to F11 through the code and tried to determine what the problem is, but I can’t tell.  Nothing happens at all.  No error; nothing.

    Can someone please help me get this working? 

     

    Thanks!!!

     

Все ответы

  • 4 марта 2012 г. 17:39
     
     

    Hello ryguy72,

    it may be helpful to have written this example for autocomplete textbox using a data source.

    http://code.msdn.microsoft.com/Esempio-di-autocompletament-e47dee78

    Regards.


  • 5 марта 2012 г. 3:50
     
     

    Thanks for trying to help, but I don't understand.  I have nothing on the Button-Click event; I think that's fine because I'm trying to get this to auto-complete words based on a user typing characters into the textBox1.  Does it make sense?

    Please provide some guidance.

    Regards.

  • 5 марта 2012 г. 5:47
     
     

    Hi ryguy72,

      It seems that the topic you mentioned doesn't amto-complete words because you uses DataBounding to meet the requirement,so I suggest you read some articles about how to bind the data to the field of DataGridView control.

      http://www.akadia.com/services/dotnet_databinding.html 

      I hope it will solve your problem.

    Sincerely,

    Jason Wang


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • 6 марта 2012 г. 4:26
     
     

    Sorry for being thick here, guys.  I'm even more lost now.  Can someone please give me some working sample code.

    Thanks!!

  • 6 марта 2012 г. 4:56
     
     Отвечено

    AH! Got it!  Here is the solution:

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


    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                initializeFields();
                loadCustomerFromNorthwindDbToDgv1();
            }

            DataGridViewTextBoxColumn CustomerId, CompanyName, Address;
            private void initializeFields()
            {
                CustomerId = new DataGridViewTextBoxColumn();
                CustomerId.Name = "CustomerID";
                CustomerId.DataPropertyName = "CustomerID";
                this.dataGridView1.Columns.Add(CustomerId);

                CompanyName = new DataGridViewTextBoxColumn();
                CompanyName.Name = "CompanyName";
                CompanyName.DataPropertyName = "CompanyName";
                this.dataGridView1.Columns.Add(CompanyName);

                Address = new DataGridViewTextBoxColumn();
                Address.Name = "Address";
                Address.DataPropertyName = "Address";
                this.dataGridView1.Columns.Add(Address);
            }
            DataView dv;
            private void loadCustomerFromNorthwindDbToDgv1()
            {
                SqlConnection conConnect = new SqlConnection("Data Source = Excel-PC; Database = 'Northwind'; Integrated Security = true");
                try
                {
                    SqlDataAdapter dAdapter = new SqlDataAdapter("SELECT CustomerID, CompanyName, Address FROM Customers ", conConnect);
                    DataSet DS = new DataSet();
                    BindingSource bs = new BindingSource();
                    dAdapter.Fill(DS, "dsCustomer");
                    this.dataGridView1.AutoGenerateColumns = false;
                    DataTable dt = DS.Tables["dsCustomer"];
                    bs.DataSource = DS.Tables["dsCustomer"];
                    dv = new DataView(DS.Tables["dsCustomer"]);
                    this.dataGridView1.DataSource = bs;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

                //will do the search for CompanyName name everytime you type in @ the textbox search
                private void textBox1_TextChanged(object sender, EventArgs e)
                {
                    dv.RowFilter = "CompanyName like '%' + '" + textBox1.Text + "' + '%' ";
                    this.dataGridView1.DataSource = dv;
                }
        
                private void Form1_Load(object sender, EventArgs e)
                {
                    // TODO: This line of code loads data into the 'northwindDataSet.Customers' table. You can move, or remove it, as needed.
                    this.customersTableAdapter.Fill(this.northwindDataSet.Customers);

                }
        }
    }

    MAKE SURE THE 'textBox1_TextChanged' IS LINKED TO THE EVENT!!!

  • 6 марта 2012 г. 7:55
    Модератор
     
      С кодом

    Hi ryguy72,

    Have you add these events to your controls?

    textBox1_TextChanged
    listBox1_SelectedIndexChanged
    dataGridView1_RowHeaderMouseClick

    Maybe you did not use the += operations to make these events to work. Such as:

    textBox1.textChanged += new EventHandler(textBox1_TextChanged);

    listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);

    dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseClick);

    Have you checked that?

    Or would you please send your demon project for us, maybe we can help you fix it directly.

    Best Regards


    Neddy Ren[MSFT]
    MSDN Community Support | Feedback to us