none
Bind WPF Auto Complete TextBox with DataTable C# RRS feed

  • Question

  • Developing application in WPF, i want to Bind Auto Complete Textbox with dataTable. I have table in database, retrive data from Database and stored in DataTable. After that bind that datatable with Auto Complete textbox. How i can achive this? Please also suggest some Auto Complete toolbox?
    Monday, October 21, 2019 8:45 PM

All replies

  • Hi,

    Using the code below, we can create our own autocomplete for TextBox.

      public partial class MainWindow : Window
        {
            DataTable dt = new DataTable();
            List<string> data = new List<string>();
            public MainWindow()
            {
                InitializeComponent();
                dt.Columns.Add(new DataColumn("column1"));
                
                dt.Rows.Add(new object[] { "Alex" });
                dt.Rows.Add(new object[] { "Zack" });
                dt.Rows.Add(new object[] { "Peter" });
                dt.Rows.Add(new object[] {"Jacky" });
                foreach (DataRow dr in dt.Rows)
                {
                    data.Add(dr["column1"].ToString());
                }
            }
    
            private void textBox_KeyUp(object sender, KeyEventArgs e)
            {
                bool found = false;
                var border = (resultStack.Parent as ScrollViewer).Parent as Border;
          
    
                string query = (sender as TextBox).Text;
    
                if (query.Length == 0)
                {
                    // Clear   
                    resultStack.Children.Clear();
                    border.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    border.Visibility = System.Windows.Visibility.Visible;
                }
    
                // Clear the list   
                resultStack.Children.Clear();
    
                // Add the result   
                foreach (var obj in data)
                {
                    if (obj.ToLower().StartsWith(query.ToLower()))
                    {
                        // The word starts with this... Autocomplete must work   
                        addItem(obj);
                        found = true;
                    }
                }
    
                if (!found)
                {
                    resultStack.Children.Add(new TextBlock() { Text = "No results found." });
                }
            }
            private void addItem(string text)
            {
                TextBlock block = new TextBlock();
    
                // Add the text   
                block.Text = text;
    
                // A little style...   
                block.Margin = new Thickness(2, 3, 2, 3);
                block.Cursor = Cursors.Hand;
    
                // Mouse events   
                block.MouseLeftButtonUp += (sender, e) =>
                {
                    textBox.Text = (sender as TextBlock).Text;
                };
    
                block.MouseEnter += (sender, e) =>
                {
                    TextBlock b = sender as TextBlock;
                    b.Background = Brushes.PeachPuff;
                };
    
                block.MouseLeave += (sender, e) =>
                {
                    TextBlock b = sender as TextBlock;
                    b.Background = Brushes.Transparent;
                };
    
                // Add to the panel   
                resultStack.Children.Add(block);
            }
        }
    }
     <StackPanel>
            <TextBox Name="textBox" Width="300" Padding="5, 3, 5, 3" KeyUp="textBox_KeyUp" />
            <Border Width="298" Height="150" BorderBrush="Black"   
            BorderThickness="1" >
                <ScrollViewer VerticalScrollBarVisibility="Auto">
                    <StackPanel Name="resultStack"></StackPanel>
                </ScrollViewer>
            </Border>
        </StackPanel>

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 22, 2019 6:29 AM
    Moderator
  • Searching is not optimsied, only match datatable information that start with keyword.
    Wednesday, October 23, 2019 5:41 AM
  • Hi,

    The effect you want is to match any letter filter, not the first letter?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 23, 2019 6:25 AM
    Moderator
  • Yeah! I want to match any letter in DataSet, and return all the assosicated data.
    Wednesday, October 23, 2019 7:00 AM
  • Yeah! I want to match any letter in DataSet, and return all the assosicated data.

    Hi,

    Just fix the code to be fine.

       foreach (var obj in data)
                {
                    if (obj.ToLower().Contains(query.ToLower()))
                    {
                        // The word starts with this... Autocomplete must work   
                        addItem(obj);
                        found = true;
                    }
                }

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 23, 2019 7:15 AM
    Moderator