locked
User Defined Search Control in C# RRS feed

  • Question

  • Hi,

    Coding in C#, using Datagridview Controls

    I wish to create a user defined control (which contains a text box and a find button) and place it on a form which has a datagridview. The idea being the user clicks on the column header and inputs search text in the textbox and clicks the find button which uses the dataview.Rowfilter code to bring back the filtered rows.

    Now what I need help is to code the user defined search control in such a way that when I drag the control from my toolbox onto the form with a datagridview it should be fully funtionaly i.e no need for me to add any extra coding manually.Is that possible.?

    Now, I managed to create the control and was able placed it on my form.I am also able to access the parent form but I am not able to access the dataview of the tabpage on which the datagridview resides. Am I in the right direction or is there a better way to approach this ..?

    TIA..

    Jayce

    • Edited by Jayce_SouthWales Tuesday, June 1, 2010 9:42 AM additiona info.
    • Changed type Jayce_SouthWales Tuesday, June 1, 2010 9:50 AM
    • Moved by CoolDadTx Wednesday, June 2, 2010 1:18 PM Winforms related (From:Visual C# General)
    Tuesday, June 1, 2010 9:41 AM

Answers

  • Perhaps a public property that you add to your User Search Control.

     

    public class UserSearchControl : UserControl
    
    {
      //Standard Code
      private DataGridView m_gridToSearch;
      public DataGridView GridToSearch
      {
       get
       {
        return m_gridToSearch; 
       }
       set
       {
        m_gridToSearch = value;
       }
      }
    }
    Then when you drag your control onto your form, go to your properties window, and you should see the GridToSearch property you just wrote in that pane.
    • Proposed as answer by DeborahKMVP Wednesday, June 2, 2010 3:05 PM
    • Marked as answer by Jayce_SouthWales Thursday, June 3, 2010 9:25 PM
    Wednesday, June 2, 2010 1:20 PM

All replies

  • You may want to consider using events. For example, when the user clicks the Find button, the user control raises an event. The parent form can capture this event and respond by performing the find in the DataGridView.

    That way you have a "separation of concerns" and the user control is not attempting to access other controls directly.

    Another option is to put the DataGridView on the user control as well. Then the user control could more readily control the DataGridview.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, June 1, 2010 3:07 PM
  • If you would like an example of defining events in a user control, I have one here:

    http://msmvps.com/blogs/deborahk/archive/2009/07/22/user-control-events-in-vb-and-c.aspx

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, June 1, 2010 3:09 PM
  • Thanks Deborah,

    Your suggestion and the reference web link is useful, however I am looking for a single drag solution, without any need for further coding. In this case if I were to drag the search control onto all of my forms containing datagridviews then I will have to do coding on each and every form..? which defeats the objective.

    alternatively are you aware of any third party tools available which can be droped onto the forms and have the search functionality.. I would still prefer a coding solution than to buy a 3rd party tool.

    cheers

    Jayce

    Wednesday, June 2, 2010 9:57 AM
  • To access a DataGridView on the Parent form:

    DataGridView FindDataGridView(Control container)
    {
     foreach(Control c in container)
     {
      DataGridView dgv = c as DataGridView;
      if(dgv == null) dgv = FindDataGridView(c);
      if(dgv != null) return dgv;
     }
     return null;
    }

     

    You can call it with FindDataGridView(Parent);

    Wednesday, June 2, 2010 12:19 PM
  • Perhaps a public property that you add to your User Search Control.

     

    public class UserSearchControl : UserControl
    
    {
      //Standard Code
      private DataGridView m_gridToSearch;
      public DataGridView GridToSearch
      {
       get
       {
        return m_gridToSearch; 
       }
       set
       {
        m_gridToSearch = value;
       }
      }
    }
    Then when you drag your control onto your form, go to your properties window, and you should see the GridToSearch property you just wrote in that pane.
    • Proposed as answer by DeborahKMVP Wednesday, June 2, 2010 3:05 PM
    • Marked as answer by Jayce_SouthWales Thursday, June 3, 2010 9:25 PM
    Wednesday, June 2, 2010 1:20 PM