How to display a query in a dataGridView?

0

I have 2 textBox (textBox1 and textBox2), the query I want is this:

The production_line would be the textBox2 and the item_number is the textBox1

 select * from tbl_volumen2 where production_line ='L1B4' AND item_number ='VPJG7F-10849-BD'

When making this query I would like you to show me all the data that exists in my table tbl_volumen2 in a dataGridView that I have in a Windows Form in C #

    
asked by abi 20.04.2017 в 17:43
source

2 answers

5

A fairly simple code to place the data of a query in your dataGridView :

var consulta= "select * from tbl_volumen2 where production_line ='L1B4' AND item_number ='VPJG7F-10849-BD'";
 var c = new SqlConnection(yourConnectionString); // Tu String de conexión
 var adaptador= new SqlDataAdapter(consulta, c); 

 var commandBuilder = new SqlCommandBuilder(adaptador);
 var ds = new DataSet();
 adaptador.Fill(ds);
 dataGridView.ReadOnly = true; 
 dataGridView.DataSource = ds.Tables[0];
    
answered by 20.04.2017 / 17:48
source
1
            DataTable tabla = new Datatable();
            using (SqlConnection conexion = new SqlConnection(datosConexion)) {
                conexion.Open();
                SqlDataAdapter adaptador = new SqlDataAdapter(query, conexion);
                adaptador.Fill(tabla);
            }
            dataGridView1.DataSource = tabla;
    
answered by 20.04.2017 в 18:02