Click here to Skip to main content
15,893,904 members

edit gridview data and save in Mysql database,Winforms

Member 10263519 asked:

Open original thread
Hi,

In button click i populated gridview (no primary key for columns in database), using dataAdapter,dataset.

when i select a cell , that record should come in textboxes for editing data, after editing that should be saved in database.

edit button should be in gridview cell at the end of columns of database how to do.....

here is my code:
C#
<pre lang="c#">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace Billling_Machine
{
    public partial class EditTransaction : Form
    {

        String ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        DataSet ds = new DataSet();
        MySqlDataAdapter da;
        MySqlCommandBuilder commandbuilder;
        String query;

        public EditTransaction()
        {
            InitializeComponent();
        }

        private void btnedittxok_Click(object sender, EventArgs e)
        {
            String date1 = dateTimePicker1.Text;
            String[] date = Regex.Split(date1, "[/: ]");
            StringBuilder sb = new StringBuilder();
            sb.Append(date[2]);
            sb.Append("-");
            sb.Append(date[0]);
            sb.Append("-");
            sb.Append(date[1]);
            String fromdate = sb.ToString();


            String date2 = dateTimePicker2.Text;
            String[] date3 = Regex.Split(date2, "[/: ]");
            StringBuilder sb1 = new StringBuilder();
            sb1.Append(date3[2]);
            sb1.Append("-");
            sb1.Append(date3[0]);
            sb1.Append("-");
            sb1.Append(date3[1]);
            String todate = sb1.ToString();
            
            String customer = txtedittxcustomer.Text;
            String card = txtedittxcard.Text;


            using (MySqlConnection con = new MySqlConnection(ConnectionString))
            {
                query = "select * from transaction where DATE(paid_date)BETWEEN '" + fromdate + "'AND'" + todate + "' AND customer_id='" + customer + "' ";
                con.Open();
               da = new MySqlDataAdapter(query, con);
                         
             da.Fill(ds);
             dataGridView1.DataSource = ds;
             dataGridView1.DataMember = ds.Tables[0].ToString();
             DataGridViewButtonColumn ButtonColumn = new
DataGridViewButtonColumn();
             ButtonColumn.Text = "Edit";
             ButtonColumn.Name = "Edit";
             ButtonColumn.DataPropertyName = "del";
             dataGridView1.Columns.Add(ButtonColumn);

            
            }
            groupBox2.Visible = true;

        }

        private void EditTransaction_Load(object sender, EventArgs e)
        {
            groupBox2.Visible = false;
        }
Tags: C# (C# 4.0), MySQL

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900