I have this code:
public void llenarDGVChoferes (string _comando)
{
//ComandoSQL para el dgvChoferes
var comando = _comando;
var datosConexion = new SqlConnection(stringconection); // Your Connection String here
//ComandoSQL para el dgvClientes
var dataAdapter = new SqlDataAdapter(comando, datosConexion);
var commandBuilder = new SqlCommandBuilder(dataAdapter);
var ds = new DataSet();
dataAdapter.Fill(ds);
dgvChoferes.ReadOnly = true;
dgvChoferes.DataSource = ds.Tables[0];
dgvChoferes.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}
Which you pass the SQL query in te _comando parameter and it fill the DataGridView called dgvChoferes with the query's result. This method works fine, but i'd like to know if there is any way to pass the DataGridView's name in the method's parameter. Something like this (pseudo-code)
public void llenarDGVChoferes (string _comando, dgvName) //dgvName = DataGridView's name
{
//ComandoSQL para el dgvChoferes
var comando = _comando;
var datosConexion = new SqlConnection(stringconnection); // Your Connection String here
//ComandoSQL para el dgvClientes
var dataAdapter = new SqlDataAdapter(comando, datosConexion);
var commandBuilder = new SqlCommandBuilder(dataAdapter);
var ds = new DataSet();
dataAdapter.Fill(ds);
dgvName.ReadOnly = true;
dgvName.DataSource = ds.Tables[0];
dgvName.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}
So I'll be able to fill any DataGridView with this method.