none
How to achieve find & find next value for datagridview RRS feed

  • Question

  • suppose user will write text in search textbox to search it in datagrid view specific row. if found then if user click again on find button then same text will be search in other rows. if value changes in search textbox then search will start from again datagridview's first row. how to achieve this. i tried this way & it worked but need to add search next feature. here is my code

                bool found = false;
                int counter = 0;
    
                if (strtofindValue.Trim()!="")
                {
                    #region Search in datagrid by li name, if data found row will be selected and top row
                    if (dgList.Rows.Count > 0)
                    {
                        foreach (DataGridViewRow dgvr in dgList.Rows)
                        {
                            if (dgvr.Cells[colindex].Value.ToString().ToUpper().Contains(strtofindValue.Trim().ToUpper()))
                            {
                                dgvr.Selected = true;
                                dgList.FirstDisplayedScrollingRowIndex = counter;
                                found = true;
                                return;
                            }
                            counter++;
                        }
                    }
                    else
                    {
                        found = false;
                    }
    
                    if (!found)
                    {
                        MessageBox.Show("Search data not found ");
                    }
    
                    #endregion
                }

    Thursday, October 10, 2019 1:13 PM

Answers

  • Here is load the BindingSource with a DataTable and set the data source of the DataGridView to the BindingSource. In the button click filter for contains or remove the filter if the text box is empty.

    using System;
    using System.Windows.Forms;
    using DataOperations;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            private readonly BackendOperations _dataOperations = 
                new BackendOperations();
            private readonly BindingSource _bsCustomers = 
                new BindingSource();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                var customerDataTable = _dataOperations.RetrieveAllRecords();
                _bsCustomers.DataSource = customerDataTable;
                _bsCustomers.Sort = "CompanyName";
    
                DataGridView1.DataSource = _bsCustomers;
    
            }
    
            private void FilterCustomerButton_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrWhiteSpace(CompanyNameTextBox.Text))
                {
                    _bsCustomers.Filter = "";
                }
                else
                {
                    _bsCustomers.Filter = $"CompanyName Like '%{CompanyNameTextBox.Text}%'";
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thursday, October 10, 2019 3:22 PM
    Moderator
  • Hi Sudip_inn,

    To search the text in the DataGridView, you can refer to this demo.

        int rowindex = 0;
        private void btSearch_Click(object sender, EventArgs e)
        {
            dataGridView1.ClearSelection();
            for (int row = rowindex; row < dataGridView1.RowCount - 1; row++)
            {
                if (dataGridView1.Rows[row].Cells[1].Value.ToString().Trim() == tbtext.Text)
                {
                    // Jump to the specified row
                    dataGridView1.CurrentCell = dataGridView1.Rows[row].Cells[0];
                    dataGridView1.Rows[row].Selected = true;
                    // Reset the start index
                    rowindex = row + 1;
                    // Return this search
                    return;
                }
            }
        }
    
        private void tbtext_TextChanged(object sender, EventArgs e)
        {
            // If text changed, reset index to 0
            rowindex = 0;
        }

    Test result,

    Regards,

    Kyle


    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.

    • Marked as answer by Sudip_inn Friday, October 11, 2019 8:54 AM
    Friday, October 11, 2019 3:25 AM

All replies

  • Hello,

    An alternate thought is to load the DataSource of the DataGridView using a BindingSource then use the Filter property to filter rather than find next via a TextBox and button and if the TextBox is empty remove a prior filter.

    DataGridView controls are best done by setting the DataSource rather than populating rows of the DataGridView.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 10, 2019 1:21 PM
    Moderator
  • i need to find data in data gridview and if found then i need to select row. how could i use in this scenario BindingSource ? better post a sample code if possible.
    Thursday, October 10, 2019 3:11 PM
  • Here is load the BindingSource with a DataTable and set the data source of the DataGridView to the BindingSource. In the button click filter for contains or remove the filter if the text box is empty.

    using System;
    using System.Windows.Forms;
    using DataOperations;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            private readonly BackendOperations _dataOperations = 
                new BackendOperations();
            private readonly BindingSource _bsCustomers = 
                new BindingSource();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                var customerDataTable = _dataOperations.RetrieveAllRecords();
                _bsCustomers.DataSource = customerDataTable;
                _bsCustomers.Sort = "CompanyName";
    
                DataGridView1.DataSource = _bsCustomers;
    
            }
    
            private void FilterCustomerButton_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrWhiteSpace(CompanyNameTextBox.Text))
                {
                    _bsCustomers.Filter = "";
                }
                else
                {
                    _bsCustomers.Filter = $"CompanyName Like '%{CompanyNameTextBox.Text}%'";
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thursday, October 10, 2019 3:22 PM
    Moderator
  • Hi Sudip_inn,

    To search the text in the DataGridView, you can refer to this demo.

        int rowindex = 0;
        private void btSearch_Click(object sender, EventArgs e)
        {
            dataGridView1.ClearSelection();
            for (int row = rowindex; row < dataGridView1.RowCount - 1; row++)
            {
                if (dataGridView1.Rows[row].Cells[1].Value.ToString().Trim() == tbtext.Text)
                {
                    // Jump to the specified row
                    dataGridView1.CurrentCell = dataGridView1.Rows[row].Cells[0];
                    dataGridView1.Rows[row].Selected = true;
                    // Reset the start index
                    rowindex = row + 1;
                    // Return this search
                    return;
                }
            }
        }
    
        private void tbtext_TextChanged(object sender, EventArgs e)
        {
            // If text changed, reset index to 0
            rowindex = 0;
        }

    Test result,

    Regards,

    Kyle


    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.

    • Marked as answer by Sudip_inn Friday, October 11, 2019 8:54 AM
    Friday, October 11, 2019 3:25 AM
  • @Kyle great sample code. thanks a lot
    Friday, October 11, 2019 8:54 AM