Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to Auto Search and Edit Data Gridview Data in C#
WhatsApp
Pintoo Yadav
Jan 19
2015
2.7
k
0
1
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Data.OleDb;
using
System.IO;
namespace
ClientManagementApp
{
public
partial
class
SearchClient : Form
{
DataSet dstResults =
new
DataSet();
DataView myView;
public
SearchClient()
{
InitializeComponent();
}
public
static
OleDbConnection conection()
{
OleDbConnection con =
new
OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ClientMgmtDb1.accdb;"
);
if
(con.State == ConnectionState.Closed)
con.Open();
return
con;
}
private
void
txtSearch_KeyUp(
object
sender, KeyEventArgs e)
{
string
outputInfo =
""
;
string
[] keyWords = txtSearch.Text.Split(
' '
);
foreach
(
string
word
in
keyWords)
{
if
(outputInfo.Length == 0)
{
outputInfo =
"(NAME LIKE '%"
+ word +
"%' OR AMT LIKE '%"
+
word +
"%' OR NARAT LIKE '%"
+ word +
"%')"
;
}
else
{
outputInfo +=
" AND (NAME LIKE '%"
+ word +
"%' OR AMT LIKE '%"
+
word +
"%' OR NARAT LIKE '%"
+ word +
"%')"
;
}
}
myView.RowFilter = outputInfo;
}
private
void
SearchClient_Load(
object
sender, EventArgs e)
{
groupBox2.Visible =
false
;
// panel2.Visible = false;
ReadData(
"SELECT * FROM tbl_ClientDetails"
,
ref
dstResults,
"NAME"
);
myView = ((DataTable)dstResults.Tables[
"NAME"
]).DefaultView;
dataGridView1.DataSource = myView;
//dataGridView1.Refresh();
}
public
void
grid(
string
s)
{
try
{
//dataGridView1.DataSource = null;
string
connectionString = @
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ClientMgmtDb1.accdb;"
;
// OleDbConnection cnn = conection();
OleDbConnection cnn =
new
OleDbConnection(connectionString); ;
if
(cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
//cnn = new OleDbConnection(connectionString);
OleDbCommand cmd =
new
OleDbCommand(s, cnn);
//cnn.Open();
DataSet Ds =
new
DataSet();
OleDbDataAdapter da =
new
OleDbDataAdapter(cmd);
da.Fill(Ds);
dataGridView1.DataSource = Ds.Tables[0];
cnn.Close();
}
catch
(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public
void
ReadData(
string
strSQL,
ref
DataSet dstMain,
string
strTableName =
"default"
)
{
try
{
string
connectionString = @
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ClientMgmtDb1.accdb;"
;
// OleDbConnection cnn = conection();
OleDbConnection cnn =
new
OleDbConnection(connectionString); ;
if
(cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
//cnn = new OleDbConnection(connectionString);
OleDbCommand cmd =
new
OleDbCommand(strSQL, cnn);
//cnn.Open();
OleDbDataAdapter da =
new
OleDbDataAdapter(cmd);
da.Fill(dstMain, strTableName);
da.Dispose();
cnn.Close();
}
catch
(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private
void
txtSearch_TextChanged(
object
sender, EventArgs e)
{
txtSearch.BackColor = System.Drawing.Color.Yellow;
}
private
void
button1_Click(
object
sender, EventArgs e)
{
this
.Close();
}
private
void
button2_Click(
object
sender, EventArgs e)
{
groupBox2.Visible =
true
;
// groupBox1.Visible = true;
int
i;
i = dataGridView1.SelectedCells[0].RowIndex;
lblid.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
txt_name.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
txt_amt.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
cmb_AMCINC.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
txt_Amc.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
txt_payment.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
dt_PaymentRecvdDate.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
rtb_narat.Text = dataGridView1.Rows[i].Cells[7].Value.ToString();
txt_Vat.Text = dataGridView1.Rows[i].Cells[8].Value.ToString();
txt_stax.Text = dataGridView1.Rows[i].Cells[9].Value.ToString();
dt_W_E_F.Text = dataGridView1.Rows[i].Cells[10].Value.ToString();
txt_DSC2.Text = dataGridView1.Rows[i].Cells[11].Value.ToString();
txt_payrecvat.Text = dataGridView1.Rows[i].Cells[12].Value.ToString();
txtdsc3.Text = dataGridView1.Rows[i].Cells[13].Value.ToString();
}
private
void
btn_Save_Click(
object
sender, EventArgs e)
{
try
{
OleDbConnection con = conection();
OleDbCommand cmd;
DateTime TempdtPayementRecvdDate = Convert.ToDateTime(dt_PaymentRecvdDate.Text);
DateTime Tempdt_InstalledDate = Convert.ToDateTime(dt_W_E_F.Text);
// 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() + "";
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() +
""
;
cmd =
new
OleDbCommand(str, con);
int
i = cmd.ExecuteNonQuery();
if
(i >= 0)
{
MessageBox.Show(
"UPDATE Sucessfully "
,
"Information"
, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
grid(
"SELECT * FROM tbl_ClientDetails"
);
groupBox2.Visible =
false
;
}
}
catch
(Exception ex)
{
File.WriteAllText(
"log"
,
"Error :\n"
+ ex.Message);
}
}
private
void
btn_Delete_Click(
object
sender, EventArgs e)
{
try
{
OleDbConnection con = conection();
OleDbCommand cmd;
DateTime TempdtPayementRecvdDate = Convert.ToDateTime(dt_PaymentRecvdDate.Text);
DateTime Tempdt_InstalledDate = Convert.ToDateTime(dt_W_E_F.Text);
string
str =
"DELETE FROM tbl_ClientDetails WHERE ID="
+ lblid.Text.Trim() +
""
;
cmd =
new
OleDbCommand(str, con);
int
i = cmd.ExecuteNonQuery();
if
(i >= 0)
{
MessageBox.Show(
"DELETE Sucessfully "
,
"Information"
, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
grid(
"SELECT * FROM tbl_ClientDetails"
);
// panel2.Visible = false;
groupBox2.Visible =
false
;
}
}
catch
(Exception ex)
{
File.WriteAllText(
"log"
,
"Error :\n"
+ ex.Message);
}
}
private
void
btnexit_Click(
object
sender, EventArgs e)
{
this
.Close();
}
private
void
dataGridView1_CellContentClick(
object
sender, DataGridViewCellEventArgs e)
{
}
private
void
txt_Vat_TextChanged(
object
sender, EventArgs e)
{
txt_Vat.BackColor = System.Drawing.Color.Navy;
}
}
}
C#
Auto Search
Gridview
Up Next
How to Auto Search and Edit Data Gridview Data in C#