row searched grid losing position.

5 posts, 1 answers
  1. Sam
    Sam avatar
    14 posts
    Member since:
    Apr 2011

    Posted 15 Mar Link to this post

    I have a grid bound to claimantBindingSource in which I have enabled row searching.

    The search returns 3 matches, I move to the second match.

    After executing the code below the bindingSource is moving to the first match which is a huge issue.

    Is there any way to keep the position on the second match?

     

     claimantDrv = this.claimantBindingSource.Current as DataRowView;

     var Claimant = claimantDrv.Row as CRSDataSet.claimantRow;
     fullname = "some name";
     Claimant.name = fullname;

     

     

     

     

  2. Sam
    Sam avatar
    14 posts
    Member since:
    Apr 2011

    Posted 15 Mar in reply to Sam Link to this post

    Sorry, 

    Searching by name, so the position is moving because the name changed.

    That said is there anyway to prevent the row from moving if the underlying value changes?

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Sam
    Sam avatar
    14 posts
    Member since:
    Apr 2011

    Posted 15 Mar in reply to Sam Link to this post

    Okay

    RadGridView.MasterView.TableSearchRow.AutomaticallySelectFirstResult = false;

    Gets me where I need to go, however how can I turn it back on only when the user enters a value into the Search Box?
  5. Answer
    Hristo
    Admin
    Hristo avatar
    714 posts

    Posted 16 Mar Link to this post

    Hello Sam,

    Thank you for writing.

    You can handle the TextChanged event of the search textbox and if the text is empty set the AutomaticallySelectFirstResult property to false. Please check my code snippet below:
    public Form1()
    {
        InitializeComponent();
     
        this.radGridView1.DataSource = this.GetData();
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.AllowSearchRow = true;
     
        this.radGridView1.ViewCellFormatting += radGridView1_ViewCellFormatting;
    }
     
    private void radGridView1_ViewCellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
    {
        GridSearchCellElement cell = e.CellElement as GridSearchCellElement;
        if (cell != null)
        {
            cell.SearchTextBox.TextChanged -= SearchTextBox_TextChanged;
            cell.SearchTextBox.TextChanged += SearchTextBox_TextChanged;
        }
    }
     
    private void SearchTextBox_TextChanged(object sender, EventArgs e)
    {
        GridSearchCellTextBoxElement searchTextBox = (GridSearchCellTextBoxElement)sender;
        if (searchTextBox.Text != "")
        {
            this.radGridView1.MasterView.TableSearchRow.AutomaticallySelectFirstResult = true;
        }
        else
        {
            this.radGridView1.MasterView.TableSearchRow.AutomaticallySelectFirstResult = false;
        }
    }
     
    private DataTable GetData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Date", typeof(DateTime));
        dt.Columns.Add("Bool", typeof(bool));
        dt.Columns.Add("Description", typeof(string));
        for (int i = 0; i < 20; i++)
        {
            dt.Rows.Add(i, "Name " + i, DateTime.Now.AddDays(i), i % 2 == 0 ? true : false, "Description " + i);
        }
     
        return dt;
    }

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  6. Sam
    Sam avatar
    14 posts
    Member since:
    Apr 2011

    Posted 16 Mar in reply to Hristo Link to this post

    Awesome Thanks!
Back to Top
UI for WinForms is Visual Studio 2017 Ready