How to show link button datagridview windows application ?


In this article I have explained about how to create link button in data grid view windows application. In ASP.NET we use link button to show data of grid view similar like this in windows application datagridview have one special class datagridview column. Find How to show link button datagridview windows application?

Learn how to show link button datagridview windows application?


Description:
For example I have employee data in the data grid view control if I click the view details link button then particular employee all details show in the small pop up screen.

How to add datagridview Link column?
Use below code to add link button in your data grid view

//Create instance for datagrid view link column class
DataGridViewLinkColumn link = new DataGridViewLinkColumn();
link.UseColumnTextForLinkValue = true;
//Enter your link button column header text
link.HeaderText = "Click to View Details";
//set the property name for link column
link.DataPropertyName = "lnkColumn";
//set default active color for link
link.ActiveLinkColor = Color.White;
link.LinkBehavior = LinkBehavior.SystemDefault;
//set default link color
link.LinkColor = Color.Blue;
//set default link text
link.Text = "View Details";

//set visited color means if user click that link what color would be change
link.VisitedLinkColor = Color.YellowGreen;
//Add this link column to datagrid view
dataGridView1.Columns.Add(link);

How to open popup if user click link button?

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Check whether user click on the first column
if (e.ColumnIndex == 0)
{
int row;
//Get the row index
row = e.RowIndex;
//Create instance of the form2 class
Form2 obj = new Form2();
//Bind the datagridview values in the second popup form
obj.Controls["TextBox1"].Text = dataGridView1.Rows[row].Cells[1].Value.ToString();
obj.Controls["TextBox2"].Text = dataGridView1.Rows[row].Cells[2].Value.ToString();
obj.Controls["TextBox3"].Text = dataGridView1.Rows[row].Cells[3].Value.ToString();
obj.ShowDialog();
}
}

Full source code:
Table structure

create table emp(eno int,empname varchar(50),sal bigint)
insert into emp values(101,'ravi','25000')
insert into emp values(102,'john','18000')
insert into emp values(103,'joseph','10000')

Server side:

using System.Data;

namespace DGV_lnkButton
{
public partial class Form1 : Form
{
SqlConnection sqlcon = new SqlConnection(@"server=RAVI-PC\SQLEXPRESS;database=test1;uid=xxx;pwd=yyy;");
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();

public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Create instance for datagrid view link column class
DataGridViewLinkColumn link = new DataGridViewLinkColumn();
link.UseColumnTextForLinkValue = true;
//Enter your link button column header text
link.HeaderText = "Click to View Details";
//set the property name for link column
link.DataPropertyName = "lnkColumn";
//set default active color for link
link.ActiveLinkColor = Color.White;
link.LinkBehavior = LinkBehavior.SystemDefault;
//set default link color
link.LinkColor = Color.Blue;
//set default link text
link.Text = "View Details";

//set visited color means if user click that link what color would be change
link.VisitedLinkColor = Color.YellowGreen;
//Add this link column to datagrid view
dataGridView1.Columns.Add(link);

//Open connection
sqlcon.Open();
//pass query
sqlcmd = new SqlCommand("select * from emp",sqlcon);
da = new SqlDataAdapter(sqlcmd);
//Fill the data in the SQL Data Adapter
da.Fill(dt);
//Bind it in the DataGrid View
dataGridView1.DataSource = dt;
sqlcon.Close();

//Uncomment below line when you want display link column at last
//dataGridView1.Columns[0].DisplayIndex = 3;
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Check whether user click on the first column
if (e.ColumnIndex == 0)
{
int row;
//Get the row index
row = e.RowIndex;
//Create instance of the form2 class
Form2 obj = new Form2();
//Bind the datagridview values in the second popup form
obj.Controls["TextBox1"].Text = dataGridView1.Rows[row].Cells[1].Value.ToString();
obj.Controls["TextBox2"].Text = dataGridView1.Rows[row].Cells[2].Value.ToString();
obj.Controls["TextBox3"].Text = dataGridView1.Rows[row].Cells[3].Value.ToString();
obj.ShowDialog();
}
}
}
}

Source Code :
Here I attached source code download it and try to show pop up window using link button.

Front End : Form Design
Code Behind : C#.NET

Output:
Lnk_Output

Conclusion:
I hope this article help to know about datagridview link column in C#.net.


Attachments

  • DGV_LinkButton (43503-221815-DGV_lnkButton.rar)
  • Comments

    Guest Author: Lakshmanan15 Jun 2012

    Really gr8.. superb..
    Really helpfull...
    Its really working.. Hats of to Ravindran...
    Good job..
    1000's of thanks..

    Author: VibhorBa23 Jan 2015 Member Level: Bronze   Points : 0

    Thanks..it is working..

    Author: hothorasman24 Jan 2015 Member Level: Bronze   Points : 0

    what if it is in the asp.net



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: