473,383 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

editing database (mysql) table with datagridview

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
fi*******@lastname.net

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?

Feb 26 '07 #1
1 8279
In addition to the Select command which you have defined, you also
need to define an Update command for the DataAdapter object.

On 26 Feb 2007 04:17:56 -0800, kr******@bommelibom.com wrote:
>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?
Feb 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bob Kaku | last post by:
I'm trying to create a text editing and updating capability to help someone who wants to maintain content on a web page without having to know any HTML or use a web authoring tool and FTP'ng the...
0
by: Allan Allan Ebdrup | last post by:
Hi I'm looking for the best webinterface for editing database data, for example publishing a table with foreign keys shown as a dropdown. I don't care what database the interface is for (MSSQL,...
1
by: artteam | last post by:
Hi frnds, I am working on a project in which one of the form has a datagridview, I was successful in retrieving the table from the database.Now that I want to edit the records on this...
2
by: Steve Richter | last post by:
what is the best way to use DataGridView to view data from large database tables? Where the sql select statement might return millions of rows? string connectionString =...
3
by: Finn Stampe Mikkelsen | last post by:
Hi I have defined a table in my database, with 2 date-fields. I have set a default value to DBNull. I have integrated a nullable datetimepicker control to my project and set the apropriate...
1
srj115
by: srj115 | last post by:
This is just something that had me tearing out my nose hairs in frustration... hypothetically. I must have spent 7 or 8 hours trying to get this to work before I smelled the coffee (so to speak). ...
4
by: thelner | last post by:
Im trying to bind a table in mysql containing a mysql date field in a datagridview in vb.net 05. When i only select fields containing non-dates it works, but once i add a date field it generates this...
5
by: Keith Rebello | last post by:
I use VB.Net in VS2005 and rarely use databases. I have a database to which I open a connection in my application (using ADO). Once I have the connection, I loop through each table in the...
2
by: crownrajesh | last post by:
First of all thanks for all who reply this Question.i have a datagridview with first column as combobox and rest as textbox column. i would like to fill the datagridview manually using c# code for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.