Set Content Of Grid Cell As Hyperlink In Datagridview Of Winform

Please refer to:

Step 1: Add DataGridView in Winform.

DataGridView

Step 2: Create data table in the code and bind the data table to DataGridView datasource in the form load event.

Create data table in code
  1. /// <summary>  
  2.        /// Get datatable of description.  
  3.        /// </summary>  
  4.        /// <returns></returns>  
  5.        private DataTable GetDescriptionTable()  
  6.        {  
  7.            DataTable l_dtDescription = new DataTable();  
  8.            l_dtDescription.Columns.Add("Description"typeof(string));  
  9.            l_dtDescription.Columns.Add("Type"typeof(string));  
  10.            l_dtDescription.Columns.Add("Email"typeof(string));  
  11.            l_dtDescription.Columns.Add("WWW"typeof(string));  
  12.   
  13.            l_dtDescription.Rows.Add("Lunch""Expense""[email protected]""http://www.sagarratna.in/");  
  14.            l_dtDescription.Rows.Add("Dinner""Expense""[email protected]""http://www.mayfairhotels.com/");  
  15.            l_dtDescription.Rows.Add("Breakfast""Expense""[email protected]""http://www.haldirams.com/");  
  16.            l_dtDescription.Rows.Add("Designing""Service""[email protected]""http://www.adjaye.com/");  
  17.            l_dtDescription.Rows.Add("Drawing""Service""[email protected]""http://www.painterartist.com/fr");  
  18.            l_dtDescription.Rows.Add("Paper""Material""[email protected]""https://riflepaperco.com/");  
  19.            l_dtDescription.Rows.Add("DrawingBoard""Material""[email protected]""http://www.drawingboard.com/");  
  20.   
  21.            return l_dtDescription;  
  22.        }  
Bind the data table with the data source of datagridview.
  1. private void Form3_Load(object sender, EventArgs e)  
  2.         {  
  3.             dgv.DataSource = GetDescriptionTable();  
  4.         }  
Step 3: Run the Application, so output will come as shown in the image, given below. Now, the task is to display Email and WWW as a hyperlink.

application

Step 4: For setting an Email and WWW columns as hyperlink, we need to set the style of a grid column.

The function to get the style for hyperlink is given below:
  1. /// <summary>  
  2.         /// This function is use to get hyperlink style .  
  3.         /// </summary>  
  4.         /// <returns></returns>  
  5.         private DataGridViewCellStyle GetHyperLinkStyleForGridCell()  
  6.         {  
  7.             // Set the Font and Uderline into the Content of the grid cell .  
  8.             {  
  9.                 DataGridViewCellStyle l_objDGVCS = new DataGridViewCellStyle();  
  10.                 System.Drawing.Font l_objFont = new System.Drawing.Font(FontFamily.GenericSansSerif, 8, FontStyle.Underline);  
  11.                 l_objDGVCS.Font = l_objFont;  
  12.                 l_objDGVCS.ForeColor = Color.Blue;                
  13.                 return l_objDGVCS;  
  14.             }  
  15.         }  
Function to set hyperlink style into the grid columns of “Email” and “WWW” is shown below:
  1. private void SetHyperLinkOnGrid()  
  2. {  
  3.     if (dgv.Columns.Contains("Email"))  
  4.     {  
  5.         dgv.Columns["Email"].DefaultCellStyle = GetHyperLinkStyleForGridCell();  
  6.     }  
  7.   
  8.     if (dgv.Columns.Contains("WWW"))  
  9.     {  
  10.         dgv.Columns["WWW"].DefaultCellStyle = GetHyperLinkStyleForGridCell();  
  11.     }  
  12. }  
Step 5: Call SetHyperLinkOnGrid() to set hyperlink over “Email” and “WWW” columns.
  1. private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)  
  2.         {  
  3.            SetHyperLinkOnGrid();  
  4.         }    
Step 6: Run the Application. Now, in the screenshot, given below, we can see Email and WWW column data is in hyperlink.

application

Step 7: Enable the hyperlink, which means when the user clicks over “WWW” content, open the link of the Web Browser and when the user clicks over “Email” content, open link in an Email.
  1. private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)  
  2.         {  
  3.   
  4.             if (dgv.Columns[dgv.CurrentCell.ColumnIndex].HeaderText.Contains("WWW"))  
  5.             {  
  6.                 if ( ! String.IsNullOrWhiteSpace (dgv.CurrentCell.EditedFormattedValue.ToString ()))  
  7.                 {                     
  8.                     System.Diagnostics.Process.Start("http://" + dgv.CurrentCell.EditedFormattedValue);  
  9.                 }                 
  10.             }  
  11.   
  12.             if (dgv.Columns[dgv.CurrentCell.ColumnIndex].HeaderText.Contains("Email"))  
  13.             {  
  14.                 if (!String.IsNullOrWhiteSpace(dgv.CurrentCell.EditedFormattedValue.ToString()))  
  15.                 {  
  16.                     System.Diagnostics.Process.Start("mailto://" + dgv.CurrentCell.EditedFormattedValue);  
  17.                 }  
  18.             }  
  19.         }  
Ebook Download
View all
Learn
View all