How to Auto Search and Edit Data Gridview Data in C#

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.OleDb;  
  10. using System.IO;  
  11. namespace ClientManagementApp  
  12. {  
  13.     public partial class SearchClient : Form  
  14.     {  
  15.         DataSet dstResults = new DataSet();  
  16.         DataView myView;  
  17.         public SearchClient()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.         public static OleDbConnection conection()  
  22.         {  
  23.             OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ClientMgmtDb1.accdb;");  
  24.             if (con.State == ConnectionState.Closed)  
  25.                 con.Open();  
  26.             return con;  
  27.         }  
  28.         private void txtSearch_KeyUp(object sender, KeyEventArgs e)  
  29.         {  
  30.             string outputInfo = "";  
  31.             string[] keyWords = txtSearch.Text.Split(' ');  
  32.             foreach (string word in keyWords)  
  33.             {  
  34.                 if (outputInfo.Length == 0)  
  35.                 {  
  36.                     outputInfo = "(NAME LIKE '%" + word + "%' OR AMT LIKE '%" +  
  37.                     word + "%' OR NARAT LIKE '%" + word + "%')";  
  38.                 }  
  39.                 else  
  40.                 {  
  41.                     outputInfo += " AND (NAME LIKE '%" + word + "%' OR AMT LIKE '%" +  
  42.                     word + "%' OR NARAT LIKE '%" + word + "%')";  
  43.                 }  
  44.             }  
  45.             myView.RowFilter = outputInfo;  
  46.         }  
  47.         private void SearchClient_Load(object sender, EventArgs e)  
  48.         {  
  49.             groupBox2.Visible = false;  
  50.             // panel2.Visible = false;  
  51.             ReadData("SELECT * FROM tbl_ClientDetails"ref dstResults, "NAME");  
  52.             myView = ((DataTable)dstResults.Tables["NAME"]).DefaultView;  
  53.             dataGridView1.DataSource = myView;  
  54.             //dataGridView1.Refresh();  
  55.         }  
  56.         public void grid(string s)  
  57.         {  
  58.             try  
  59.             {  
  60.                 //dataGridView1.DataSource = null;  
  61.                 string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ClientMgmtDb1.accdb;";  
  62.                 // OleDbConnection cnn = conection();  
  63.                 OleDbConnection cnn = new OleDbConnection(connectionString); ;  
  64.                 if (cnn.State == ConnectionState.Closed)  
  65.                 {  
  66.                     cnn.Open();  
  67.                 }  
  68.                 //cnn = new OleDbConnection(connectionString);  
  69.                 OleDbCommand cmd = new OleDbCommand(s, cnn);  
  70.                 //cnn.Open();  
  71.                 DataSet Ds = new DataSet();  
  72.                 OleDbDataAdapter da = new OleDbDataAdapter(cmd);  
  73.                 da.Fill(Ds);  
  74.                 dataGridView1.DataSource = Ds.Tables[0];  
  75.                 cnn.Close();  
  76.             }  
  77.             catch (Exception ex)  
  78.             {  
  79.                 Console.WriteLine(ex.ToString());  
  80.             }  
  81.         }  
  82.         public void ReadData(string strSQL, ref DataSet dstMain, string strTableName = "default")  
  83.         {  
  84.             try  
  85.             {  
  86.                 string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ClientMgmtDb1.accdb;";  
  87.                 // OleDbConnection cnn = conection();  
  88.                 OleDbConnection cnn = new OleDbConnection(connectionString); ;  
  89.                 if (cnn.State == ConnectionState.Closed)  
  90.                 {  
  91.                     cnn.Open();  
  92.                 }  
  93.                 //cnn = new OleDbConnection(connectionString);  
  94.                 OleDbCommand cmd = new OleDbCommand(strSQL, cnn);  
  95.                 //cnn.Open();  
  96.                 OleDbDataAdapter da = new OleDbDataAdapter(cmd);  
  97.                 da.Fill(dstMain, strTableName);  
  98.                 da.Dispose();  
  99.                 cnn.Close();  
  100.             }  
  101.             catch (Exception ex)  
  102.             {  
  103.                 Console.WriteLine(ex.ToString());  
  104.             }  
  105.         }  
  106.         private void txtSearch_TextChanged(object sender, EventArgs e)  
  107.         {  
  108.             txtSearch.BackColor = System.Drawing.Color.Yellow;  
  109.         }  
  110.         private void button1_Click(object sender, EventArgs e)  
  111.         {  
  112.             this.Close();  
  113.         }  
  114.         private void button2_Click(object sender, EventArgs e)  
  115.         {  
  116.             groupBox2.Visible = true;  
  117.             // groupBox1.Visible = true;  
  118.             int i;  
  119.             i = dataGridView1.SelectedCells[0].RowIndex;  
  120.             lblid.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();  
  121.             txt_name.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();  
  122.             txt_amt.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();  
  123.             cmb_AMCINC.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();  
  124.             txt_Amc.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();  
  125.             txt_payment.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();  
  126.             dt_PaymentRecvdDate.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();  
  127.             rtb_narat.Text = dataGridView1.Rows[i].Cells[7].Value.ToString();  
  128.             txt_Vat.Text = dataGridView1.Rows[i].Cells[8].Value.ToString();  
  129.             txt_stax.Text = dataGridView1.Rows[i].Cells[9].Value.ToString();  
  130.             dt_W_E_F.Text = dataGridView1.Rows[i].Cells[10].Value.ToString();  
  131.             txt_DSC2.Text = dataGridView1.Rows[i].Cells[11].Value.ToString();  
  132.             txt_payrecvat.Text = dataGridView1.Rows[i].Cells[12].Value.ToString();  
  133.             txtdsc3.Text = dataGridView1.Rows[i].Cells[13].Value.ToString();  
  134.         }  
  135.         private void btn_Save_Click(object sender, EventArgs e)  
  136.         {  
  137.             try  
  138.             {  
  139.                 OleDbConnection con = conection();  
  140.                 OleDbCommand cmd;  
  141.                 DateTime TempdtPayementRecvdDate = Convert.ToDateTime(dt_PaymentRecvdDate.Text);  
  142.                 DateTime Tempdt_InstalledDate = Convert.ToDateTime(dt_W_E_F.Text);  
  143.                 // string str = " update tbl_ClientDetails set BankName='" + txtbankName.Text.Trim() + "',ContactPersonName='" + txt_contactPerson.Text + "',ContactNumber='" + txt_contactNo.Text + "',EmailId='" + txt_emailId.Text + "',PORecieved='" + cmb_PORecieved.Text + "',PayementRecievedDate='" + TempdtPayementRecvdDate.ToShortDateString() + "',Remark ='" + rtb_remark.Text + "',EToken='" + txt_eToken.Text + "',DigitalCertificate='" + txt_DigitalCert.Text + "',ProductKey='" + txt_ProductKey.Text + "',UserKey='" + txt_UserKey.Text + "',InstalledDate='" + Tempdt_InstalledDate.ToShortDateString() + "' where ID=" + lblid.Text.Trim() + "";  
  144.                 string str = " update tbl_ClientDetails set NAME='" + txt_name.Text.Trim() + "',AMT='" + txt_amt.Text + "',AMC_INC='" + cmb_AMCINC.Text + "',AMC='" + txt_Amc.Text + "',PAYMENT_RECVD='" + txt_payment.Text + "',PayementRecievedDate='" + TempdtPayementRecvdDate.ToShortDateString() + "',NARAT ='" + rtb_narat.Text.Trim() + "',VAT='" + txt_Vat.Text + "',S_T='" + txt_stax.Text + "',W_E_F='" + Tempdt_InstalledDate.ToShortDateString() + "',PAYMENT_RECVD_vat='" + txt_payrecvat.Text + "',DSC_2='" + txt_DSC2.Text.Trim() + "',DSC_3='" + txtdsc3.Text.Trim() + "' where ID=" + lblid.Text.Trim() + "";  
  145.                 cmd = new OleDbCommand(str, con);  
  146.                 int i = cmd.ExecuteNonQuery();  
  147.                 if (i >= 0)  
  148.                 {  
  149.                     MessageBox.Show("UPDATE Sucessfully ""Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);  
  150.                     grid("SELECT * FROM tbl_ClientDetails");  
  151.                     groupBox2.Visible = false;  
  152.                 }  
  153.             }  
  154.             catch (Exception ex)  
  155.             {  
  156.                 File.WriteAllText("log""Error :\n" + ex.Message);  
  157.             }  
  158.         }  
  159.         private void btn_Delete_Click(object sender, EventArgs e)  
  160.         {  
  161.             try  
  162.             {  
  163.                 OleDbConnection con = conection();  
  164.                 OleDbCommand cmd;  
  165.                 DateTime TempdtPayementRecvdDate = Convert.ToDateTime(dt_PaymentRecvdDate.Text);  
  166.                 DateTime Tempdt_InstalledDate = Convert.ToDateTime(dt_W_E_F.Text);  
  167.                 string str = "DELETE FROM tbl_ClientDetails WHERE ID=" + lblid.Text.Trim() + "";  
  168.                 cmd = new OleDbCommand(str, con);  
  169.                 int i = cmd.ExecuteNonQuery();  
  170.                 if (i >= 0)  
  171.                 {  
  172.                     MessageBox.Show("DELETE Sucessfully ""Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);  
  173.                     grid("SELECT * FROM tbl_ClientDetails");  
  174.                     // panel2.Visible = false;  
  175.                     groupBox2.Visible = false;  
  176.                 }  
  177.             }  
  178.             catch (Exception ex)  
  179.             {  
  180.                 File.WriteAllText("log""Error :\n" + ex.Message);  
  181.             }  
  182.         }  
  183.         private void btnexit_Click(object sender, EventArgs e)  
  184.         {  
  185.             this.Close();  
  186.         }  
  187.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)  
  188.         {  
  189.         }  
  190.         private void txt_Vat_TextChanged(object sender, EventArgs e)  
  191.         {  
  192.             txt_Vat.BackColor = System.Drawing.Color.Navy;  
  193.         }  
  194.     }