Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > C# AutoComplete เวลาพิมพ์ค้นหาใน textbox มันไม่ค้นหาคำอื่นๆด้วย %text% แบบนี้ แต่มันค้นหาจากคำแรก ไม่ได้หมายถึงตอนกด Enter นะครับ หมายถึงตอนพิมพ์ และยังไม่ได้เลือก ยังไม่ได้ Enter



 

C# AutoComplete เวลาพิมพ์ค้นหาใน textbox มันไม่ค้นหาคำอื่นๆด้วย %text% แบบนี้ แต่มันค้นหาจากคำแรก ไม่ได้หมายถึงตอนกด Enter นะครับ หมายถึงตอนพิมพ์ และยังไม่ได้เลือก ยังไม่ได้ Enter

 



Topic : 125331



โพสกระทู้ ( 249 )
บทความ ( 0 )



สถานะออฟไลน์




C# AutoComplete เวลาพิมพ์ค้นหาใน textbox มันไม่ค้นหาคำอื่นๆด้วย %text% แบบนี้ แต่มันค้นหาจากคำแรก ไม่ได้หมายถึงตอนกด Enter นะครับ ..หมายถึงตอนพิมพ์ และยังไม่ได้เลือก ยังไม่ได้ Enter ..พิมพ์เพื่อให้แสดง list รายการใกล้เคียงกับคำที่พิมพ์

ออกตัวก่อนเลยว่า กำลังเรียกรู้ได้ 3 วันครับ พอมีพื้นฐานจาก html php มาบ้าง เลยเลือกภาษา c# เห็นเขาบอกว่าคล้ายกัน มาติดตรงเนี้ยแหละ สอนผมหน่อย กรุณาหน่อยครับ ผมไม่รู้จะค้นหาคำว่าอะไร รู้แต่ "AutoComplete textbox database แสดง ใน list" อะไรแบบนี้ ผมใช้ visual studio 2012 เขียนครับ (ไม่รู้เกี่ยวอะไรกับคำถามหรือเปล่า ฮ่าๆ) ผมยังไม่ได้บอกอะไรอีกสอบถามได้นะครับ อยู่ดูโพสตลอดครับ จนกว่าจะได้คำตอบที่ต้องการ

1.เวลาพิมพ์ค้นหาใน textbox มันไม่ค้นหาคำอื่นๆมาแสดงด้วย %text% แบบนี้ แต่มันค้นหาจากคำแรก ค้นหานี้ไม่ได้หมายถึงตอนกด Enter นะครับ หมายถึงตอนพิมพ์ และยังไม่ได้เลือก ยังไม่ได้ Enter .......พิมพ์เพื่อให้แสดง list รายการใกล้เคียงกับคำที่พิมพ์
1

2. เมื่อเราเลือกแล้ว ไม่ว่าจะ Enter หรือกดจาก list ที่แสดงขึ้นมา มันเอาค่าทั้งหมดใน textbox ไปค้นหา "10-00001 BANNER 100 CAP. 10086" ซึ่งแบบนี้จะไม่พบข้อมูล เพราะผมสั่งให้มัน = ไม่ได้ like ถ้าเทียบกับ html ก็จะเป็นแบบด้านล่างนี้ คือเอาค่า code เข้าไปคิวรี่ใน sql
<select>
<option value="CODE">CODE| NAME1 | NAME2</option>
</select>

ตอนนี้แก้ปัญหาแบบนี้ไปก่อน อาจจะเป็นวิธีที่ไม่ถูกต้องครับ
2

3. ทำไมมันเปิดโปรแกรมโคตรช้าเลยครับ พอมีวิธีทำให้มันเร็วไหม ตอนนี้ผมแก้ที่ sql : select top 30 แทน ข้อมูลที่จะนำมาแสดงใน list ค้นหา มี 3-4 พันบรรทัดครับ พอจะมีวิธีไหมครับที่ทำให้ไวขึ้นกว่านี้

