C# Controls Datagridview Add Hyperlink Column | Hyperlink C# - c# - c# tutorial - c# net



What is Datagridview?

  • The DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms.
  • The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior.
  • The DataGridView control provides TextBox, CheckBox, Image, Button, ComboBox and Link columns with the corresponding cell types.
 DataGridview Hyperlink

DataGridview Hyperlink

Add Hyperlink Column:

  • AddHyperlink represents a column of cells that contain links in a DataGridView control
  • In C# Controls, the DataGridViewLinkColumn class is a specialized type of the DataGridViewColumn class is used to logically host cells that respond to user clicks.
  • The DataGridViewLinkColumn class is similar to the DataGridViewButtonColumn class but provides a different user experience that can be more appropriate in certain situations, such as displaying a URL stored in a database table.
  • Here in the below table is representing to Windows Forms:
 csharp add hyperlink column
  1. Here Fetch Data button will display the data values from the SQL and displays it by clicking it.
  2. Go to tool box and click on the DataGridview option the form will be open.

C# Sample Code - C# Examples:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace WikiTechy_Csharp_Windows_Application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataSet ds = new DataSet();
        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString =
                @"Data Source=VENKAT\KAASHIV;Initial Catalog=wikitechy;
                        user id = venkat;password=venkat";

            string sql = "SELECT * FROM login_table";
            SqlConnection connection = new SqlConnection(connectionString);

            SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
          
            connection.Open();
                        dataadapter.Fill(ds);
            connection.Close();
            dataGridView1.DataSource = ds.Tables[0];
            DataGridViewLinkColumn lnk = new DataGridViewLinkColumn();
            dataGridView1.Columns.Add(lnk);
            lnk.HeaderText = "Link Data";
            lnk.Name = "http://www.wikitechy.com/";
            lnk.Text = "http://www.wikitechy.com/";
            lnk.UseColumnTextForLinkValue = true;
            }
        }
    }
click below button to copy the code. By - c# tutorial - team

Code Explanation:

 c-sharp hyperlink code
  1. Here DataSet ds = new DataSet(); specifies to Create a new instance of the DataSet class. The DataSet Object represents a complete set of data, including related tables, constraints, and relationships among the tables.
  2. Here string connectionString = @"Data Source=VENKAT\KAASHIV;Initial Catalog=wikitechy; user id = venkat;password=venkat"; connection string specifies that includes the source database name here the name is VENKAT\KAASHIV, and other parameters needed to establish the initial catalog connection is wikitechy. The default value is user id and password is venkat. Here in this example string sql = "SELECT * FROM login_table"; specifies to select the login_table in SQL server.
  3. Here SqlConnection connection = new SqlConnection (connectionString); this statement is used to acquire the SqlConnection as a resource. Here in this example SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection); specifies to initializes a new instance of the SqlDataAdapter class with a SelectCommand (sql) and a SqlConnection object (connection).
  4. Here in this example, connection.Open(); specifies to Open the SqlConnection. Here dataadapter.Fill(ds); specifies to fetches the data from User and fills in the DataSet ds. Here in this example connection.Close(); it is used to close the Database Connection.
  5. Here dataGridView1.DataSource = ds.Tables[0]; is used to specify the data source of datagridview and the table which will be bind to. When we run the code a window will show with a datagridview that containing all the returning rows.
  6. Here DataGridViewLinkColumn lnk = new DataGridViewLink Column(); specifies to initializes a new instance of the DataGridViewLinkColumn class. Here dataGridView1.Columns.Add(lnk); specifies to add hyperlink in the column of a DataGridView , the column type contains cells of type DataGridViewLinkCell and renders the text in the cell to look like a hyperlink. lnk.HeaderText = "Link Data";b> used to store the URL in DataTable. Here lnk.Name = www.wikitechy.com; specifies to Link name of the URL. Here lnk.Text = www.wikitechy.com; specifies to Link text of the URL.Here lnk.UseColumnTextForLinkValue = true; specifies if the column Text property value is true it displays the link text.

Sample C# examples - Output :

 csharp-add-hyperlink-column
  1. Here in this output the Fetch Data button will display the data values from the SQL and displays it by clicking it.
  2. Here in this output table displays the Id "1,2,3" and name "venkat, jln, arun" and Link Data www.wikitechy.com.


Related Searches to C# Controls Datagridview Add Hyperlink Column | Hyperlink C#