connectseekers.com

Saturday, 27 September 2014

How to Add HyperLink and Retrieve Row Value on Link Click in DataGridView #, Windows application

9/27/2014 - By Pranav Singh 0

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.


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


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top