Winform Data grid view link button

QuestionsCategory: WinFormWinform Data grid view link button
XorientXorient asked 1 year ago

 

How to Add Link button in datagridview in c#.net?

1 Answers
Best Answer
Mahesh DeshmaneMahesh Deshmane answered 1 year ago

To create  Data grid view link button, we can use DataGridViewLinkColumn.  DataGridViewLinkColumn class is a specialized type of the DataGridViewColumn class used to logically host cells that respond to user clicks.

First lets create DataGridViewLinkColumn in Data grid view


DataGridViewLinkColumn link = new DataGridViewLinkColumn
{
   UseColumnTextForLinkValue = true,
   HeaderText = "User Profile",
   DataPropertyName = "lnkColumn",
   ActiveLinkColor = Color.White,
   LinkBehavior = LinkBehavior.SystemDefault,
   LinkColor = Color.Blue,
   Text = "View Profile",
   VisitedLinkColor = Color.YellowGreen
};

add column to data grid view

dataGridView.Columns.Add(link);

We can bind other collection upon it like we can set DataSource property to data grid view. here DataGridViewLinkColumn column index remains to ‘0’

dataGridView.DataSource = dataTable;

and then handle cell content click event in data grid view.

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

UI will look like below snapshot
Data Grid View Link Button

on click event we can then redirect user wherever we want typical examples are click other user form based on click or open browser with specified link etc..

Below is C# code snippet for the same.

Below code for demo purpose i have create temp Datatable which we can get it from any database directly & cell content click event assigned to grid click method.


using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms; 

namespace DatagridView{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeForm();
        }
        
        public void InitializeForm()
        {
            var dt = GetData();
            dataGridView.DataSource = dt;
            var link = new DataGridViewLinkColumn
            {
                UseColumnTextForLinkValue = true,
                HeaderText = "User Profile",
                DataPropertyName = "lnkColumn",
                ActiveLinkColor = Color.White,
                LinkBehavior = LinkBehavior.SystemDefault,
                LinkColor = Color.Blue,
                Text = "View Profile",
                VisitedLinkColor = Color.YellowGreen
            };
            
            dataGridView.Columns.Add(link);
        }
                    
        public DataTable GetData()
        {
            var ordersTable = new DataTable("Users");
            // Create OrderId column
            var dtColumn = new DataColumn
            {
                DataType = Type.GetType("System.Int32"),
                ColumnName = "UserId",
                Caption = "User ID",
            };
            ordersTable.Columns.Add(dtColumn);
            dtColumn = new DataColumn
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "Name",
                Caption = "Name"
            };
            ordersTable.Columns.Add(dtColumn);
            dtColumn = new DataColumn
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "Designation",
                Caption = "Designation"
            };
            ordersTable.Columns.Add(dtColumn);
            var dtRow = ordersTable.NewRow();
            dtRow["UserId"] = 1;
            dtRow["Name"] = "Mahesh";
            dtRow["Designation"] = "Engineer";
            ordersTable.Rows.Add(dtRow);
            dtRow = ordersTable.NewRow();
            dtRow["UserId"] = 2;
            dtRow["Name"] = "Shalini";
            dtRow["Designation"] = "DB Admin";
            ordersTable.Rows.Add(dtRow);
            dtRow = ordersTable.NewRow();
            dtRow["UserId"] = 3;
            dtRow["Name"] = "Kiran";
            dtRow["Designation"] = "Software Engineer";
            ordersTable.Rows.Add(dtRow);
            dtRow = ordersTable.NewRow();
            dtRow["UserId"] = 4;
            dtRow["Name"] = "Dipali";
            dtRow["Designation"] = "Tester";
            ordersTable.Rows.Add(dtRow);
            return ordersTable;
        }
                                    
        private void GridClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                MessageBox.Show($"User Clicked on row : {e.RowIndex}");
                //Either redirect to new form 
                //redirect to browser
                //redirect to report
                 
            }
        }
        
    }
} 

Here is more detail about Data Grid View Link Column