Use inner join with BindingSource

2

I'm trying to query an access database using inner join along with BindingSource , this binding will send the data to a Datagridview .

However, when the data is loaded into the grid, the displayed results are a select common% and not inner join I'm looking for the solution some time and could not find.

Follow the code snippet to add data to the datagrid

private void VinculaDadosDataGridView()
    {
        try
        {
            conn = new OleDbConnection(strConexaoSQL);
            conn.Close();
            string query = @"SELECT Inventario.Código, Polos.Polo, Inventario.Tipo, Inventario.Equipamento, Inventario.Marca,
                             Inventario.Modelo, Inventario.NS, Inventario.Patrimonio, Inventario.[Constar no CC], Inventario.Usuario
                             FROM (Inventario
                             INNER JOIN Polos ON Inventario.Alocado_em = Polos.Código);";
            conn.Open();
            DataSet ds = new DataSet();
            OleDbCommand cmd = new OleDbCommand(query, conn);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(ds, "Inventario");
            bs.DataSource = ds;
            bs.DataMember = ds.Tables[0].TableName;
            dgvDados.DataSource = bs;
            formataGridView();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            //dr.Close();
            conn.Close();
        }
    }

I do not know if it is important, but it follows the formatting of theGridView

private void formataGridView()
    {
        var grd = dgvDados;
        //grd.AutoGenerateColumns = false;
        //grd.RowHeadersVisible = false;
        grd.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
        grd.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
        grd.Columns[0].Visible = false;
        //altera a cor das linhas alternadas no grid
        grd.RowsDefaultCellStyle.BackColor = Color.White;
        grd.AlternatingRowsDefaultCellStyle.BackColor = Color.Aquamarine;
        /*//altera o nome das colunas
        grd.Columns[0].HeaderText = "Código";
        grd.Columns[1].HeaderText = "Alocado em";
        grd.Columns[2].HeaderText = "Tipo";
        grd.Columns[3].HeaderText = "Equipamento";
        grd.Columns[4].HeaderText = "Marca";
        grd.Columns[5].HeaderText = "Modelo";
        grd.Columns[6].HeaderText = "NS";
        grd.Columns[7].HeaderText = "Patrimônio";
        grd.Columns[8].HeaderText = "Constar no CC";
        grd.Columns[9].HeaderText = "Outros";
        //largura colunas
        grd.Columns[0].Width = 50;
        grd.Columns[1].Width = 150;
        grd.Columns[2].Width = 150;
        grd.Columns[3].Width = 150;
        grd.Columns[4].Width = 150;
        grd.Columns[5].Width = 150;
        grd.Columns[6].Width = 150;
        grd.Columns[7].Width = 150;
        grd.Columns[8].Width = 150;
        grd.Columns[9].Width = 150;*/
        //seleciona a linha inteira
        grd.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        //não permite seleção de multiplas linhas
        grd.MultiSelect = false;
        // exibe nulos formatados
        //grd.DefaultCellStyle.NullValue = " - ";
        //permite que o texto maior que célula não seja truncado
        grd.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    }

Result

Notice that the column alocado_em continues in ID

    
asked by anonymous 26.08.2014 / 20:18

1 answer

1

OleDbCommand receives a normal database query.

See if it works:

string query = @ "SELECT
inv.Code
, Polos.Polo
, inv.Tipo
, inv.Equipment
, inv.Marca
, inv.Model
, inv.NS
, inv.Patrimonio
, inv. [Join the CC]
, User.Usuario
FROM inv inventory INNER JOIN polos
ON inv.alocado_em = poles.code; "

As for the OleDbDataAdapter just leave the DataSet with an argument.

da.Fill (ds)

    
27.08.2014 / 18:09