how to add edit and delete buttons in datagridview in c#

Advertisement
In This Article I will Explain How to add Edit and Delete Button Links in DataGridView.

In My previous post ,DataGridview will looks like this.



Now  i want to add link buttons to datagridview by using  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 System.Data.SqlClient;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            displayDataGridView();

            DataGridViewLinkColumn Editlink = new DataGridViewLinkColumn();
            Editlink.UseColumnTextForLinkValue = true;
            Editlink.HeaderText = "Edit";
            Editlink.DataPropertyName = "lnkColumn";
            Editlink.LinkBehavior = LinkBehavior.SystemDefault;
            Editlink.Text = "Edit";
            dataGridView1.Columns.Add(Editlink);

            DataGridViewLinkColumn Deletelink = new DataGridViewLinkColumn();
            Deletelink.UseColumnTextForLinkValue = true;
            Deletelink.HeaderText = "delete";
            Deletelink.DataPropertyName = "lnkColumn";
            Deletelink.LinkBehavior = LinkBehavior.SystemDefault;
            Deletelink.Text = "Delete";
            dataGridView1.Columns.Add(Deletelink);

        }


        public void displayDataGridView()
        {


            SqlConnection con = new SqlConnection("Data Source = CHINNU;Initial Catalog = dotnetdb;Uid = sa;Password = password123;");
            {
                SqlCommand cmd;
                cmd = new SqlCommand("select * from Employee", con);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];

                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.AllowUserToAddRows = false;
                int i = 1;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells["SNO"].Value = i;
                    i++;
                }


            }

        }
    }
}

OutPut:
Advertisements
SHARE

Lakshmi Tulasi Jasti

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

16 comments:

  1. how to handle the edit button event

    ReplyDelete
    Replies
    1. yes how can we make that links work?

      Delete
  2. Replies
    1. just go to the datagridview column where you add this link and double click there after that the key press event is generated..................simple..........

      Delete
    2. Hoooow where is the datagridview column which one should I press

      Delete
  3. just go to the datagridview column where you add this link and double click there after that the key press event is generated..................simple..........

    ReplyDelete
  4. how to handle those two buttons

    ReplyDelete
  5. I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy. ucmini

    ReplyDelete
  6. Any sort of information that is incorporated into the spared shape can be revised and furthermore can be transported in and sent out in XML design from/to the DMS with any database. edit pdf

    ReplyDelete
  7. I would like to say that this blog really convinced me to do it! Thanks, very good post. 2020 rap songs

    ReplyDelete