I've a datagridview and two buttons which get data from sql server DB. Whenever i click on btn1 it fetches the datagridview with the record AND if i then click btn2 it combines the btn2 records with the btn1 records.
How can i clear the datagridview so that btn2 will show me it records and not combinig the records?
clear:
DataSet.Clear();
http://msdn.microsoft.com/en-us/library/system.data.dataset.clear.aspx
Add below is a example of Populating a datagridview with sql query results.
string select = "SELECT * FROM EXAMPLETABLE";
Connection c = new Connection();
SqlDataAdapter dataAdapter = new SqlDataAdapter(select, c.con); //c.con is the connection string
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.ReadOnly = true;
dataGridView.DataSource = ds.tables[0];
Related
Greetings everyone i want to click an image in my datagridview then it will display it in another form in full size.
the problem is the image it self is populated from mysql table
and i have read other questions that carry the same meaning but all of them were data that came from local table and not mysql table.
here is my dataset
Con.Open();
string Myquery = "select * from SampleTable";
MySqlDataAdapter da = new MySqlDataAdapter(Myquery, Con);
MySqlCommandBuilder builder = new MySqlCommandBuilder(da);
var ds = new DataSet();
da.Fill(ds);
ProductsGv.DataSource = ds.Tables[0];
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn = (DataGridViewImageColumn)ProductsGv.Columns[1];
Thank you.
note: there is a con close at the end of the code i just didnt post it here because there are too many lines where i manipulated the columns size width and name
Not sure if this question has been asked before, so here goes..
I have a front end app coded in C# windows forms. On a second form i have two datagridviews that gets populated from two different sql server pre-defined views
I need to refresh both datagrids at the same time with a single button click
My button click even looks like this..
private void RefreshBTN_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("removed for illustration only");
string query = "select * from daily_orders order by orderno DESC";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
How i understand this is, C# opens new connection, queries the DB and returns by filling datagridview1 with the required data. I would like the same click event to request data from another sql view and populate another datagridview at the same time. Visually both grids are aligned vertically on the same form, one on to of the other.
Many thanks in advance
Move the code for refreshing Grid1 into a separate function. Then copy paste and duplicate that function for Grid2. Change the SQL for Grid2 as well as the Grid2 name. Rename the copied function with 2. Then add a call to both functions so your button click will refresh both grids.
private void RefreshBTN_Click(object sender, EventArgs e)
{
//call both functions to refresh both on button click
RefreshGrid1();
RefreshGrid2();
}
private void RefreshGrid1()
{
SqlConnection myConnection = new SqlConnection("removed for illustration only");
string query = "select * from daily_orders order by orderno DESC";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
//give this function a unique name to represent the second grid refresh
private void RefreshGrid2()
{
SqlConnection myConnection = new SqlConnection("removed for illustration only");
string query = "select * from daily_orders order by orderno DESC";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
//rename this to your second grid name
dataGridView2.DataSource = dt;
}
In my database I have 4 column. I fetch this database value in a datagridview. But I want to add two columns in datagridview. So, I want to make a datagridview with 6 columns. Within this 6 columns 4 columns will filled by database value. How can I do this?
OleDbConnection con = new OleDbConnection("CONNECTION STRING");
con.Open();
DataTable dtusers = new DataTable();
OleDbCommand cmd = new OleDbCommand("Select Code,Description,Qnty,Rate from PurchaseTable'", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dtusers);
dataGridView1.DataSource = dtusers;
dataGridView1.Columns[0].Name = "Code ";
dataGridView1.Columns[1].Name = "Description";
dataGridView1.Columns[2].Name = "Qnty";
dataGridView1.Columns[3].Name = "Rate";
con.Close();
Here are 4 columns. Code,Description,Qnty,Rate. I want to add two more columns in this datagridview. Amount and Narration. But Amount and Narration columns are not present in PurchaseTable.How can I do this?
If you want blank columns then create them and insert. If the data is in the database then add a join to your query to retrieve the data.
Adding blank columns
DataGridView dgv = new DataGridView();
dgv.DataSource = dtusers;
DataGridViewColumn amount = new DataGridViewColumn();
amount.HeaderText = "Amount";
amount.Name = "Amount";
dgv.Columns.Insert(0, amount);
DataGridViewColumn narration = new DataGridViewColumn();
narration.HeaderText = "Narration";
narration.Name = "Narration";
dgv.Columns.Insert(0, narration);
Assumed that you already have dtusers ..
Do Column adding to dtuser .. http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx
Do looping for all rows in your table to fill your new column
Assign the table as dgv datasource .. dataGridView1.DataSource = dtusers;
Because your column names are hardcoded, I think you can create a predefined columns in your datagridview through designer(Click datagridview -> choose Edit Columns).
Create all 6 columns you want. On the first four set DataPropertyName to name of the column from your query. Last two leave empty.
And remember set datagridview.AutoGeneratedColumns = false; before you binding data to datagridview...(somewhere in form_Load handler)
After this your code will be:
using (OleDbConnection con = new OleDbConnection("CONNECTION STRING"))
{
con.Open();
DataTable dtusers = new DataTable();
using(OleDbCommand cmd = new OleDbCommand("Select Code,Description,Qnty,Rate from PurchaseTable'", con))
{
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dtusers);
dataGridView1.DataSource = dtusers;
}
con.Close();
}
I've a database in MS Access.
And i'm placing the data in the database to datagridview on form load.
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + Application.StartupPath + "/Db.mdb";
OleDbConnection conObj = new OleDbConnection(connectionString);
conObj.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("SELECT * FROM StopMaster", conObj);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
conObj.Close();
on execution, it'll show datagridview with the items in the database,
along with an empty row where we can add new data
and if we click on the existing row's items with data we can update it and even remove it
how to code for that update delete and add and reflect to the database
is there any event or what
also how can i make data grid view columns not resizable.
also say if i have two columns "name" and "pin code" in the datagridview when i click on column header say for now "pin code"it must sort the datagridview rows in order
is there any event and how to for that too
The first thing to mention, is that you will need to Bind your data table to the Datagridview. This is so that any changes you make whilst editing the DataGridView control reflect directly through to the underlying DataTable.
I'll assume that you have already populated the DataTable object.
You start by creating BindingSource object :-
BindingSource bs = new BindingSource();
bs.Datasource = dt;
Now we need to make this Binding Object the datasource for your DataGridView Control :-
dataGridView1.DataSource = bs;
In a method, you can use the following code to commit changes :-
using (OleDbConnection con = new OleDbConnection(connectionString))
{
var adaptor = new OleDbDataAdapter();
adaptor.SelectCommand = new OleDbCommand(""SELECT * FROM [StopMaster]", con);
var cbr = new OleDbCommandBuilder(adaptor);
cbr.GetDeleteCommand();
cbr.GetInsertCommand();
cbr.GetUpdateCommand();
try
{
con.Open();
adaptor.Update(dt);
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message, "OledbException Error");
}
catch (Exception x)
{
MessageBox.Show(x.Message, "Exception Error");
}
}
I am doing some c# and mysql and I was successful at getting mysql data into a grid view for the first time! Now, my main question is, how do I manage the grid view style with this? For example, say I have already created columns and such, how do I put the mysql data into a specific column in the grid view?
Below is the code that is actually loading the data into the grid view.
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
conn.Open();
// - DEBUG
// MessageBox.Show("Connection successful!");
MySqlDataAdapter MyDA = new MySqlDataAdapter();
MyDA.SelectCommand = new MySqlCommand("SELECT * FROM `swipes`", conn);
DataTable table = new DataTable();
MyDA.Fill(table);
BindingSource bSource = new BindingSource();
bSource.DataSource = table;
dataGridView1.DataSource = bSource;
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
Close();
}
Also, this creates columns based on the mysql data, how do I modify the width of these columns and such, or like stated above, use my own custom columns for my data? I've never done any mysql work in any UI, so I'm open to suggestions and tutorials as well. Thanks in advance!
If you really want to do this (as someone has already stated you should look at other options) you can create the columns in the designer and set the DataGridViewColumn.DataPropertyName on each column to the columns returned by the autogenerated dataset. Remember to turn of autogeneration of columns (AutoGenerateColumns) on the grid. This way you have full control of the column styles.
try this
string connection = "server=localhost;database=adil;user=root;password=";
MySqlConnection con = new MySqlConnection(connection);
con.Open();
MySqlCommand command = new MySqlCommand();
command.Connection = con;
MySqlDataAdapter MyDA = new MySqlDataAdapter();
string sqlSelectAll = "SELECT * from studentrec";
MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, con);
DataTable table = new DataTable();
MyDA.Fill(table);
BindingSource bSource = new BindingSource();
bSource.DataSource = table;
dataGridView1.DataSource = bSource;