2017-09-16 55 views
1

你好,我有一个POS系统有两个datagridview,当我扫描条形码时,产品通过textchanged事件自动添加到购物车datagridview。C#Winforms。当按下清除按钮时,textchanged事件将停止工作

它工作正常,但当我点击“清除按钮”时,textchanged事件将停止工作。任何想法表示感谢你。

清除按钮代码:

private void btnClearcart_Click(object sender, EventArgs e) 
    { 
     dgvPOScart.Rows.Clear(); 
     dgvPOScart.Refresh(); 

     if (dgvPOSproduct.Rows.Count > 0) 
     { 
      dgvPOSproduct.DataSource = null; 
     } 

      DataTable dt = new DataTable("Products"); 

      using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString)) 
      { 
       if (cnn.State == ConnectionState.Closed) 
        cnn.Open(); 
       using (SqlDataAdapter da = new SqlDataAdapter("Select ProductID, BrandName, GenericName, Quantity, SellingPrice, Dosage, Form, S,P, VE , Barcode , Category , Description from Products where Status = 'Active' and Quantity > 0", cnn)) 
       { 
        da.Fill(dt); 
        dgvPOSproduct.DataSource = dt; 
        productwidth(); 

       } 
      } 

    } 

代码来填充产品datagridview的形式负载:

DataTable dt = new DataTable("Products"); 

    private void dgvProductNew() 
    { 
     try 
     { 
      using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString)) 
      { 
       if (cnn.State == ConnectionState.Closed) 
        cnn.Open(); 
       using (SqlDataAdapter da = new SqlDataAdapter("Select ProductID, BrandName, GenericName, Quantity, SellingPrice, Dosage, Form, S,P, VE , Barcode , Category , Description from Products where Status = 'Active' and Quantity > 0", cnn)) 
       { 
        da.Fill(dt); 
        dgvPOSproduct.DataSource = dt; 

        productwidth(); 

       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

TextChanged事件代码:

private void txtBarcodeSearch_TextChanged(object sender, EventArgs e) 
    { 
     DataView dv = dt.DefaultView; 
     selectedRow = null; 

     dv.RowFilter = string.Format("Barcode like '{0}%' ", txtBarcodeSearch.Text); 
     productwidth(); 

     if (txtBarcodeSearch.Text.Length == 13) 
     { 

      if (dgvPOSproduct.Rows.Count == 1) 
      { 
       selectedRow = 0; 
      } 

      if (selectedRow.HasValue) 
      { 
       addcartbarcode(); 
       txtBarcodeSearch.Clear(); 
      } 

     } 
    } 
+0

方法productwidth()的实现是什么? –

+0

这只是产品datagridview先生中列宽的设置。 – StudentDev

+0

你确定textchanged事件是什么不工作或扫描本身? –

回答

0

你检查把一个调试器上框TextChanged事件?因为它必须触发改变文本框的文本。

所以,我认为这不是你的问题。

但以下可能是你的问题,如果我没有错。

您的txtBarcodeSearch_TextChanged事件总是触发,行过滤器也会根据需要过滤行,但不是相同的对象即dt。 这意味着你没有过滤相同的数据表对象。因为你已经宣布了DT对象两次,一个公众和当你清除按钮单击以

private void btnClearcart_Click(object sender, EventArgs e) 
{ 
    dgvPOScart.Rows.Clear(); 
    dgvPOScart.Refresh(); 

    if (dgvPOSproduct.Rows.Count > 0) 
    { 
     dgvPOSproduct.DataSource = null; 
    } 

     //DataTable dt = new DataTable("Products"); //this was your issue 
     dt = new DataTable("Products");   //this will work 

     using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString)) 
     { 
      if (cnn.State == ConnectionState.Closed) 
       cnn.Open(); 
      using (SqlDataAdapter da = new SqlDataAdapter("Select ProductID, BrandName, GenericName, Quantity, SellingPrice, Dosage, Form, S,P, VE , Barcode , Category , Description from Products where Status = 'Active' and Quantity > 0", cnn)) 
      { 
       da.Fill(dt); 
       dgvPOSproduct.DataSource = dt; 
       productwidth(); 

      } 
     } 

} 

btnClearcart_Click事件的其他私人因此,清除按钮后点击您的datagridview绑定到一个新的DataTable实例每次单击在清除按钮上。但是你的textchage事件过滤了第一个dt对象(datatable),它不再绑定到datagridview。

+0

是的,先生,我真的认为datatable也是问题,你是对的。现在已经修好了,非常感谢你! – StudentDev

+0

对于迟到的接受答案感到抱歉。我最近很忙。 – StudentDev

+0

欢迎您,不错。 –