Black Diamond

Black Diamond

  • NA
  • 236
  • 106.7k

Auto Complete Datagridview using sql server.

Feb 28 2018 7:47 PM
Hi ! 
 
Good day everyone,
 
It is possible to make my datagridview cell auto complete drop the data read coming from the database sql server? 
 
How to code this one?
 
This is my code I used to insert the data to sql server... I want every cell autodrop the data which save from database sql server every time I input something in a cell.
 
Hope anyone can help me.
 
Thank you! 
  1. public void LoadDgMaintenance()  
  2. {  
  3. try  
  4. {  
  5. dataGridView1.ColumnCount = 14;  
  6. dataGridView1.Columns[0].Name = "Particular";  
  7. dataGridView1.Columns[0].Width = 180;  
  8. dataGridView1.Columns[1].Name = "QTY";  
  9. dataGridView1.Columns[1].Width = 180;  
  10. dataGridView1.Columns[2].Name = "Unit";  
  11. dataGridView1.Columns[2].Width = 180;  
  12. dataGridView1.Columns[3].Name = "Code";  
  13. dataGridView1.Columns[3].Width = 180;  
  14. dataGridView1.Columns[4].Name = "Type";  
  15. dataGridView1.Columns[4].Width = 180;  
  16. dataGridView1.Columns[5].Name = "Vendor";  
  17. dataGridView1.Columns[5].Width = 180;  
  18. dataGridView1.Columns[6].Name = "Vendor Add";  
  19. dataGridView1.Columns[6].Width = 180;  
  20. dataGridView1.Columns[7].Name = "Vendor Tel. No.";  
  21. dataGridView1.Columns[7].Width = 180;  
  22. dataGridView1.Columns[8].Name = "Serial No.";  
  23. dataGridView1.Columns[8].Width = 180;  
  24. dataGridView1.Columns[9].Name = "Unit Price";  
  25. dataGridView1.Columns[9].Width = 180;  
  26. dataGridView1.Columns[10].Name = "Amount";  
  27. dataGridView1.Columns[10].Width = 180;  
  28. dataGridView1.Columns[11].Name = "Remarks";  
  29. dataGridView1.Columns[11].Width = 180;  
  30. dataGridView1.Columns[12].Name = "Req. Branch";  
  31. dataGridView1.Columns[12].Width = 180;  
  32. ////dataGridView1.Columns[13].Name = "Date Added";  
  33. ////dataGridView1.Columns[13].Width = 180;  
  34. ////dataGridView1.Columns[14].Name = "Date Purchase";  
  35. ////dataGridView1.Columns[14].Width = 180;  
  36. dataGridView1.Columns[13].Name = "Other Notes";  
  37. dataGridView1.Columns[13].Width = 180;  
  38. }  
  39. catch (Exception ex) { MessageBox.Show(ex.Message); }  
  40. }  
  41. using (connect = new SqlConnection(ItemformConn.connection))  
  42. {  
  43. connect.Close();  
  44. using (SqlCommand command = new SqlCommand())  
  45. {  
  46. command.Connection = connect;  
  47. command.CommandText = @"INSERT INTO tblReceivingItem([ItemInvID],[ItemRefNo],[ItemPONo],[ItemParticulars],[ItemQTY],[ItemUnit],[ItemCode],[ItemType],[ItemVendor],[ItemVendorAdd],[ItemVendorTelNo],[ItemSerialNo],[ItemUnitPrice],[ItemAmount],[ItemRemarks],[ItemBranchReq],[ItemDateRecieved],[ItemDatePurchased],[ItemOthers]) VALUES(@ItemInvID,@ItemRefNo,@ItemPONo,@ItemParticulars,@ItemQTY,@ItemUnit,@ItemCode,@ItemType,@ItemVendor,@ItemVendorAdd,@ItemVendorTelNo,@ItemSerialNo,@ItemUnitPrice,@ItemAmount,@ItemRemarks,@ItemBranchReq,@ItemDateRecieved,@ItemDatePurchased,@ItemOthers)";  
  48. try  
  49. {  
  50. foreach (DataGridViewRow row in dataGridView1.Rows)  
  51. {  
  52. if (!row.IsNewRow)  
  53. {  
  54. //=====================================================Check duplication============================  
  55. SqlCommand cm;  
  56. SqlDataReader dr;  
  57. string sql = @"Select * from tblReceivingItem where ItemInvID like '" + IdHolder + "'";  
  58. cm = new SqlCommand(sql, connect);  
  59. connect.Open();  
  60. dr = cm.ExecuteReader();  
  61. dr.Read();  
  62. if (dr.HasRows)  
  63. {  
  64. generateItemIDRevDG();  
  65. dr.Close();  
  66. connect.Close();  
  67. }  
  68. dr.Close();  
  69. connect.Close();  
  70. //===================================================End Checking duplicaiton=================  
  71. command.Parameters.Clear();  
  72. SqlParameter unitsParamID = command.Parameters.AddWithValue("@ItemInvID", IdHolder);  
  73. if(IdHolder==null){ unitsParamID.Value = DBNull.Value; }  
  74. SqlParameter unitsParamRef = command.Parameters.AddWithValue("@ItemRefNo", lblRefNo.Text);  
  75. if (lblRefNo.Text == null) { unitsParamRef.Value = DBNull.Value; }  
  76. SqlParameter unitsParamPO = command.Parameters.AddWithValue("@ItemPONo", txtPONo.Text);  
  77. if (txtPONo.Text == null) { unitsParamPO.Value = DBNull.Value; }  
  78. SqlParameter unitsParamRec = command.Parameters.AddWithValue("@ItemDateRecieved", txtDatePurch.Text);  
  79. if (txtDatePurch.Text== null) { unitsParamRec.Value = DBNull.Value; }  
  80. SqlParameter unitsParamPur = command.Parameters.AddWithValue("@ItemDatePurchased", lblDateOn.Text);  
  81. if (lblDateOn .Text== null) { unitsParamPur.Value = DBNull.Value; }  
  82. SqlParameter unitsParamPar = command.Parameters.AddWithValue("@ItemParticulars", row.Cells[0].Value);  
  83. if (row.Cells[0].Value == null) { unitsParamPar.Value = DBNull.Value; }  
  84. SqlParameter unitsParamQTY = command.Parameters.AddWithValue("@ItemQTY", row.Cells[1].Value);  
  85. if (row.Cells[1].Value == null) { unitsParamQTY.Value = DBNull.Value; }  
  86. SqlParameter unitsParamUnit = command.Parameters.AddWithValue("@ItemUnit", row.Cells[2].Value);  
  87. if (row.Cells[2].Value == null) { unitsParamUnit.Value = DBNull.Value; }  
  88. SqlParameter unitsParamCode = command.Parameters.AddWithValue("@ItemCode", row.Cells[3].Value);  
  89. if (row.Cells[3].Value == null) { unitsParamCode.Value = DBNull.Value; }  
  90. SqlParameter unitsParamtype = command.Parameters.AddWithValue("@ItemType", row.Cells[4].Value);  
  91. if (row.Cells[4].Value == null) { unitsParamtype.Value = DBNull.Value; }  
  92. SqlParameter unitsParamven = command.Parameters.AddWithValue("@ItemVendor", row.Cells[5].Value);  
  93. if (row.Cells[5].Value == null) { unitsParamven.Value = DBNull.Value; }  
  94. SqlParameter unitsParamvenadd = command.Parameters.AddWithValue("@ItemVendorAdd", row.Cells[6].Value);  
  95. if (row.Cells[6].Value == null) { unitsParamvenadd.Value = DBNull.Value; }  
  96. SqlParameter unitsParamtelno = command.Parameters.AddWithValue("@ItemVendorTelNo", row.Cells[7].Value);  
  97. if (row.Cells[7].Value == null) { unitsParamtelno.Value = DBNull.Value; }  
  98. SqlParameter unitsParamSer = command.Parameters.AddWithValue("@ItemSerialNo", row.Cells[8].Value);  
  99. if (row.Cells[8].Value == null) { unitsParamSer.Value = DBNull.Value; }  
  100. SqlParameter unitsParamPrice = command.Parameters.AddWithValue("@ItemUnitPrice", row.Cells[9].Value);  
  101. if (row.Cells[9].Value == null) { unitsParamPrice.Value = DBNull.Value; }  
  102. SqlParameter unitsParamamount = command.Parameters.AddWithValue("@ItemAmount", row.Cells[10].Value);  
  103. if (row.Cells[10].Value == null) { unitsParamamount.Value = DBNull.Value; }  
  104. SqlParameter unitsParamRem = command.Parameters.AddWithValue("@ItemRemarks", row.Cells[11].Value);  
  105. if (row.Cells[11].Value == null) { unitsParamRem.Value = DBNull.Value; }  
  106. SqlParameter unitsParamReq = command.Parameters.AddWithValue("@ItemBranchReq", row.Cells[12].Value);  
  107. if (row.Cells[12].Value == null) { unitsParamReq.Value = DBNull.Value; }  
  108. SqlParameter unitsParamOther = command.Parameters.AddWithValue("@ItemOthers", row.Cells[13].Value);  
  109. if (row.Cells[13].Value == null) { unitsParamOther.Value = DBNull.Value; }  
  110. connect.Open();  
  111. command.ExecuteNonQuery();  
  112. connect.Close();  
  113. }  
  114. }  
  115. btnAddNew.Enabled = true;  
  116. btnAdd.Enabled = false;  
  117. btnUpdate.Enabled = false;  
  118. btnDelete.Enabled = false;  
  119. }  
  120. catch (Exception ex) { MessageBox.Show(ex.Message); return; }finally { MessageBox.Show("Successfull Added!"); dataGridView1.Rows.Clear(); dataGridView1.Refresh(); }  
  121. }  
  122. }  

Answers (1)