Update DataGridView to DataSet - c#

I'm displaying information from a dataset in a datagridview. How do I update the dataset with the data in the datagridview after it's modified? (automaticly or through button) Using winforms,in visual studio 2010, c#

The first, you should create a new datatable to contains data from Datagridview
After that, in the buttun are:(i'm connecting with database is accsess(sql is the same))
DataTable dt = new DataTable();
dt = (DataTable)dataGridView1.DataSource;
OleDbDataAdapter adp = new OleDbDataAdapter("Your query", con);
OleDbCommandBuilder cmdb = new OleDbCommandBuilder(adp);
adp.Update(dt);
// con: the variable name to connecting with database

Related

Main dataset is getting modified when local dataset is modified

Below is my code.
DataSet ds = new DataSet();
ds = dsComplaints;
DataTable dt = new DataTable();
dt = ds.Tables[0];
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] {"--Select Complaint--" };
dt.Rows.InsertAt(dr, 0);
dsComplaints is globally declared which has the main data. I am copying it to a local DataSet ds. Then I assign the table from ds to a datatable. I am adding a row(select complaint) in data table. But when the insertion happens, my main dataset i.e. dsComplaints is also added with a row. How can i avoid that. I don't know why this is happening.
You can use the Copy method to create a new copy of same DataSet instead of using the reference only of the original one to avoid this :
DataSet ds = dsComplaints.Copy();
From MSDN DOCS this is what Copy does:
Copies both the structure and data for this DataSet.
Now you can do whatever is needed with the copy and it would not effect the original version of DataSet.

inserting DataTable into psql

if I use:
NpgsqlCommand cmd = new NpgsqlCommand("select * from table", conn)
DataTable dt = new DataTable();
using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
{
da.Fill(dt);
}
To get data from the server to a DataTable,
Is there a way to similarly put it back after I'm done with the data?
Like emptying the table and inserting the whole DataTable into it.
You could execute a command to drop the table (or just empty all rows if the table structure won't be modified) and iterate through the rows in your DataTable to insert the modified rows from the DataTable into the new table in your Database.

Binding Datagridview to dataset

I have a problem binding datagridview to dataset, when I debug code I can see that dataset is filled ok with data from database, but it wont show on datagridview...
Here is my code:
MySqlConnection conn = new MySqlConnection(connectionString);
string sql = #"select artikli.idArtikla, artikli.NazivArtikla
from artikli";
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Artikli");
// Bind the data table to the data grid
dataGridView1.DataSource = ds;
EDIT: Thx for answers, now when I can see my data in grid, what is the easiest way to allow inserting, deleting and editing in my grid and forwarding those cnages to my database table?
DataGridView doesn't know how to bind DataSet, you must bind a DataTable.
dataGridView1.DataSource = ds.Tables["Artikli"];
or
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Artikli";
You need to provide the which DataTable you want to bind to DataGridView.
Try:
dataGridView1.DataSource = ds.Tables[0];
You forgot to bind the dataset to gridview.
At the end of your code add the following line.. after datasource.
dataGridView1.DataBind();

Selecting specified columns form DataTable in C#

I have a DataTable object which is read from Excel speradsheet. Since the spread sheet has blank columns. I want to get rid of the blank columns in my DataTable retaining only the columns which have header data. Any better approaches rather than reading though each column?
I use C# 3.5.
You can better use "OleDbConnection" rather than looping through each columns to fetch data.
It will avoid the blank columns
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
OleDbDataAdapter da = new OleDbDataAdapter("select * from YourTable", con);
DataTable dt = new DataTable();
da.Fill(dt);

Binding a DataGridView to a DataSet directly fails

A DataGridView bound by this code does not display information as expected:
dataGridView1.DataSource = ds;
here's the code for ds:
public DataSet ConnectandReadList()
{
DataSet ds = new DataSet();
string connection_string="Data Source=hermes;database=qcvalues; Integrated Security=SSPI;";
using (var myConnection = new SqlConnection(connection_string))
{
myConnection.Open();
var command = new SqlCommand(InitializeQuery(), myConnection);
var adapter = new SqlDataAdapter(command);
adapter.Fill(ds);
}
return ds;
}
Try binding to the table inside the dataset: dataGridView1.DataSource = ds.Tables[0];
From the documentation for the DataGridView.DataSource property, you can also bind to a DataSet and use the DataMember property:
When binding to a data source that contains multiple lists or tables, you must set the DataMember property to a string that specifies the list or table to bind to.
I think in this case the table name would be "Table" since you are not naming it explicitly.
Is ds a DataSet?
If so, try setting your DGV's DataMember to a DataTable within the DataSet or specifying your DataSet's DataTable for the DataSource.

Categories