How to place two Tables in a Datagridview?

Asked

Viewed 131 times

0

I have a program in which I wanted to present two tables of my database in one datagridview, these two tables have relation of n to n but I don’t know how I can see the second table.

Step to demonstrate the code :

 private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                Database.DatabaseAPI.NonQuery("INSERT INTO Jogador(Id_Jogador, Nome, Idade, País, id_equipa) Values (NewID(), '" + textBox1.Text.Replace("'", "''") + "', '" + textBox2.Text.Replace("'", "''") + "', '" + textBox3.Text.Replace("'", "''") + "', '" + comboBox1.ValueMember.ToString() + "')");
                if (textBox1.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Nome em Falta");
                    return;
                }
                if (textBox2.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Idade em Falta");
                    return;
                }
                if (textBox3.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("País em Falta");
                    return;
                }
                MessageBox.Show("Dados do Jogador adicionado com sucesso!");
                DataTable dataTable = Database.DatabaseAPI.SelectDataTable("Select Id_Jogador, Nome, Idade, País, id_jogador From Jogador");
                dataGridView1.DataSource = dataTable;
            }
        }

I was able to make the team list in one combobox but I do not know how I can do for when select already get the player data rights.

Database :

inserir a descrição da imagem aqui

Can someone help me?

  • Pass the schematic of the tables you want to display so that we can make a proper query for your problem.

  • There’s a database here for a little extra help.

1 answer

3


Just do the Join with the desired player bringing the information connecting the tables:

Select Jogador.Id_Jogador, Jogador.Nome, Jogador.Idade, Jogador.País, Jogador.id_equipa From Jogador
Join Equipa on Jogador.id_equipa = Equipa.id_Equipa
  • Now when trying to open this error appears: System.Data.Sqlclient.Sqlexception: 'Ambiguous column name' Name'.'

  • Done. You must specify in the SQL command when you make a Join, from which table you will get the select information

  • Thanks I already got.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.