question

SushilAgarwal-8271 avatar image
0 Votes"
SushilAgarwal-8271 asked JackJJun-MSFT commented

Filter DataGridView

![256012-picklist1.png][1]
![255979-pick-complete.png][2]
![256003-picklist-flter-fail.png][3]

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
[1]: /answers/storage/attachments/256012-picklist1.png
then after complete scan all rows become goldeen
[2]: /answers/storage/attachments/255979-pick-complete.png
lastly button to check if really on rows have met the condition [picklist can be large of 100 items],
[3]: /answers/storage/attachments/256003-picklist-flter-fail.png

windows-forms
picklist1.png (43.0 KiB)
pick-complete.png (43.8 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @SushilAgarwal-8271,What does your srl do? What should the correct result of your query for this statement be?

0 Votes 0 ·

srl is serial number it has no significance in the current problem context.

correct result i am willing is to get only filtered rows whose qty is not euval to qtyPickedTotal and MRP_Unit ot equal to MrpScaned. all 4 columns decimal values need to be compared.

unfortunetly i get one row which meets the above criteria but still shown on filter, whats wrong is happening is not understood. 256661-70260-short-parts-scans.txt256589-70260-parts-scans.txt


256636-70260-short-csv.txt


0 Votes 0 ·

In my tests, your problem was not reproduced.

 private void Form1_Load(object sender, EventArgs e)
 {
     string co = @"Data Source=(localdb)\ProjectModels;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
     SqlConnection conn = new SqlConnection(co);
     SqlDataAdapter sda = new SqlDataAdapter("select * from dbo.TMPdemo", conn);
     DataSet ds = new DataSet();
     sda.Fill(ds, "TMPdemo");
     dataGridView1.DataSource = ds.Tables[0];
     dataGridView1.AllowUserToAddRows = false;
 }
    
 private void button1_Click(object sender, EventArgs e)
 {
    
     string query;
     if (string.IsNullOrEmpty((dataGridView1.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";
     }
      (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = query;
 }

enter image description here


0 Votes 0 ·
Show more comments

@SushilAgarwal-8271, is any update? Do you have time to check if my answer works for you?

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@SushilAgarwal-8271, 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.


1.gif (597.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.