How to edit datagridview using SqlCeDataAdapter? - c#

I've a frmEdit with datagridview that's bouned to this:
string sqlqry1 = "select p_Name from Items where p_Id=" + p_Id;
using (SqlCeDataAdapter a = new SqlCeDataAdapter(sqlqry1, conn))
{
DataTable dt1 = new DataTable();
a.Fill(dt1);
dataGridView1.DataSource = dt1;
}
How to edit cells and save them back to the db?, tried using this:
using (SqlCeDataAdapter a = new SqlCeDataAdapter(sqlqry1, conn))
{
DataTable dt1 = new DataTable();
a.Fill(dt1);
dataGridView1.DataSource = dt1;
a.Update(dt1);
}
Nothing.
Is there anyway?.

Found solution:
SqlCeDataAdapter da;
SqlCeCommandBuilder cmdBuilder;
da = new SqlCeDataAdapter("select * from Items", conn);
cmdBuilder = new SqlCeCommandBuilder(da);
da.Fill(myDatabaseDataSet, "Items");
myDatabaseDataSet.Tables["Items"].Rows[0]["p_Name"] = dataGridView1.Rows[0].Cells["p_Name"].Value.ToString();
da.Update(myDatabaseDataSet, "Items");

DataTable dT;
BindingSource bS;
using (SqlCeConnection yourConnection = new SqlCeConnection("Data Source=|DataDirectory|\\YourDatabase.sdf"))
{
dT = new DataTable();
bS = new BindingSource();
string query = "SELECT * FROM table01";
SqlCeDataAdapter dA = new SqlCeDataAdapter(query, yourConnection);
SqlCeCommandBuilder cBuilder = new SqlCeCommandBuilder(dA);
dA.Fill(dT);
bS.DataSource = dT;
dgv01.DataSource = bS;
}
Resurrecting this with a working example because of this pages rank on google.

Related

Save edits from DataGridView to Datatable

Hi i want to save edits in DataGriView to datatable , i tried that code but an error shows 'System.ArgumentOutOfRangeException: 'The index was out of range. It must not be negative and must be smaller than the size of the collection.
Parameter name: index '' help
private void button11_Click(object sender, EventArgs e)
{
con.Open();
String query = "SELECT * FROM Taom";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
DataTable dt1 = new DataTable();
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
dataGridView1.DataSource = dt1;
SDA.Fill(dt);
dataGridView1.Rows[2].Cells[2].Value = Math.Atan((Convert.ToDouble(dataGridView1.Rows[5].Cells[2].Value)) / (Convert.ToDouble(dataGridView1.Rows[6].Cells[2].Value)));
dataGridView1.Rows[3].Cells[2].Value = Math.Atan((Convert.ToDouble(dataGridView1.Rows[7].Cells[2].Value)) / (Convert.ToDouble(dataGridView1.Rows[8].Cells[2].Value)));
ds1.Tables.Add(dt);
con.Close();}
I changed my code to that code no errors showed after i run values on datagridview change but no changes in datatable !!!
string query = "SELECT * FROM [dbo].[Taom]";
SqlConnection conn = new SqlConnection(#"Data Source=STE-P0024818PW;Initial Catalog=test;Integrated Security=True");
conn.Open();
SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SDA.Fill(dt);
dt.Rows[0]["Contents"] = "98"; //Before it was 10
dt.Rows[1]["Contents"] = "99"; //Before it was 11
ds1.Tables.Add(dt);
conn.Close();
Fill the right value inside your table and after pass the table rightly filled in your datasource, don't change this directly in the gridview like:
I try myself and it works:
private void Run()
{
string query = "SELECT * FROM dbo.[Anrufthema]";
SqlConnection conn = new SqlConnection("MyConnectionString");
conn.Open();
SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SDA.Fill(dt);
dt.Rows[0]["Anrufthema"] = "98"; //Before it was 10
dt.Rows[1]["Anrufthema"] = "99"; //Before it was 11
ds1.Tables.Add(dt);
conn.Close();
}
My Result, it works !

Error displaying data from DB to predefined columns of datagrid View on button click event

I am using the following code:
string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
connection.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter(getinputs, connection);
MySqlCommandBuilder cmdbuilder = new MySqlCommandBuilder(adapter);
DataTable dt = new DataTable();
adapter.Fill(dt);
BindingSource bindsrc = new BindingSource();
DataGridView datagridv = new DataGridView();
bindsrc.DataSource = dt;
datagridv.DataSource = bindsrc;
connection.Close();
I have three predefined columns in datagridview1 say 'PLU_Code', 'Barcode' and 'Product_Name'.
I want to display the result (more than one row) of the select query in the datagridview. But I am not getting any.
How do I achieve this?
Try this code:
string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
connection.Open();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(getinputs, connection);
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
dataAdapter.Fill(table);
datagridview1.DataSource = table;
connection.Close();
I got the result. I achieved two empty rows on using #user4340666 code. when i scrolled the grid i found the set of data's been added as new columns leaving predefined column cells empty. so i just cleared the columns.
dataGridView1.Columns.Clear();
string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
connection.Open();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(getinputs, connection);
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
dataAdapter.Fill(table);
datagridview1.DataSource = table;
connection.Close();
Try this code:
string getinputs = "Select Query";
connection.Open();
MySqlCommandBuilder cmdbuilder = new MySqlCommandBuilder(getinputs,connection);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmdbuilder);
DataTable dt = new DataTable();
adapter.Fill(dt);
DataGridView datagridv = new DataGridView();
datagridv.DataSource = dt;
datagridv.DataBind();
connection.Close();

