Filter DataGridView

Sushil Agarwal 381 Reputation points
2022-11-01T08:55:40.753+00:00

256012-picklist1.png
255979-pick-complete.png
256003-picklist-flter-fail.png

Hello Experts,
I hava a datagridview dgvPickList which is mapped to a data table dtPickList whose fields are as under
internal class PicklistItem
{
public int srl { get; set; }
public decimal qtyPickedTotal { get; set; } = 0;
public decimal Qty { get; set; }
public decimal MRP_Unit { get; set; } = 0;
public decimal MrpScaned { get; set; }

        public string part_number { get; set; }  
        public string Part_name { get; set; }  

        //public string Rack { get; set; }  
        //public string Bin { get; set; }  
        public decimal Rate { get; set; }  

        public decimal Curr_Stock { get; set; }  
    }  

after some Q.R.Code scanning the dgvpicklist fields MrpScaned and qtyPickedTotal are updated

on the data entry form i have set a button to filter dgvPicklist

but this filter is not working correct. it shows at least on row

i want to display rows whose qty <>qtyPickedTotal and MRP_Unit <>MrpScaned

can some bodu guide what should be best way to do this ?

        private void btnCheckPickOk_Click(object sender, EventArgs e)  
        {  
            string query;  
            if (string.IsNullOrEmpty((dgvPickList.DataSource as DataTable).DefaultView.RowFilter))  
            {  
                query = "qtyPickedTotal <> Qty and MrpScaned<>MRP_Unit";  
                lblFilter.Text = "Filtered Pick List";  
            }  
            else  
            {  
                query = string.Empty;  
                lblFilter.Text = "UnFiltered Pick List";  
            }  
            (dgvPickList.DataSource as DataTable).DefaultView.RowFilter = query;  
}  

if you see attached 256012-picklist1.png its 1st appearance
then after complete scan all rows become goldeen
lastly button to check if really on rows have met the condition [picklist can be large of 100 items],

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,839 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-11-22T06:37:47.087+00:00

    @Sushil Agarwal , Sorry for the late response, based on my test, I almost reproduced your problem.

    Please remove the rows in dgvPickList after you press the tnCheckPickOk if you want to show no records in dgvPickList.

    You could try the following code:

      private void btnCheckPickOk_Click(object sender, EventArgs e)  
            {  
                string query;  
                //dgvPickList.DataSource as DataTable).DefaultView.RowFilter)  
                if (string.IsNullOrWhiteSpace((dgvPickList.DataSource as DataTable).DefaultView.RowFilter))  
                {  
                    query = "qtyPickedTotal <> Qty and MrpScaned<>MRP_Unit";  
                    lblFilter.Text = "Filtered Pick List";  
                }  
                else  
                {  
                    query = string.Empty;  
                    lblFilter.Text = "UnFiltered Pick List";  
                }  
                //(dgvPickList.DataSource as DataTable).DefaultView.RowFilter = query;  
                (dgvPickList.DataSource as DataTable).DefaultView.RowFilter = query;  
                dgvPickList.Refresh();  
      
                int index = 0;  
                int rowsCount = dgvPickList.Rows.Count - 1;  
                lblFilter.Text += " " + rowsCount.ToString() + "/" + dtpicklistItems.Rows.Count.ToString();  
                if (rowsCount > 0)  
                {  
                    for (int item = 0; item < rowsCount; item++)  
                    {  
                        //verify if the next rows is visible  
                        if (dgvPickList.Rows[(item + 1)].Visible)  
                        {  
                            decimal.TryParse(dgvPickList["qtyPickedTotal", item].Value.ToString(), out decimal qtyPickedTotal);  
                            decimal.TryParse(dgvPickList["Qty", item].Value.ToString(), out decimal Qty);  
                            decimal.TryParse(dgvPickList["MRP_Unit", item].Value.ToString(), out decimal MRP_Unit);  
                            decimal.TryParse(dgvPickList["MrpScaned", item].Value.ToString(), out decimal MrpScaned);  
                            if (qtyPickedTotal.Equals(Qty)  
                                && MRP_Unit.Equals(MrpScaned))  
                            {  
                                dgvPickList.Rows[item].DefaultCellStyle.BackColor = Color.Gold;  
                                index = item;  
                            }  
                                 
                        }  
                    }  
                }  
                dgvPickList.Rows.RemoveAt(index);  
      
            }  
    

    Result:
    262916-1.gif

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments