[英]Delete selected row from datagrid view and update mysql database in C#
m developing application for my office and in my application there is a datagrid view that linked to a mysql database. 在为我的办公室开发应用程序时,在我的应用程序中有一个链接到mysql数据库的datagrid视图。 local users can update the database using datagridview but they cant delete any recodes.
本地用户可以使用datagridview更新数据库,但不能删除任何重新编码。 i want to implement a method that user's are select exact raw in the datagridview and delete it as well as delete the database record soon.
我想实现一种方法,使用户可以在datagridview中选择确切的原始数据并将其删除以及尽快删除数据库记录。 i managed to make select and delete datagridview row using below code but it not update the database
我设法使用下面的代码进行选择和删除datagridview行,但它没有更新数据库
private void button60_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
{
if (oneCell.Selected)
dataGridView1.Rows.RemoveAt(oneCell.RowIndex);
}
}
and this is my connection string that i normally use to view the database data in datagrid view. 这是我通常用于在datagrid视图中查看数据库数据的连接字符串。 i don't have good knowledge to combined these two.can someone please show me how to do that
我没有足够的知识来将两者结合起来。有人可以告诉我如何做到这一点吗?
my connection string 我的连接字符串
private void showdatagrid() { 私人无效showdatagrid(){
string constring = string.Format("datasource='{0}';username=******;port=3306;password=***********;Connect Timeout=20000;Command Timeout=28800", dbserverip.Text);
MySqlConnection conwaqDatabase = new MySqlConnection(constring);
MySqlCommand cmdwaqDatabase = new MySqlCommand(" select * from warit.loans ; ", conwaqDatabase);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdwaqDatabase;
dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
dataGridView1.DataSource = bsource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try this code 试试这个代码
private void button60_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
{
if (oneCell.Selected)
{
dataGridView1.Rows.RemoveAt(oneCell.RowIndex);
int loannumber = dataGridView1.Rows[oneCell.RowIndex].Cells['index of loannumber column in datagridview'].Value; // assuming loannmber is integer
string username = dataGridView1.Rows[oneCell.RowIndex].Cells['index of username column in datagridview'].Value; // assuming username is string
/* Now create an object of MySqlConnection and MySqlCommand
* and the execute following query
*/
string query = string.Format("DELETE FROM table_name WHERE loannumber = {0} AND username = '{1}'", loannumber, username);
}
}
}
string connection = "server = URL; uid = user; pwd = password; database = database;";
using (MySqlConnection conn = new MySqlConnection(connection)) {
conn.Open();
string idLocRemv = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string removeVolCred = "DELETE FROM TableName WHERE ID = " + idLocRemv;
using (MySqlCommand command = new MySqlCommand(removeVolCred, fbcConn)) {
command.ExecuteNonQuery();
}
conn.Close();
}
Apply necessary exception handlers. 应用必要的异常处理程序。 (try catch finally)
(最后尝试抓住)
Additionally, you will need to re-load DataGridView afterwards. 此外,此后您将需要重新加载DataGridView。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.