I have a search textbox in my windows form application and I want to search character by character means when I write h in textbox then shows result in datagridview of h and when I add h with a like ha in search textbox then shows result in datagridview of ha and its change results in datagridview from h to ha as same as mobile phone contacts searching. and I working like below:
public partial class Form2 : Form
{
SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\Administrator\\Documents\\Contact.mdf;Integrated Security=True;");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
bindDatagridview();
if (textBox1.Text != string.Empty)
{
search();
}
}
public void bindDatagridview()
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = new SqlCommand("Select * from contactsinfo", connection);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
//dataGridView1.DataBindings();
}
public void search()
{
da.SelectCommand = new SqlCommand("Select * from contactsinfo where ContactName = '" + textBox1.Text + "'", connection);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
clear();
}
}
But this only work when the form is load and the form is only loaded at first time:
Kindly suggest me what i do, waiting for your reply.
Thanks.
If you can load all contacts at once, then its simple. On form load get all contacts and save them in DataView. Then bind grid to this view:
DataView contactsView;
private void Form2_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
var da = new SqlDataAdapter("SELECT * FROM from contactsinfo", conn);
da.Fill(dt);
}
contactsView = dt.AsDataView();
dataGridView1.DataSource = contactsView;
}
Then just change row filter of DataView when text changes in filter TextBox:
private void textBox1_TextChanged(object sender, EventArgs e)
{
contactsView.RowFilter =
String.Format("ContactName LIKE '{0}%'", textBox1.Text);
}
If you can't pre-load all data, then you should use same TextChanged event to query for filtered data.
NOTE: You should handle ' in user input.
Related
Is there a way to display data from the database into the textbox but every time you click NEXT new row displays. I have this code but it doesn't work like I want it to because it displays all data into a textbox and not one row at a time.
private void buttonNEXT_Click(object sender, EventArgs e)
{
SQLiteConnection conn = new SQLiteConnection("data source = people.sqlite");
conn.Open();
SQLiteCommand com = new SQLiteCommand(conn);
com.CommandText = "SELECT id, name, surname FROM people;";
SQLiteDataReader reader = com.ExecuteReader();
while (reader.Read())
{
textBox1.Text += reader["id"].ToString();
textBox2.Text += reader["name"].ToString();
textBox3.Text += reader["surname"].ToString();
}
conn.Close();
}
If you want to have a Next and Previous button and don't want to use a BindingNavigator control on your form, you can use a DataSet to store data and an SQLiteDataAdapter to fill it.
So you can have a function to fill and return a data set:
public DataSet GetDataSet(string ConnectionString, string SQL)
{
SQLiteConnectionconn = new SQLiteConnection(ConnectionString);
SQLiteDataAdapter da = new SQLiteDataAdapter ();
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = SQL;
da.SelectCommand = cmd;
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
return ds;
}
Then you should keep its returns value to a global variable in your main function like FormLoad :
dataset ds; // global variable
int index = 0; // for loop over dataset
private void frm_Load(object sender, EventArgs e)
{
ds = GetDataSet("data source = people.sqlite","SELECT id, name, surname FROM people;");
}
And in your button click:
private void buttonNEXT_Click(object sender, EventArgs e)
{
textBox1.Text = ds.Tables["People"].Rows[i]["id"].ToString();
textBox2.Text = ds.Tables["People"].Rows[i]["name"].ToString();
textBox3.Text = ds.Tables["People"].Rows[i]["surname"].ToString();
i++;
}
Note: You should check for i variable value by each click, it should not be bigger than ds.Tables["People"].Rows.Count
Do the same for previous button if you want it also.
Now I'm trying to solve this problem. But I don't know why it didn't work.
I've finished to change data in datagridview form, but the sqlite file in desktop wasn't changed.
namespace Ex1
{
public partial class Form1 : Form
{
int initValue = 0;
String chkString;
SQLiteConnection conn = new SQLiteConnection("Data Source=I:\\Coding\\VS\\Study1\\Ex1\\Ex1\\Test.db;Version=3;");
SQLiteDataAdapter adapter = null;
BindingSource bsOrigin = null;
DataSet ds = null;
Random rand = new Random();
KnownColor[] Colors = (KnownColor[])Enum.GetValues(typeof(KnownColor));
KnownColor randColor;
public Form1()
{
InitializeComponent();
textBox1.Text = initValue.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
bsOrigin = new BindingSource();
ds = GetData();
bsOrigin.DataSource = ds.Tables[0];
dataGridView1.DataSource = bsOrigin;
}
private DataSet GetData()
{
adapter = new SQLiteDataAdapter("SELECT * from Skill", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
adapter = new SQLiteDataAdapter("SELECT * from Skill", conn);
BindingSource bs = (BindingSource)dataGridView1.DataSource;
DataTable dt = bs.DataSource as DataTable;
adapter.Update(dt);
}
}
}
Is there anyone who can teach me?
I use this method:
public void saveData ()
{
SQLiteDataConnection con = new SQLiteDataConnection("yourconnectionstring");
SQLiteDataAdapter da= new SQLiteDataAdapter("Select statement", con);
SQLiteCommandBuiler com=new SQLiteCommandBuiler(da);
DataSet ds= new DataSet ();
DataTable dt = new DataTable ():
DataRow dr;
con.Open();
da.fill(ds, "tablename");
cn.Close();
dt=ds.Tables["tablename"];
dr=dt.NewRow();
dr["rowname"]=value;
//"same for the other rows"
dr.Rows.Add(dr);
da.Update(dt.Select(null, null, DataViewRowState.Added));
}
you should also check the file permission, I have seen issues with sqlite when they are access from certain folders, the desktop was one.
public partial class Form2 : Form
{
OleDbConnection conn1 = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dgovan\documents\visual studio 2015\Projects\LenovoWarranty2\LenovoWarranty2\LenovoWarranty.accdb");
DataTable dt = new DataTable();
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
fillData();
}
private void fillData()
{
//Init
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand cmd;
//statement
string sql = "SELECT * FROM Warranty";
cmd = new OleDbCommand(sql, conn1);
adapter.SelectCommand = cmd;
//fill
adapter.Fill(dt);
//bind
dataGridView1.DataSource = dt;
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
DataView dv = new DataView(dt);
dv.RowFilter = string.Format("SerialNumber LIKE '%{0}%'", txtSearch.Text);
dv.RowFilter = string.Format("CompanyName LIKE '%{0}%'", txtSearch.Text);
//BIND
dataGridView1.DataSource = dt;
}
}
Whenever I input anything into the txtSearch textbox - nothing happens? Can anyone spot the issue?
I am trying to allow the user to filter through SerialNumber + CompanyName fields from my database using the txtbox_textchanged event so whenever a character is inputed, the rows will filter.
Try
dataGridView1.DataSource = dv.ToTable();
Also I believe you are overriding the filter criteria instead of combining them. I think only the CompanyName filter will be used
I have a registeration form for new students and I need to have a datagridview with a textbox and a button above the datagridview to search students by studentID.I tried this code, but it didn`t work
private void txtsearch_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
DataTable dt = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter();
SDA.Fill(dt);
dataGridView1.DataSource = dt;
}
You are missing a couple things: The connection string to the database and a Select statement. You should do this code in a button click event rather than a click event on a textbox. Here is a better approach:
private void btnSearch_Click(object sender, EventArgs e)
{
var conn = new SqlConnection();
var dt = new DataTable();
var SDA = new SqlDataAdapter("Select * from students where
studentId = " + txtSearch.Text, "Your connection string here");
SDA.Fill(dt);
dataGridView1.DataSource = dt;
}
I have fetched some data from a SQL database into a datagridview, but after the user modified the data in the datagridview, how can I upload the data back?
And also, I found a code like this:
this.dataGridView1.Update();
What does this method Update? Here is the code where I bind the data to datagridview:
SqlDataReader read;
SqlCommand cmd;
DataTable dt = new DataTable();
cmd = new SqlCommand("Select * from Table", 204.192.49.3);
read = cmd.ExecuteReader();
dt.Load(read);
dataGridView1.DataSource = dt;
The easiest way is to create a temporary DataSet, fill it with data using a SqlDataAdapter and then passing it as dataGridView's DataSource. This code should do the trick:
DataSet temp = new DataSet();
SqlDataAdapter SDA = new SqlDataAdapter();
SqlCommand command = new SqlCommand();
SqlConnection connection = new SqlConnection();
string connstring = "YourConnectionString";
And then in the method you want to trigger the update do this:
`connection.Open();
command.CommandText = "SELECT * FROM Table" //Adjust the SQL query to your needs
command.Connection = connection;
SDA.SelectCommand = command;
SDA.Fill(temp);
dataGridView1.DataSource = temp.Tables[0].DefaultView;`
This should solve your problem.
To do this, you have fill your datagrid with Datasource property and SqlDataAdapter. You just have to update the adadpter object.
you can not update yr grid with this "this.dataGridView1.Update();" code
you must bind your grid view to db after update your data in db
private void dataGridVies1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(sda);
sda.Update(ds);
}
public partial class Form2 : Form
{
private void Form2_Load(object sender, EventArgs e)
{
con.ConnectionString = connectionstr;
sda = new SqlDataAdapter("select * from table_user", con);//define dataadapter
sda.Fill(ds );
dataGridView1.DataSource = ds.Tables[0];//bind the data,now the datagridview can show up the data
}
string connectionstr = "integrated security=SSPI;Initial Catalog=****;Data Source=localhost;";//init
SqlConnection con = new SqlConnection();//
SqlDataAdapter sda = new SqlDataAdapter();//
DataSet ds = new DataSet();
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//you can not update sda here
}
private void button1_Click(object sender, EventArgs e)
{
}
private void dataGridVies1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(sda);//auto generate the cmd of create, delete and update
sda.Update(ds);//flush to database
}
}