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:
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:
Click on the 2nd row Button:
Display message:
There are a total of four kinds of FlatStyles.
See the following all four kinds of FlatStyles:
-
Flat Style:
a. This kind of FlatStyle's image is displayed above the image.
b. dgvButton.FlatStyle= FlatStyle.Flat;
-
System:
a. dgvButton.FlatStyle= FlatStyle.System;
-
Popup:
a. dgvButton.FlatStyle= FlatStyle.Popup;
-
Standard:
a. dgvButton.FlatStyle= FlatStyle.Standard;
The whole code:
xusing 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.