Thursday, May 12, 2016

How to create table, Update and Delete rows in Data Grid view

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 WindowsFormsApplication19
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();
        private void button1_Click(object sender, EventArgs e)
        {
            DataColumn dc1 = new DataColumn("Id", typeof(int));
            DataColumn dc2 = new DataColumn("Name", typeof(string));
            DataColumn dc3 = new DataColumn("Salary", typeof(int));
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            DataRow dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = "Sachin";
            dr[2] = 2000;
            dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            dt.Rows[0].Delete();
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            dt.Rows[0][0] = 2;
        }
    }
}

Output : 

click on show button: 
 click on update button :
click on delete button :

1 comment:

  1. please sachin describe fully, you cannot specify the update button update only first row and first column.. rest of article is good

    ReplyDelete