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();
}