filter data in the datagridview that is already loaded c #

2

I currently have a grid that is automatically filled when I open the form, but when writing in the txt (code of the extinguisher) I need to filter in the grid depending on the id_extintor with the data already loaded in the grid (without connection to the database).

loading the grid first generated the dataset, then the datatable what I want to achieve is to filter the datagridview with the data already in it to avoid an overload of back and forth to the server, every time I write a number.

I currently load the grid in a class and I call it when I open the form.

private void cargar()
{
     grillas grilla = new grillas();
     grilla.llenargrillaextintorsincliente(grilla_cliente);
}

// 
public void llenargrillaextintorsincliente(DataGridView grillaparticular)
{
      try
      {
           //ejecuta sentencia sql y abre conexion
           int asignado = 2;
           int estado = 1;
           MySqlCommand cmd = new MySqlCommand("select a.id_extintor,a.rut_cliente,a.fecha_recarga,a.fecha_vencimiento as vencimiento,b.descripcion_kilo as kilo,c.descripcion_tipo as tipo,d.descripcion_estado as estado,e.descripcion_asignado as asignado from tb_extintores a INNER JOIN tb_kilos_extintor b ON a.id_kilo = b.id_kilo INNER JOIN tb_tipo_extintor c ON a.id_tipo = c.id_tipo INNER JOIN tb_estado d ON a.id_estado = d.id_estado INNER JOIN tb_extintor_asignado e ON a.id_asignado = e.id_asignado where a.id_asignado = '" + asignado + "' AND a.id_estado ='"+estado+"'", conexion.obtenerConexion());

            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataTable dt = new DataTable();

            da.Fill(dt);
            // devuelve la los datos a la grilla
            grillaparticular.DataSource = dt;

       }
       catch (Exception f)
       {
            MessageBox.Show("error");
       }
       finally
       {
            // con.Close();
            conexion.obtenerConexion().Close();
       }
}
    
asked by sebastian bizama inostroza 24.10.2017 в 19:44
source

3 answers

1

What can work is to put the dataTable as a public variable so that after you have filled it you can consult directly every need and you do not need to be making requests to the database

public  DataTable dt = new DataTable();
public void llenargrillaextintorsincliente(DataGridView grillaparticular)
{
  try
  {
       //ejecuta sentencia sql y abre conexion
       int asignado = 2;
       int estado = 1;
       MySqlCommand cmd = new MySqlCommand("select a.id_extintor,a.rut_cliente,a.fecha_recarga,a.fecha_vencimiento as vencimiento,b.descripcion_kilo as kilo,c.descripcion_tipo as tipo,d.descripcion_estado as estado,e.descripcion_asignado as asignado from tb_extintores a INNER JOIN tb_kilos_extintor b ON a.id_kilo = b.id_kilo INNER JOIN tb_tipo_extintor c ON a.id_tipo = c.id_tipo INNER JOIN tb_estado d ON a.id_estado = d.id_estado INNER JOIN tb_extintor_asignado e ON a.id_asignado = e.id_asignado where a.id_asignado = '" + asignado + "' AND a.id_estado ='"+estado+"'", conexion.obtenerConexion());

        MySqlDataAdapter da = new MySqlDataAdapter(cmd);


        da.Fill(dt);
        // devuelve la los datos a la grilla
        grillaparticular.DataSource = dt;

   }
   catch (Exception f)
   {
        MessageBox.Show("error");
   }
   finally
   {
        // con.Close();
        conexion.obtenerConexion().Close();
   }
}

Method to filter (for filtering I put as an example the click event of a button but you can occupy the event that suits you best):

private void Filtrar_Click(object sender, EventArgs e)
    {
        try
        {
           var lst = dt.Select(string.format("id_extintor = '{0}'",txt_buscar_extintor.Text);

           //Declaras un nuevo dataTable para asignarlo a grid y no afectar el original
           var table = new DataTable();
           var column = new DataColumn();
           //Agregas las columnas correspondientes al nuevo dataTable
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "id_extintor";
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "rut_cliente";
            table.Columns.Add(column);
            .
            .
            .

            //LLenas el dataTable con el resultado de la busqueda
            foreach (DataRow row in lst)
            {
                table.ImportRow(row);
            }
           //Asignas el nuevo dataTable al grid
          grillaparticular.DataSource = table;
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    
answered by 25.10.2017 в 09:05
0

Use the Select from the dataTables

DataRow[] result = dt.Select("id_extintor = '" + txt_buscar_extintor.Text + "'");
    
answered by 24.10.2017 в 20:20
0

The easiest way to filter a DataGridView loaded from a DataTable is to apply the filter to DefaultView of DataTable using the property Filter . In your case, depending on when you want to filter it, the code would be the following:

private void textBox1_TextChanged(object sender, EventArgs e)
{
     ((DataTable)grillaparticular.DataSource).DefaultView
                                         .RowFilter = "id_extintor=" + textBox1.Text;
}
    
answered by 25.10.2017 в 07:13