This article will show you how to add hyperlink and retrieve row value on click in dataGridView
c#, Windows application. In this we will show row detail on click of link in dataGridView.
Some of my previous articles are as follows: How
to Add Button Control and Retrieve Row Value on Button Click in DataGridView
Using C#.Net, Windows Application, Splash
Screen With Please Wait OR Loading Message in C#.net in Windows Application,
Paging
in DataGridview Using C#.Net In Windows Application, Windows
Application - Excel Sheet Name in C#.Net, How
To Make a Single Row of DataGridview Bold Using C#.Net in Windows Application.
So for this article fist we will create a new windows application and add a dataGridView control on form.
So for this article fist we will create a new windows application and add a dataGridView control on form.
After adding all the columns your grid will look as shown below.
Now on form load of the application add the below code.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
public
DataTable GetTable()
{
// Here
we create a DataTable with four columns.
DataTable
dt = new DataTable();
dt.Columns.Add("Id",
typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Location", typeof(string));
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Link", typeof(string));
// Here
we add five DataRows.
dt.Rows.Add(1, "Vinay", "India",
DateTime.Now, "http://www.yahoo.com");
dt.Rows.Add(2, "Imran", "Pakistan",
DateTime.Now, "http://www.google.com");
dt.Rows.Add(3, "Pawan", "U.S.",
DateTime.Now, "http://www.ask.com");
dt.Rows.Add(4, "Manash", "Nepal",
DateTime.Now, "http://www.about.com");
dt.Rows.Add(5, "Ranu", "Sri
Lanka", DateTime.Now, "http://www.aol.com");
return
dt;
}
private
void Form1_Load(object
sender, EventArgs e)
{
DataTable
dt = new DataTable();
dt = GetTable();
if
(dt.Rows.Count > 0)
{
dataGridView1.DataSource =
dt;
}
}
private
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs
e)
{
if
(dataGridView1.Columns[e.ColumnIndex].Name == "Hyperlink")
{
string
id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
string
name = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
string
location = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
string
date = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
string
hyperlink = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
string
message = "Id : " + id + "\nName : " + name + "\nLocation : " + location + "\nDate : " + date + "\nHyperLink : " + hyperlink;
MessageBox.Show(message);
}
}
}
}
|
In above code I have added a I have prepared a data table
with value and bind the data table to the dataGridView.
So we have done run the application and check the output.
Now click on hyper link
0 comments:
Please let me know your view