Wednesday 11 November 2015

Insert update delete in DataGridView in C#

Insert update delete in DataGridView in 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;

namespace AccountOpenning
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DataTable dt = new DataTable();
        int i, j;
        int rowcount, sum;

        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("S.No", typeof(int));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Card No", typeof(int));
            dt.Columns.Add("Client Name", typeof(string));
            dt.Columns.Add("Product Name", typeof(string));
            dt.Columns.Add("Empty Bottle", typeof(int));
            dt.Columns.Add("Balance Account", typeof(int));
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            dt.Rows.Add(txtSno.Text, dateTimePicker1.Value, txtCno.Text, txtCname.Text, txtPname.Text, txtEbottle.Text, txtBaccount.Text);
            dataGridView1.DataSource = dt;

            txtSno.Text = null;
            txtCno.Text = null;
            txtCname.Text = null;
            txtEbottle.Text = null;
            txtPname.Text = null;
            txtBaccount.Text = null;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                dt.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
            DataGridViewRow row = dataGridView1.Rows[i];
            txtSno.Text = row.Cells[0].Value.ToString();
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            txtCno.Text = row.Cells[2].Value.ToString();
            txtCname.Text = row.Cells[3].Value.ToString();
            txtPname.Text = row.Cells[4].Value.ToString();
            txtEbottle.Text = row.Cells[5].Value.ToString();
            txtBaccount.Text = row.Cells[6].Value.ToString();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dataGridView1.Rows[i];
            row.Cells[0].Value = txtSno.Text;
            row.Cells[1].Value = dateTimePicker1.Text;
            row.Cells[2].Value = txtCno.Text;
            row.Cells[3].Value = txtCname.Text;
            row.Cells[4].Value = txtPname.Text;
            row.Cells[5].Value = txtEbottle.Text;
            row.Cells[6].Value = txtBaccount.Text;
        }
    }
}

No comments:

Post a Comment