editing database (mysql) table with datagridview

K

kristian

Hi all

I have a simple form with not much more than a datagridview control,
and want to show and edit the contents of a table in my mysql table.

So far I have managed to set the db table as a datasource for the
datagridview, and display the contents.
Code bit:

public partial class FormEditDbTable : Form
{
OdbcDataAdapter da;
OdbcConnection odbcCon;
DataSet ds;
string table;

public FormEditDbTable(OdbcConnection inputOdbcCon, string
inputTable)
{
odbcCon = inputOdbcCon;
table = inputTable;

InitializeComponent();

if (odbcCon.State == ConnectionState.Closed)
{
odbcCon.Open();

string sqlString = "SELECT * FROM " + table;

OdbcCommand cm = new OdbcCommand(sqlString, odbcCon);
da = new OdbcDataAdapter(cm);


da.TableMappings.Add("Table", table);

ds = new DataSet(table);
da.Fill(ds);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds;
dataGridView1.DataMember = table;

odbcCon.Close();


}


}
}

This shows the datagridview with the variable number of columns in the
table given. I can of course then edit the cells in the datagridview,
but I have not been able to push the edited info back to the mysql
db.

Anyone have a hint for me?
--
Kristian Svartveit
(e-mail address removed)

PS: Would it be possible to give this form an SQL-statement instead of
a table, and would one be able to edit and save back to the DB then?
 
T

Tomas Vera

In addition to the Select command which you have defined, you also
need to define an Update command for the DataAdapter object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top