none
passing combobox from one form to another

    Question

  • I want to pass combobox which is binded to column in datatable which is in database from one form  A to another form B
    So I added one property in form B as following

    public

     

    ComboBox cmbBox

    {

     

    set

    {

    cBox =

    value;

    }

     

    get

    {

     

    return cBox;

    }

    }

    This is constructor of form B

     

    public FormB(ComboBox Cmbcontrol)

    {

    InitializeComponent();

    cmbBox = Cmbcontrol;

    }

     



    =====================
     In Form A

     

    FormB addform = new FormB(cboGroceryItem); 

    addform.Show();
    ====================
    In Form B I am checking if certain element present in combobox as follows

     

    private int Check_Row()

    {

     

    int iRetVal = 0;

     

    foreach (object item in cmbBox.Items)

    {

     

    if (((DataRowView)item).Row[1].ToString() == txtadditem.Text)

    {

    iRetVal = 1;

     

    break;

    }

    }

     

    return iRetVal;

    }

    My code is working

    But Am I passing the combobox & checking the  if perticular element present  in combobox in right way?

    I have another option of connect to database & check column of that table for perticular element present  in table or not
    that code is also working fine

     

    private int Check_Row()

    {

     

    int retvalue = 0;

     

    SqlConnection con = new SqlConnection(strConnectionString);

     

    string strseltct = "SELECT * FROM Grocery_Item WHERE item_name = @item ";

     

    SqlCommand cmd = new SqlCommand();

    cmd.Parameters.Add(

    "@item", SqlDbType.Char, 30);

    cmd.Parameters[0].Value = txtadditem.Text;

    cmd.CommandText = strseltct;

    cmd.Connection = con;

    con.Open();

     

    try

    {

    retvalue = (

    int)cmd.ExecuteScalar();

    }

     

    catch

    {

    con.Close();

    }

     

    return retvalue;

    }

     

      Which way is efficient connect to database or pass combobox from one form to another form?

    Saturday, May 02, 2009 5:15 PM

Answers

  • If you are looking for efficiency in lookup, you can keep a dataTable around and do lookup in it? And the dataTable can be passed around.
    • Marked as answer by Kira Qian Friday, May 08, 2009 5:23 AM
    Sunday, May 03, 2009 1:26 PM
  • I agree with ChronusDOTNet

    Rather then pass the ComboBox, pass the DataTable or whatever it is bound to as a property to the other form or through its constructor


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Kira Qian Friday, May 08, 2009 5:23 AM
    Sunday, May 03, 2009 4:06 PM
  • Just passing the ComboBox through the property is very fast.  ComboBox is a class, so it is passed by reference (the ComboBox in the called Form is the exact same object instance as the one in the callee Form).  No copy of any ComboBox data is occurring.  On the other hand, your second example runs a SQL query, so it is much more intense.

    • Marked as answer by Kira Qian Friday, May 08, 2009 5:22 AM
    Saturday, May 02, 2009 5:26 PM
  • You can have a method in Form2 called "AddRow" which allows you to add a row and rebinds the grid. This is if you are doing this from Form1. If you are adding a row in Form2's DataGridView that you want to be included for Form1, create an event in Form2 and have form1 subscribe to that event.

    Communicating from the MainForm to child forms is done through Properties and Public Methods. Communicating from a sibling form back to the parent is done through events.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Kira Qian Friday, May 08, 2009 5:22 AM
    Monday, May 04, 2009 5:00 PM

All replies

  • Just passing the ComboBox through the property is very fast.  ComboBox is a class, so it is passed by reference (the ComboBox in the called Form is the exact same object instance as the one in the callee Form).  No copy of any ComboBox data is occurring.  On the other hand, your second example runs a SQL query, so it is much more intense.

    • Marked as answer by Kira Qian Friday, May 08, 2009 5:22 AM
    Saturday, May 02, 2009 5:26 PM
  • when I return to formA comboBox doesn't show newly added item but its added in table in database
    any idea how to add new item in formB
    • Edited by MVPatil Saturday, May 02, 2009 8:30 PM typo
    Saturday, May 02, 2009 8:30 PM
  • Which of your two methods are you using?

    Also, you did not post the code surrounding the use of the ComboBox.  That would be helpful in figuring this out.

    Sunday, May 03, 2009 1:12 AM
  • If you are looking for efficiency in lookup, you can keep a dataTable around and do lookup in it? And the dataTable can be passed around.
    • Marked as answer by Kira Qian Friday, May 08, 2009 5:23 AM
    Sunday, May 03, 2009 1:26 PM
  • I agree with ChronusDOTNet

    Rather then pass the ComboBox, pass the DataTable or whatever it is bound to as a property to the other form or through its constructor


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Kira Qian Friday, May 08, 2009 5:23 AM
    Sunday, May 03, 2009 4:06 PM
  • Since I want to add the row into table if it isn't present in table
    Because after addition when I return to fromA my combobox doesn't show newly added element so I have to databind combobox to datatable in formB again then only in formA it shows newly added elemtent
    so what if I take that table into dataset in formB, check the row & add the row if not present & bind to combobox again

    will this be efficient ?

    Monday, May 04, 2009 4:54 PM
  • You can have a method in Form2 called "AddRow" which allows you to add a row and rebinds the grid. This is if you are doing this from Form1. If you are adding a row in Form2's DataGridView that you want to be included for Form1, create an event in Form2 and have form1 subscribe to that event.

    Communicating from the MainForm to child forms is done through Properties and Public Methods. Communicating from a sibling form back to the parent is done through events.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Kira Qian Friday, May 08, 2009 5:22 AM
    Monday, May 04, 2009 5:00 PM
  • Thank you all for input
    Wednesday, May 06, 2009 8:41 PM