0
votes

I've a datagridview which has values from 2 Tables.

When I double click on a row, I need to pass those values to another form. That form has textbox and a Combobox which contains values from a table which gives me all need value to select.

For the textboxes, no problem at all, but for the combobox, that's a real pain.

But I need this ComboBox gets the Selected Value from the datagridview, but it allways turns the first value from the list.

Here's my code for double ckick event in the datagridview.

Can please someone help me making this work?

This is a Windows Forms project.

public System.Data.DataTable ContratosTable { get; set; }

    public void LoadContratosTable()
    {
        ContratosTable = new System.Data.DataTable();
        using (var cn = new SqlConnection { ConnectionString = ConfigurationManager.ConnectionStrings["IWMConnectionString"].ConnectionString.ToString() })
        {
            using (var cmd = new SqlCommand { Connection = cn })
            {
                cn.Open();
                cmd.CommandText = "SELECT IdContrato, Designacao FROM dbo.Contratos";
                ContratosTable.Load(cmd.ExecuteReader());
            }
        }
    }

    private void concelhos_datagrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        BindingSource bs = new BindingSource();
        LoadContratosTable();
        bs.DataSource = ContratosTable;
        Concelhos_Edit f = new Concelhos_Edit();
        f.txt_id.Text = concelhos_datagrid.CurrentRow.Cells[0].Value.ToString();
        var _nomeContrato = ContratosTable.AsEnumerable().FirstOrDefault(a => a.Field<int>("IdContrato") == ((DataRowView)bs.Current).Row.Field<int>("IdContrato")).Field<string>("Designacao");
        var pos = f.cmb_contrato.FindString(_nomeContrato);
        if (pos > -1)
        {
            f.cmb_contrato.SelectedIndex = pos;
        }
        f.txt_sigla.Text = concelhos_datagrid.CurrentRow.Cells[2].Value.ToString();
        f.txt_nome.Text = concelhos_datagrid.CurrentRow.Cells[3].Value.ToString();
        f.txt_codigo.Text = concelhos_datagrid.CurrentRow.Cells[4].Value.ToString();
        f.txt_qservico.Text = concelhos_datagrid.CurrentRow.Cells[5].Value.ToString();
        f.chk_ativo.Checked = (bool)concelhos_datagrid.CurrentRow.Cells[6].Value;
        f.MdiParent = this.MdiParent;
        f.Show();
    }
1
Which values do you want to show in the dropdown on the 2nd form? - CodingYoshi
The values from the Database Table "Contratos" but with the SelectedValue from the DataGridView. The Values from the Table Contratos are being displayed into the ComboBox, but the SelectedValue is always the first from the Table Contratos and not he one in the DataGridView. - Carlos Ferreira
When and where are you assigning the values for "f.cmb_contrato"? Like the way you are doing for other text boxes? Before assigning SelectedIndex, Are you sure that combobox have items in it? - Arpit Gupta

1 Answers

0
votes

This will point, and help, you in the right direction. Try and follow either MVC or MVVM and the design will help you keep the concerns separate and you will end up with better and cleaner code.

For now, create a class like shown below:

public class Info
{
    public Info()
    {
        this.NomeContrato = new List<string>();
    }
    public string Id { get; set; }
    public string Sigla { get; set; }
    public string Nome { get; set; }
    public string Codigo { get; set; }
    public string QServico { get; set; }
    public bool Ativo { get; set; }
    public List<string> NomeContrato { get; set; }
}

Then add this property to the Concelhos_Edit class:

public Info Model { get; set; }

Also add this code to _Load handler of Concelhos_Edit form:

this.cmb_contrato.DataSource = this.Model.NomeContrato;
// If you need the SelectedValue from the other form, then add another 
// property to Info class and put the value there.
// this.cmb_contrato.SelectedValue = this.Model.SelectedValue;
this.cmb_contrato.SelectedValue = "whatever you want to be selected";

Then, modify the cell double click method named: concelhos_datagrid_CellDoubleClick to look like below:

private void concelhos_datagrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    // Your code...
    var editForm = new Concelhos_Edit();
    var editFormModel = new Info();
    editFormModel.Id = f.txt_id.Text = concelhos_datagrid.CurrentRow.Cells[0].Value.ToString();
    // Set other properties

    // Here is the work for dropdown
    var _nomeContrato = ContratosTable.AsEnumerable().FirstOrDefault(a => a.Field<int>("IdContrato") == ((DataRowView)bs.Current).Row.Field<int>("IdContrato")).Field<string>("Designacao");

    editFormModel.NomeContrato.AddRange(_nomeContrato);

    formEdit.Model = editFormModel;
    formEdit.MdiParent = this.MdiParent;
    formEdit.Show();
}

Please use this code as a guide and you may need to tweak it. The idea is to pass the information the form needs, and then let that form do whatever with the information: whether it wants to show the information in a TextBox or RadioButton is that form's business.