Click here to Skip to main content
15,891,204 members
Articles / Web Development / ASP.NET
Article

Creating DataGrid BoundColumn ButtonColumn HyperLinkColumn at Runtime in Asp.net

Rate me:
Please Sign up or sign in to vote.
1.41/5 (10 votes)
14 Dec 20052 min read 78.1K   640   24  
This Article Show How to create a Creating DataGrid BoundColumn ButtonColumn HyperLinkColumn at Runtime in Asp.net

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

<P>Dim Conn As New SqlClient.SqlConnection()</P><P>Dim Cmd As New SqlClient.SqlCommand()</P><P>Dim Adap As New SqlClient.SqlDataAdapter()</P><P>Dim Tab As New DataTable()</P><P> </P>

Declare tje Following Datagrid Columns

<P>Dim Bound_Column As New BoundColumn()</P><P>Dim Hyper_Column As New HyperLinkColumn()</P><P>Dim Button_Column As New ButtonColumn()</P>

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)

<P>Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load</P><P> </P><P>AddHandler gr.ItemCommand, AddressOf disp       ' it will links the gr Grid OnItemCommand Event to Disp method that was created in below of this code</P><P> </P>
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
<PRE> 
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
<P>Button_Column = New ButtonColumn()</P><P>Button_Column.DataTextField = "firstname"</P><P>Button_Column.DataTextFormatString = ""</P><P>Button_Column.HeaderText = "Firstname"</P><P>Button_Column.ItemStyle.BackColor = Color.Blue</P><P>Button_Column.ItemStyle.ForeColor = Color.Green</P><P>Button_Column.ItemStyle.BorderColor = Color.Black</P><P>Button_Column.ItemStyle.BorderWidth = System.Web.UI.WebControls.Unit.Parse(2)</P><P>Button_Column.ItemStyle.Width = System.Web.UI.WebControls.Unit.Parse(2)</P><P>Button_Column.HeaderStyle.ForeColor = Color.Green</P><P>Button_Column.HeaderStyle.BackColor = Color.Red</P><P>Button_Column.HeaderStyle.BorderColor = Color.Black</P><P>Button_Column.HeaderStyle.BorderWidth = System.Web.UI.WebControls.Unit.Parse(2)</P><P>Button_Column.ButtonType = ButtonColumnType.LinkButton</P><P>Button_Column.CommandName = "disp"</P><P>Button_Column.Text = ""</P><P>Button_Column.ItemStyle.HorizontalAlign = HorizontalAlign.Center</P><P>Hyper_Column.HeaderStyle.HorizontalAlign = HorizontalAlign.Center</P><P>gr.Columns.Add(Button_Column)</P>
Call binddata for to Bind the Datasource
<PRE>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.

 

<PRE>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)
<PRE>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

 

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Iam a Vb.Net, ASP.Net, SQL Server Professional working in INTEK SYSTEMS - COIMBATORE





Comments and Discussions

 
-- There are no messages in this forum --