Introduction
This Code Show that How to create a BoundColumn, HyperLinkColumn and ButtonColumn for DataGrid in Asp.net at Runtime.
This Code also show that how to create a Master Detail with Two Grid controls
Steps:
1. Open A WebProject in VB.NET
2. In the WebForm Place Two Datagrid Controls
DataGrid1 -> Change the ID as -> gr (This is the Master Grid)
DataGrid2 -> Change the ID as -> Detail (This is The Detail Grid)
3. open the code window for to Write code
Note:
Database Information:
1.Ms Sql 2000
2.Database Name : Northwind
3. Tables used: Employees for both (MAster and Detail Grid)
Coding:
Declare the Following for DataBase Access
Dim Conn As New SqlClient.SqlConnection()
Dim Cmd As New SqlClient.SqlCommand()
Dim Adap As New SqlClient.SqlDataAdapter()
Dim Tab As New DataTable()
Declare tje Following Datagrid Columns
Dim Bound_Column As New BoundColumn()
Dim Hyper_Column As New HyperLinkColumn()
Dim Button_Column As New ButtonColumn()
Now we can Initilize the BoundColumn,HyperLinkColumn and ButtonColumn properties
before that The OnItemCommand Event Should Be Created and assigned to the method Disp() for the Datagrid1(gr datagrid) then only we will perporm Button command for Specific Column(Button Coulmn)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler gr.ItemCommand, AddressOf disp ' it will links the gr Grid OnItemCommand Event to Disp method that was created in below of this code
Initilize Datagrid gr
Set AutoGenerate Column to False , for Adding Manual Columns at Runtime
gr.AutoGenerateColumns = False
gr.BorderWidth = System.Web.UI.WebControls.Unit.Parse(2)
gr.BorderColor = Color.Black
Assign BoundColumn Properties
Bound_Column = New BoundColumn()
Bound_Column.DataField = "employeeid" ' Data Field Name(that is present in Query)
Bound_Column.DataFormatString = "" ' You May Specify the format of the Column
Bound_Column.HeaderText = "Employee ID"
Bound_Column.ItemStyle.BackColor = Color.White
Bound_Column.ItemStyle.ForeColor = Color.Blue
Bound_Column.ItemStyle.BorderColor = Color.Black
Bound_Column.ItemStyle.BorderWidth = System.Web.UI.WebControls.Unit.Parse(2)
Bound_Column.ItemStyle.Width = System.Web.UI.WebControls.Unit.Parse(100)
Bound_Column.HeaderStyle.ForeColor = Color.Green
Bound_Column.HeaderStyle.BackColor = Color.Blue
Bound_Column.HeaderStyle.BorderColor = Color.White
Bound_Column.HeaderStyle.BorderWidth = System.Web.UI.WebControls.Unit.Parse(2)
Bound_Column.ItemStyle.HorizontalAlign = HorizontalAlign.Center
Bound_Column.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
gr.Columns.Add(Bound_Column) ' Adding the Column to Datagrid
Assign HyperLink Column Properties
Hyper_Column = New HyperLinkColumn()
Hyper_Column.DataTextField = "lastname"
Hyper_Column.DataTextFormatString = ""
Hyper_Column.HeaderText = "Last Name"
Hyper_Column.ItemStyle.BackColor = Color.White
Hyper_Column.ItemStyle.ForeColor = Color.Green
Hyper_Column.ItemStyle.BorderColor = Color.Black
Hyper_Column.ItemStyle.BorderWidth = System.Web.UI.WebControls.Unit.Parse(2)
Hyper_Column.ItemStyle.Width = System.Web.UI.WebControls.Unit.Parse(100)
Hyper_Column.HeaderStyle.ForeColor = Color.Blue
Hyper_Column.HeaderStyle.BackColor = Color.Yellow
Hyper_Column.HeaderStyle.BorderColor = Color.White
Hyper_Column.HeaderStyle.BorderWidth = System.Web.UI.WebControls.Unit.Parse(2)
Hyper_Column.DataNavigateUrlField = "employeeid"
Hyper_Column.DataNavigateUrlFormatString = "details.aspx?ID={0}"
Hyper_Column.NavigateUrl = "details.aspx?ID={0}"
Hyper_Column.Target = "_old"
Hyper_Column.ItemStyle.HorizontalAlign = HorizontalAlign.Center
Hyper_Column.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
gr.Columns.Add(Hyper_Column)
Assign ButtonColumn Properties
Button_Column = New ButtonColumn()
Button_Column.DataTextField = "firstname"
Button_Column.DataTextFormatString = ""
Button_Column.HeaderText = "Firstname"
Button_Column.ItemStyle.BackColor = Color.Blue
Button_Column.ItemStyle.ForeColor = Color.Green
Button_Column.ItemStyle.BorderColor = Color.Black
Button_Column.ItemStyle.BorderWidth = System.Web.UI.WebControls.Unit.Parse(2)
Button_Column.ItemStyle.Width = System.Web.UI.WebControls.Unit.Parse(2)
Button_Column.HeaderStyle.ForeColor = Color.Green
Button_Column.HeaderStyle.BackColor = Color.Red
Button_Column.HeaderStyle.BorderColor = Color.Black
Button_Column.HeaderStyle.BorderWidth = System.Web.UI.WebControls.Unit.Parse(2)
Button_Column.ButtonType = ButtonColumnType.LinkButton
Button_Column.CommandName = "disp"
Button_Column.Text = ""
Button_Column.ItemStyle.HorizontalAlign = HorizontalAlign.Center
Hyper_Column.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
gr.Columns.Add(Button_Column)
Call binddata for to Bind the Datasource
BindData()
End Sub ' page load
Create Sub pro Disp() for the OnItemCommand Event (Datagrid gr)
the Disp sub is assigned for OnItemCommand of gr(datagrid1) in eariler code.
inthis code , the the user clicked rows has been recived with EventArg e
by uising that we can make the relationship with master and detail grid.
Public Sub disp(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
Dim sql As String
Tab.Rows.Clear()
sql = "Select * from employees where employeeid=" & e.Item.Cells(0).Text
Conn = New SqlClient.SqlConnection("User ID=sa;Initial Catalog=Northwind;Data Source=NET7")
Cmd = New SqlCommand(sql, Conn)
Adap = New SqlDataAdapter(Cmd)
Adap.Fill(Tab)
Detail.DataSource = Tab
Detail.DataBind()
End Sub
BindData () for Binding the result to the Datagrid(gr)
Public Sub BindData()
Dim sql As String
sql = "Select employeeid,lastname,firstname from employees"
Conn = New SqlClient.SqlConnection("User ID=sa;Initial Catalog=Northwind;Data Source=NET7")
Cmd = New SqlCommand(sql, Conn)
Adap = New SqlDataAdapter(Cmd)
Adap.Fill(Tab)
gr.DataSource = Tab
gr.DataBind()
End Sub
Note:
In the BindData() , just change your Database Server, UsrId, Database ....
Here, i used NorthWind Database and Employees Table,
Output:
The output will be as
Datagrid1 - gr -> it will displays employeeid, firstname, lastname
Firstname as ButtonColumn
Lastname as HyperlinkColumn
if you click on the HyperLink Column it will calls the Detail.aspx?id= <youremployeeid>
if you Click on the ButtonColumn(Fristname) it will displays the all columns in the Datagrid2 with the CorresPonding Employee you had selected.
I hope it will help you for datagrid column operations,
Thank you,
Yours,
Kannan.R