Please refer to:
Step 1: Add DataGridView in Winform.
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
-
-
-
-
- private DataTable GetDescriptionTable()
- {
- DataTable l_dtDescription = new DataTable();
- l_dtDescription.Columns.Add("Description", typeof(string));
- l_dtDescription.Columns.Add("Type", typeof(string));
- l_dtDescription.Columns.Add("Email", typeof(string));
- l_dtDescription.Columns.Add("WWW", typeof(string));
-
- l_dtDescription.Rows.Add("Lunch", "Expense", "[email protected]", "http://www.sagarratna.in/");
- l_dtDescription.Rows.Add("Dinner", "Expense", "[email protected]", "http://www.mayfairhotels.com/");
- l_dtDescription.Rows.Add("Breakfast", "Expense", "[email protected]", "http://www.haldirams.com/");
- l_dtDescription.Rows.Add("Designing", "Service", "[email protected]", "http://www.adjaye.com/");
- l_dtDescription.Rows.Add("Drawing", "Service", "[email protected]", "http://www.painterartist.com/fr");
- l_dtDescription.Rows.Add("Paper", "Material", "[email protected]", "https://riflepaperco.com/");
- l_dtDescription.Rows.Add("DrawingBoard", "Material", "[email protected]", "http://www.drawingboard.com/");
-
- return l_dtDescription;
- }
Bind the data table with the data source of datagridview.
- private void Form3_Load(object sender, EventArgs e)
- {
- dgv.DataSource = GetDescriptionTable();
- }
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.
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:
-
-
-
-
- private DataGridViewCellStyle GetHyperLinkStyleForGridCell()
- {
-
- {
- DataGridViewCellStyle l_objDGVCS = new DataGridViewCellStyle();
- System.Drawing.Font l_objFont = new System.Drawing.Font(FontFamily.GenericSansSerif, 8, FontStyle.Underline);
- l_objDGVCS.Font = l_objFont;
- l_objDGVCS.ForeColor = Color.Blue;
- return l_objDGVCS;
- }
- }
Function to set hyperlink style into the grid columns of “Email” and “WWW” is shown below:
- private void SetHyperLinkOnGrid()
- {
- if (dgv.Columns.Contains("Email"))
- {
- dgv.Columns["Email"].DefaultCellStyle = GetHyperLinkStyleForGridCell();
- }
-
- if (dgv.Columns.Contains("WWW"))
- {
- dgv.Columns["WWW"].DefaultCellStyle = GetHyperLinkStyleForGridCell();
- }
- }
Step 5: Call SetHyperLinkOnGrid() to set hyperlink over “Email” and “WWW” columns.
- private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
- {
- SetHyperLinkOnGrid();
- }
Step 6: Run the Application. Now, in the screenshot, given below, we can see Email and WWW column data is in hyperlink.
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.
- private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
-
- if (dgv.Columns[dgv.CurrentCell.ColumnIndex].HeaderText.Contains("WWW"))
- {
- if ( ! String.IsNullOrWhiteSpace (dgv.CurrentCell.EditedFormattedValue.ToString ()))
- {
- System.Diagnostics.Process.Start("http://" + dgv.CurrentCell.EditedFormattedValue);
- }
- }
-
- if (dgv.Columns[dgv.CurrentCell.ColumnIndex].HeaderText.Contains("Email"))
- {
- if (!String.IsNullOrWhiteSpace(dgv.CurrentCell.EditedFormattedValue.ToString()))
- {
- System.Diagnostics.Process.Start("mailto://" + dgv.CurrentCell.EditedFormattedValue);
- }
- }
- }