ARTICLE

DataGridViewLinkColumn & DataGridViewButtonColumn in C#

Posted by Ghanashyam Nayak Articles | Visual C# February 21, 2012
In this article you will learn how to add a DataGridViewLinkColumn & DataGridViewButtonColumn in a DataGridView at runtime & how to access the click event of the added columns.
Reader Level:
Download Files:
 

Code For Add LinkColumn in DataGridView at Runtime:

First of all I have added two columns named "User ID" & "Password".

myDataGridView.ColumnCount = 2;
myDataGridView.Columns[0].Name = "User ID";
myDataGridView.Columns[1].Name = "Password";

Then add rows with the data:

string[] row = new string[] { "abc""abc"};
myDataGridView.Rows.Add(row); row = new string[] { "pqr""pqr"};
myDataGridView.Rows.Add(row); row = new string[] { "ghanashyam""ghanashyam"};
myDataGridView.Rows.Add(row); row = new string[] { "jignesh""jignesh"};
myDataGridView.Rows.Add(row);

I have added a total of 4 rows with User Ids & Passwords.

Then take an object of the DataGridViewLinkColumn.

DataGridViewLinkColumn dgvLink = new DataGridViewLinkColumn();

Then set all needed properties.

dgvLink.UseColumnTextForLinkValue = true;
dgvLink.LinkBehavior = LinkBehavior.SystemDefault;
dgvLink.HeaderText = "Link Data";
dgvLink.Name = "SiteName";
dgvLink.LinkColor = Color.Blue;
dgvLink.TrackVisitedState = true;
dgvLink.Text = "http://www.c-sharpcorner.com";
dgvLink.UseColumnTextForLinkValue = true;

Finally add that column to the DataGridView:
 
myDataGridView.Columns.Add(dgvLink);

You can access that LinkColumn by clicking on it:
 
private void myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 2)
    {
       
//Write here your code...
    }
}

See the following image:

01.png

The whole code:

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

namespace DataGridView
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }        private void btn_Click(object sender, EventArgs e)
        {

            myDataGridView.Columns.Clear();

            myDataGridView.ColumnCount = 2;
            myDataGridView.Columns[0].Name = "User ID";
            myDataGridView.Columns[1].Name = "Password";

            string[] row = new string[] { "abc""abc"};
            myDataGridView.Rows.Add(row);
            row = new string[] { "pqr""pqr"};
            myDataGridView.Rows.Add(row);
            row = new string[] { "ghanashyam""ghanashyam"};
            myDataGridView.Rows.Add(row);
            row = new string[] { "jignesh""jignesh"};
            myDataGridView.Rows.Add(row);

            DataGridViewLinkColumn dgvLink = new DataGridViewLinkColumn();
            dgvLink.UseColumnTextForLinkValue = true;
            dgvLink.LinkBehavior = LinkBehavior.SystemDefault;
            dgvLink.HeaderText = "Link Data";
            dgvLink.Name = "SiteName";
            dgvLink.LinkColor = Color.Blue;
            dgvLink.TrackVisitedState = true;
            dgvLink.Text = "http://www.c-sharpcorner.com";
            dgvLink.UseColumnTextForLinkValue = true;

            myDataGridView.Columns.Add(dgvLink);
        }

        private void myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                //Write here your code...
            }
        }
    }
}

 
Code For Add ButtonColumn in DataGridView at Runtime:

As per the above code I have added the two columns named "User ID" & "Password" into the DataGridView & added rows.

Now create the object of the DataGridViewButtonCloumn:

DataGridViewButtonColumn dgvButton = new DataGridViewButtonColumn();

Then set all related properties of the button Column:
 
dgvButton.FlatStyle= FlatStyle.Flat;
dgvButton.HeaderText = "Button";
dgvButton.Name = "Button";
dgvButton.UseColumnTextForButtonValue = true;
dgvButton.Text = "New";

Finally add that ButtonColumn into the DataGridView.
 
myDataGridView.Columns.Add(dgvButton);

You can also access that button click event & write your own code:
 

private void myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

    if
 (e.ColumnIndex == 2)
    {
        //Write here your code...
        MessageBox
.Show("You Have Selected " + (e.RowIndex + 1).ToString() + " Row Button");
   }
}
 


See the following images:

  02.png
 

Click on the 2nd row Button:

  03.png
 

Display message:

04.png
 

There are a total of four kinds of FlatStyles.

See the following all four kinds of FlatStyles: 

  1. Flat Style:
     
    a.  This kind of FlatStyle's image is displayed above the image.
    b.  dgvButton.FlatStyle= FlatStyle.Flat;
     

  2. System:

    a.  dgvButton.FlatStyle= FlatStyle.System;
     
    06_Popup.png
     

  3. Popup:

    a.  dgvButton.FlatStyle= FlatStyle.Popup;
     
    07_Standard.png
     
     

  4. Standard:

    a.  dgvButton.FlatStyle= FlatStyle.Standard;

    08_System.png
      
     

 

The whole code:
 

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

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

        private void btn_Click(object sender, EventArgs e)
        {
            myDataGridView.Columns.Clear();

            myDataGridView.ColumnCount = 2;
            myDataGridView.Columns[0].Name = "User ID";
            myDataGridView.Columns[1].Name = "Password";

            string[] row = new string[] { "abc""abc"};
            myDataGridView.Rows.Add(row);
            row = new string[] { "pqr""pqr"};
            myDataGridView.Rows.Add(row);
            row = new string[] { "ghanashyam""ghanashyam"};
            myDataGridView.Rows.Add(row);
            row = new string[] { "jignesh""jignesh"};
            myDataGridView.Rows.Add(row);

            DataGridViewButtonColumn dgvButton = new DataGridViewButtonColumn();

            dgvButton.FlatStyle= FlatStyle.System;

            dgvButton.HeaderText = "Button";
            dgvButton.Name = "Button";
            dgvButton.UseColumnTextForButtonValue = true;
            dgvButton.Text = "New";

            myDataGridView.Columns.Add(dgvButton);
        }

        private void myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                //Write here your code...
                MessageBox.Show("You Have Selected " + (e.RowIndex + 1).ToString() + " Row Button");
            }
        }
    }
}
 


Hope this is clear to you.

Login to add your contents and source code to this article
post comment
     

You are welcome sara...

Posted by Ghanashyam Nayak Mar 11, 2012

Thank you divya...

Posted by Ghanashyam Nayak Mar 11, 2012

Thank you!!!

Posted by Sara Nguyen Mar 10, 2012

Thank you for this article ,Keep it up.

Posted by divya perumal Mar 10, 2012

Thank You So Much Daisy...

Posted by Ghanashyam Nayak Feb 22, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts