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.
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:
- Here Fetch Data button will display the data values from the SQL and displays it by clicking it.
- Go to tool box and click on the DataGridview option the form will be open.
C# Sample Code - C# Examples:
- 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.
- 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.
- 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).
- 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.
- Here dataGridView1.DataSource = ds.Tables; 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.
- 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 :
- Here in this output the Fetch Data button will display the data values from the SQL and displays it by clicking it.
- Here in this output table displays the Id "1,2,3" and name "venkat, jln, arun" and Link Data www.wikitechy.com.