Filter Bindingsource and datagridview

51 Views Asked by At

I am trying to filter a bindingsource and display it in a datagridview. But it doesn't work. The binding source is filled by a database request Here is my code :

    private void SecteurForm_Load(object sender, EventArgs e)
    {
        _bind.DataSource = _dbPreventif.T_PREVENTIF_COURANT.Where(e => e.PRE_COU_ID_SECTEUR == _idSecteur).ToList();

        advancedDataGridView1.DataSource = _bind;
        //dataGridView1.CellValueChanged += dataGridView1_CommentChanged;
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        advancedDataGridView1.DataSource = null;
        advancedDataGridView1.Rows.Clear();
        _bind.Filter = "PRE_COU_ID = 2339";
        advancedDataGridView1.DataSource = _bind;
    }

It's working if I add a row but not with the filter. Any idea ? Thanks

1

There are 1 best solutions below

0
Eraaz On

it works with a datatable

_liste = _dbPreventif.T_PREVENTIF_COURANT.Where(e => e.PRE_COU_ID_SECTEUR == _idSecteur).ToList();

        dataTable.Columns.Add("ID", typeof(int));
        dataTable.Columns.Add("Ligne", typeof(string));
        dataTable.Columns.Add("MaintenancePlan", typeof(int));
        dataTable.Columns.Add("Order", typeof(int));
        dataTable.Columns.Add("Date", typeof(DateTime));
        dataTable.Columns.Add("Description", typeof(string));
        dataTable.Columns.Add("Description2", typeof(string));
        dataTable.Columns.Add("Priorite", typeof(string));
        dataTable.Columns.Add("Commentaire", typeof(string));

        foreach (T_PREVENTIF_COURANT item in _liste)
        {
            DataRow newRow = dataTable.NewRow();

            newRow["ID"] = item.PRE_COU_ID;
            newRow["Ligne"] = item.PRE_COU_LIGNE;
            newRow["MaintenancePlan"] = item.PRE_COU_MAINTENANCE_PLAN;
            newRow["Order"] = item.PRE_COU_ORDRE;
            newRow["Date"] = item.PRE_COU_DATE;
            newRow["Description"] = item.PRE_COU_DESCRIPTION;
            newRow["Description2"] = item.PRE_COU_DESCRIPTION_2;
            newRow["Priorite"] = item.PRE_COU_PRIORITE;
            newRow["Commentaire"] = item.PRE_COU_COMMENT;

            dataTable.Rows.Add(newRow);
        }