i need a way to show records on a new form by clicking on a datagridview on another form

33 views Asked by At

I have a form with a datagridview showing inventory. the inventory table has a field called unitid.

i want it to work such that when i select a record from the inventory datagridview it must open another from e.g frminventorydetails, and show all the fields in textboxes and combo boxes from the record i selected.

i have it working but the problem is the combobox. it does not show the correct value in the combobox. when the details form loads i have a method to load the combobox with values from the units table. but when the details form loads it does not go to the appropriate displaymember.

it works perfectly when i put the combo box in the same form as the inventory datagrid. but does not work when the combobox is n another form.

  private void dginventory_KeyDown(object sender, KeyEventArgs e)
    {

        if (e.KeyCode == Keys.Enter)
        {
            try
            {
                string getdetails = @"select * from tblinventory where
                tblinventory.invid='" + Convert.ToInt32(dginventory.CurrentRow.Cells[0].Value.ToString()) + "'";

                performqueries.singleResult(getdetails);

                frmviewinvdetails invdetopen = new frmviewinvdetails();

                if (performqueries.dt.Rows.Count > 0)
                {
                    invdetopen.txtinvid.Text = performqueries.dt.Rows[0].Field<int>("invid").ToString();
                    invdetopen.txtinvcode.Text = performqueries.dt.Rows[0].Field<string>("InventoryCode").ToString();
                    invdetopen.txtInvDescription.Text = performqueries.dt.Rows[0].Field<string>("InvDescription").ToString();
                    invdetopen.txtInvShortText.Text = performqueries.dt.Rows[0].Field<string>("InvShortText").ToString();

                    //here im assigning the unitid to the selectedvalue property of the combobox in the details form.
                    invdetopen.cbunits.SelectedValue = Convert.ToInt32(performqueries.dt.Rows[0].Field<int>("unitid").ToString());

                    }

                   invdetopen.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw;
            }

    // this fills the combobox on the details form when it loads
    private void frmviewinvdetails_Load(object sender, EventArgs e)
    {
       SqlConfig performcrud = new SqlConfig();
        string units = "select * from tblunits";
        performcrud.fill_CBO(units, cbunits);
    }

i expect the combo box to display the appropriate unit description for the particilar item i selected, but instead it just remains on the first record.

it works if the combobox is on the same form as the datagrid but not if im opening a new form.

1

There are 1 answers

0
Oak_3260548 On

You don't show the code for setting the ValueMember and DisplayMember of the cbunits. Make sure that those properties are set, then the DataSource of the ComboBox is set and finally you can use SelectedValue.

Also, this might be a problem:

string units = "select * from tblunits";

You must match the property (Diplay and Value) names, so I'd strongly recommend to use specific field names in SQL. I don't know if it even works with '*'. Edit: I tested it, it does work with the asterix, so you only need to make sure the field names are matching.. Also using DataTable as the DataSource would be an advantage:

dt Datatable = FunctionToGetDataTable("select ID, UnitName as UnitName from tblunits");

So then you can use:

invdetopen.cbunits.ValueMember = "ID";
invdetopen.cbunits.ValueMember = "UnitName";
invdetopen.cbunits.ValueMember = dt;

You should always be able to stop your code with breakpoint and look into the variables. If you use DataTable, you can take advantage of DataTable Visualizer (click the little magnifying glass over "dt") in the debugging mode and you'll actually see it as a table.

It doesn't take more than those few lines above, if it is done correctly.