ขอบคุณครับได้แค่นี้น่าจะทำให้ผมไปต่อได้เยอะเลย ..

3
Code (C#)
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 MultiAutocompleteSQLC
{
    public partial class Form1 : Form
    {
        //initialize all classes
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();

        //declaring variables
        string query;
        //int result;

        public Form1()
        {
            InitializeComponent();
        }

        private void AutoSuggest()
        {
            try
            {
                //set a query for retrieving data in the database
                query = "Select top 30 code,name1,name2 from bcitem";
                //initialize new Sql commands
                cmd = new SqlCommand();
                //hold the data to be executed.
                cmd.Connection = conn;
                cmd.CommandText = query;
                //initialize new Sql data adapter
                da = new SqlDataAdapter();
                //fetching query in the database.
                da.SelectCommand = cmd;
                //initialize new datatable
                dt = new DataTable();
                //refreshes the rows in specified range in the datasource. 
                da.Fill(dt);
               int max =  dt.Columns.Count;

               txtMultiSuggest.AutoCompleteCustomSource.Clear();
                foreach (DataRow r in dt.Rows)
                {
                    for (int i = 0; i < max; i++)
                    {
                        //getting all rows in the specific field|Column
                        string rw = r.Field<string>("code").ToString()
                           + "  " + r.Field<string>("name1").ToString()
                           + "  " + r.Field<string>("name2").ToString();                        
                        //MessageBox.Show(rw);

                        //Set the properties of a textbox to make it auto suggest.
                        txtMultiSuggest.AutoCompleteMode = AutoCompleteMode.Suggest;
                        txtMultiSuggest.AutoCompleteSource = AutoCompleteSource.CustomSource;
                        //adding all rows into the textbox
                        txtMultiSuggest.AutoCompleteCustomSource.Add(rw);
                    }
                } 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);             
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.ActiveControl = txtMultiSuggest;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;

            //set a connection between SQL server and Visual C#
            conn.ConnectionString = "data source=XXXXXX;initial catalog=XXXXXX;user id=sa;password=XXXXXX;";
            AutoSuggest();
        }

        private void txtMultiSuggest_KeyDown(object sender, KeyEventArgs e)
        {
           
            if (e.KeyCode == Keys.Enter)
            {
                SqlConnection con = new SqlConnection("data source=XXXXXX;initial catalog=XXXXXX;user id=sa;password=XXXXXX;");
                SqlDataAdapter oda = new SqlDataAdapter("select code,name1,name2 from bcitem where code = '" + txtMultiSuggest.Text "'", con);
                con.Open();
                DataTable data = new DataTable();
                oda.Fill(data);
                if (data.Rows.Count > 0)
                {
                    dataGridView1.DataSource = data;
                }
                else
                {
                    MessageBox.Show("ไม่พบสินค้า   " + txtMultiSuggest.Text);
                }
                con.Close();
                txtMultiSuggest.Clear();
            }
            
        }
        
    }
}





Tag : .NET, C#









ประวัติการแก้ไข
2016-11-11 12:30:50
2016-11-11 15:17:34
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-11-11 11:30:24 By : nPointXer View : 3300 Reply : 2
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Quote:
SqlDataAdapter oda = new SqlDataAdapter("select code,name1,name2 from bcitem where code = '" + txtMultiSuggest.Text "'", con);


ลอง Debug ดูค่า SQL แล้วเอาไปรันดูใน Query ครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-11-14 10:07:12 By : mr.win
 


 

No. 2



โพสกระทู้ ( 249 )
บทความ ( 0 )



สถานะออฟไลน์


งมๆมาได้แค่นี้แหละครับ มือใหม่ ฮ่าๆๆ ถ้าใช้ DataGridView แทน listbox ข้อความจะออกมาเป็นระเบียบกว่านี้ มันจะแสดงแยกเป็น col เลย ทีนี้ส่งค่าสบาย ๆ ไม่ต้องไปตัดช่องว่างออกเหมือนผม 5555 ลองๆเอาไปประยุกต์กันดูครับ อาจจะยังไม่ถูกหลักการ เดี๋ยวพอเราเรียนรู้คำสั่งมากขึ้นค่อยกลับมาแก้ให้ code สั้นลงครับ

Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace doproject
{
    public partial class frm_stkitem : Form
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;
        SqlDataAdapter da = new SqlDataAdapter();
        string MyDB = ConfigurationManager.ConnectionStrings["DBConnect"].ConnectionString;        
             
        public frm_stkitem()
        {
            InitializeComponent();
            this.ActiveControl = textBox1;
            textBox1.AutoCompleteMode = AutoCompleteMode.None;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            AutoSuggest();
            hideResults();
        }

        private void AutoSuggest()
        {
            try
            {
                conn = new SqlConnection(MyDB);
                conn.Open();
                cmd = new SqlCommand("SELECT Barcode, Code, Name1, Name2 FROM items", conn);
                dr = cmd.ExecuteReader();

                textBox1.AutoCompleteCustomSource.Clear();
                AutoCompleteStringCollection acsc = new AutoCompleteStringCollection();
                textBox1.AutoCompleteCustomSource = acsc;

                while (dr.Read())
                {
                    string rw = (string)dr["code"] + "   " + (string)dr["name1"] + "   " + (string)dr["name2"] + "   " + (string)dr["Barcode"];
                    acsc.Add(rw.ToString());
                }

                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        void textBox1_TextChanged(object sender, System.EventArgs e)
        {
            listBox1.Items.Clear();
            if (textBox1.Text.Length == 0)
            {
                hideResults();
                return;
            }
          
            foreach (String s in textBox1.AutoCompleteCustomSource)
            {                
                if (s.ToUpper().Contains(textBox1.Text.ToUpper()))
                {
                    listBox1.Items.Add(s);
                    listBox1.Visible = true;
                }
            }

            if (listBox1.Items.Count > 0)
            {
                listBox1.SelectedIndex++;
            }
            else {
                listBox1.Visible = false;
            }
        }

        void hideResults()
        {
            listBox1.Visible = false;
        }

        void result() {
            string Text1 = textBox1.Text;
            Text1 = Text1.Replace("'", "''");
            string[] str1 = Text1.Split("   ".ToCharArray());

            conn.Open();
            cmd = new SqlCommand("SELECT Barcode, Code, Name1, Name2 FROM items WHERE code like '%" + str1[0] + "%' or Barcode like '%" + str1[0] + "%'", conn);
            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("ไม่พบสินค้า   " + textBox1.Text);
            }
            textBox1.Clear();
            textBox1.Focus();
            conn.Close();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {            
            if (e.KeyCode == Keys.Enter)
            {
                if (listBox1.Items.Count > 0)
                {                    
                    textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
                    result();
                }
            }

            if (e.KeyCode == Keys.Up && this.listBox1.SelectedIndex - 1 > -1)
            {
                listBox1.SelectedIndex--;
                textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
            }

            if (e.KeyCode == Keys.Down && this.listBox1.SelectedIndex + 1 < this.listBox1.Items.Count)
            {
                listBox1.SelectedIndex++;
                textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
            }

        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
            hideResults();
            result();
        }
    }
}




ประวัติการแก้ไข
2016-11-16 17:58:34
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-11-16 17:57:25 By : nPointXer
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# AutoComplete เวลาพิมพ์ค้นหาใน textbox มันไม่ค้นหาคำอื่นๆด้วย %text% แบบนี้ แต่มันค้นหาจากคำแรก ไม่ได้หมายถึงตอนกด Enter นะครับ หมายถึงตอนพิมพ์ และยังไม่ได้เลือก ยังไม่ได้ Enter
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่