如果使用Datagridview在表2中存在列A的值,如何添加它

时间:2016-08-24 08:01:22

标签: c# datagridview

马布海!

最有效或更方便的方法是什么。

我有一个select查询并将其放在我的过滤器上的datagridview上。它有5列。我想知道来自该Datagridview的列CA是否已存在于第3列的表2中?

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=db;User ID=sa;Password=pw");
            // DateTime dt = new DateTime();
            //dt = DateTime.Now.ToString();
            SqlDataAdapter sda = new SqlDataAdapter("SELECT max(PurchaseOrder.POTitle) as Description,sum(PurchaseOrderEntry.Price *PurchaseOrderEntry.QuantityOrdered) as Amount, max(PurchaseOrder.PONumber)as PONumber, " +
                      " max(PurchaseOrderEntry.OrderNumber) as BoxCount, max(PurchaseOrderEntry.OrderNumber) as PLC,max(PurchaseOrderEntry.OrderNumber) as Branch, max(PurchaseOrderEntry.OrderNumber) as PreparedBy, max(PurchaseOrderEntry.OrderNumber) as CheckedBy " +
                      " FROM PurchaseOrder LEFT OUTER JOIN" +
                      " PurchaseOrderEntry ON PurchaseOrder.ID = PurchaseOrderEntry.PurchaseOrderID" +
                      " WHERE (PurchaseOrder.Remarks like '%" + tanggapan.Text + "%') AND (PurchaseOrder.DateCreated BETWEEN '" + dateTimePicker1.Text + "' AND '" + dateTimePicker2.Text + "' and PurchaseOrder.OtherStoreID = '" + branch.Text + "') Group By PurchaseOrder.PONumber", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;

这显示我的查询过滤器。如果我的记录中已经存在 PurchaseOrderEntry.OrderNumber 的任何提示,那么我可以管理哪一个重复。

谢谢! 克里斯

1 个答案:

答案 0 :(得分:0)

我现在在我的作品中添加了这段代码,现在正在使用。

 DataTable dt = new DataTable();
 dt.Clear();
 dt.Reset();

 con.Close();

 adaptors1.SelectCommand = con.CreateCommand();
 adaptors1.SelectCommand.CommandText = "Select TOP 1 [ponumber],[clref]  from [ISSPandayan].[dbo].[" + branch.Text + "] where [ponumber] = '" + dr.Cells["ponumber"].Value + "' ORDER BY [clref] ASC";
 adaptors1.Fill(dt); 

 // select query para malam kung existing ponumber
 if (dt.Rows.Count == 1)
 {
     adaptorss.InsertCommand.Parameters.Add("@already", SqlDbType.VarChar).Value = dt.Rows[0][1].ToString();
 }
 else
 {
     adaptorss.InsertCommand.Parameters.Add("@already", SqlDbType.VarChar).Value = " ";
 }

 //adaptorss.InsertCommand.Parameters.Add("@already", SqlDbType.VarChar).Value = "";

 //MessageBox.Show(Convert.ToString(dr.Cells["description"].Value));
 con.Close();
 con.Open();

 adaptorss.InsertCommand.ExecuteNonQuery();
 adaptorss.InsertCommand.Parameters.Clear();