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;
}
Related
I do not get any error messages, but the Datagrid won't show any Data from my Database.
var con = new MySqlConnection("server= localhost; database=fußballmanager; Uid=root;Pwd=;");
private void Form4_Load(object sender, EventArgs e)
{
MySqlDataAdapter da = new MySqlDataAdapter("Select * from tbl_anmeldedaten", con);
da.SelectCommand.CommandType = CommandType.Text;
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
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 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.
When I select any value from the DropDownList, the first one gets selected.
The code's purpose is whenever I choose a value from the DropDownList the corresponding value from the database should be displayed in the TextBox.
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("connection string");
con.Open();
DataTable Seminars = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT SeminarName, ID FROM SeminarData", con);
adapter.Fill(Seminars);
DropDownList1.DataSource = Seminars;
DropDownList1.DataTextField = "SeminarName";
DropDownList1.DataValueField = "SeminarName";
DropDownList1.DataBind();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("connection string");
con.Open();
DataTable dt = new DataTable();
SqlCommand sqlCmd = new SqlCommand("SELECT SeminarNameE,TrainerName FROM SeminarData WHERE SeminarName='" + DropDownList1.SelectedItem.Value +"'", con);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
TextBox1.Text = dt.Rows[0]["SeminarNameE"].ToString();
TextBox2.Text = dt.Rows[0]["TrainerName"].ToString();
}
}
Replace your page load with this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
SqlConnection con = new SqlConnection("connection string");
con.Open();
DataTable Seminars = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT SeminarName, ID FROM SeminarData", con);
adapter.Fill(Seminars);
DropDownList1.DataSource = Seminars;
DropDownList1.DataTextField = "SeminarName";
DropDownList1.DataValueField = "SeminarName";
DropDownList1.DataBind();
con.Close();
}
}
It needs !Page.IsPostBack, because everytime you bind, you clear any selections. In this code, it binds on every page load. Adding !Page.IsPostback will ensure it binds only the first load.
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
}
}