command builder update doesn't work

Code seems to be fine but when i click it, nothing happens.
THanks!
private void button2_Click(object sender, EventArgs e)
{
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = new MySqlCommand("select * from poitems", coninsert);
MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "poitems");
da.Update(ds, "poitems");
//DataTable dt1 = new DataTable();
//da.Fill(dt1);
//da.Update(dt1);
//dtgPo.DataSource = dt1;
}
you are not updating anything in your dataset, check below sample update code
MySqlConnection conn = new MySqlConnection(connectionString);
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Test", conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(da, true);
DataTable dt = new DataTable();
da.Fill(dt);
//update datatable
dt.Rows[0][0] = "my changed value";
DataTable changes = dt.GetChanges();
//call update
da.Update(changes);
dt.AcceptChanges();

Search data in two datagrids at same time

I have this two datagrids where JMBGvlasnika is in both of them same as BrojSasije.
For search I use this code:
DataView DV = new DataView(dbdt);
DV.RowFilter = String.Format("JMBGvlasnika LIKE '%{0}%'", textBox1.Text);
korisniciDataGridView.DataSource = DV;
Loading tables code:
private void svikorisnici_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand(" SELECT * FROM Korisnici", konekcija);
SqlCommand cmd1 = new SqlCommand(" SELECT * FROM vozilo", konekcija);
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
dbdt = new DataTable();
sda.Fill(dbdt);
BindingSource kbs = new BindingSource();
kbs.DataSource = dbdt;
korisniciDataGridView.DataSource = kbs;
sda.Update(dbdt);
SqlDataAdapter sda1 = new SqlDataAdapter();
sda1.SelectCommand = cmd1;
DataTable dbdt1 = new DataTable();
sda1.Fill(dbdt1);
BindingSource kbs1 = new BindingSource();
kbs1.DataSource = dbdt1;
voziloDataGridView.DataSource = kbs1;
sda1.Update(dbdt1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Problem is when I search by JMBGvlasnika I get search results only for 1st table
image link http://ge.tt/999Igxm/v/0?c
If I try to do this:
DataView DV1 = new DataView(dbd1t);
DV1.RowFilter = String.Format("JMBGvlasnika LIKE '%{0}%'", textBox1.Text);
voziloDataGridView.DataSource = DV1;
And start searching I get empty 2nd table, and if I delete what I wrote 2nd table is not filtering.
Can someone help me to make search in both datagrids in same time ?

how to store multiple DataTables into single DataSet in c#?

I have the code below which has multiple DataTables with results and I need to pass a single DataSet with all DataTable values.
MySqlCommand cmd = new MySqlCommand(getLikeInfo, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
MySqlCommand cmd1 = new MySqlCommand(getKnowInfo, con);
MySqlDataAdapter da1 = new MySqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
MySqlCommand cmd2 = new MySqlCommand(getHotelInfo, con);
MySqlDataAdapter da2 = new MySqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
MySqlCommand cmd3 = new MySqlCommand(getRoomInfo, con);
MySqlDataAdapter da3 = new MySqlDataAdapter(cmd3);
DataTable dt3 = new DataTable();
da3.Fill(dt3);
MySqlCommand cmd4 = new MySqlCommand(getFoodInfo, con);
MySqlDataAdapter da4 = new MySqlDataAdapter(cmd4);
DataTable dt4 = new DataTable();
da4.Fill(dt4);
How can I do that?
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dt1);
...
If you want your tables named in the DataSet you can create your tables from the DataSet
DataSet ds = new DataSet();
DataTable t1 = ds.Tables.Add("t1"); // Create
DataTable t1Again = ds.Tables["t1"]; // fetch by name
You can also set the TableName property of the tables if you use the first approach.

Categories