ARTICLE

How to cretae Button Column in VB.NET

Posted by Munesh Sharma Articles | ADO.NET in VB.NET May 10, 2011
In this article, we will learn abut the data column and create the button column in VB.NET.
Download Files:
 
Reader Level:

How to define the data columns:

We known that a data grid control displays one column for each column in the data source. if if that's not what you want. you can change that are column that are displayed using the column page of the properties dialog box. You can also use this dialog box to change the text that's displayed in the header for a column or specify the  formatting for a column.

When you first display the column page of this dialog box, the create column  automatically at run time options is selected. if you want manually then add a column to add the data grid. you simply select it form the list of the column in the data field group and click on the add button as you select there column.

How to create  button column:

There are various step to create the button column, that are:

  • Step first go to visual studio >>new >> Project >> Asp project.

  • Step second drag and drop  data grid in web page.

  • Step third select the properties:

    select-button.gif

    button-Text.gif

Coding for defining column in ASP. NET:

%@ Page Language=VB Debug=true %>
<%
@ Import Namespace="System.Data" %>
<
script runat=server>
Sub
Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        Dim MyDT As New DataTable
        Dim MyRow As DataRow
        MyDT.Columns.Add(New DataColumn("EmployeeID", _            GetType(Int32)))
        MyDT.Columns.Add(New DataColumn("EmployeeFirstName", _            GetType(String)))
        MyDT.Columns.Add(New DataColumn("EmployeeLastName", _            GetType(String)))
        MyDT.Columns.Add(New DataColumn("BirthDate", _            GetType(Date)))
        MyDT.Columns.Add(New DataColumn("Salary", _            GetType(Single))) 
        MyRow = MyDT.NewRow()
        MyRow(0) = 1
        MyRow(1) = "Bob"
        MyRow(2) = "Miller"
        MyRow(3) = "5/15/65"
        MyRow(4) = "40000"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 2
        MyRow(1) = "Jenny"
        MyRow(2) = "Fry"
        MyRow(3) = "7/22/75"
        MyRow(4) = "73050"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 3
        MyRow(1) = "Lisa"
        MyRow(2) = "Smith"
        MyRow(3) = "12/8/71"
        MyRow(4) = "62500"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 4       
        MyRow(1) = "Stephanie"
        MyRow(2) = "Myer"
        MyRow(3) = "3/15/54"
        MyRow(4) = "43222"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 5
        MyRow(1) = "Jimmy"
        MyRow(2) = "Parker"
        MyRow(3) = "3/12/47"
        MyRow(4) = "52825"
        MyDT.Rows.Add(MyRow) 
        dg1.DataSource = MyDT
        dg1.DataBind()
    End If
    If Len(Request.QueryString("EmployeeID")) > 0 Then
        lblMessage.Text = "You selected employee " _
            & Request.QueryString("EmployeeID") & ".<BR>"
    End If

End
Sub
Sub
Click_Grid(ByVal Sender as Object, ByVal E as DataGridCommandEventArgs)
    lblMessage.Text = "You selected " _
        & E.Item.Cells(2).Text & " " _
        & E.Item.Cells(1).Text & "<BR>"

End
Sub
</
SCRIPT>
<
HTML>
<
HEAD>
<
TITLE>DataGrid 3 Control Sample Page</TITLE>
</
HEAD>
<
BODY >
<
form id="Form1" runat="server">
<
Font Face="Tahoma">
<
asp:Label
    id="lblMessage"
    runat="server"
/>
<
BR>
<
ASP:DataGrid
    id="dg1"
  runat="server"
    Width="90%"
    BorderColor="black"
    CellPadding=3
    CellSpacing="0"
    Font-Name="Trebuchet MS"
    Font-Size="10pt"
    ForeColor="Black"
    BackColor="Beige"
    AlternatingItemStyle-ForeColor="Cornsilk"
    AlternatingItemStyle-BackColor="DarkBlue"
    AlternatingItemStyle-Font-Name="Arial"
    AlternatingItemStyle-Font-Italic="True"
    HeaderStyle-BackColor="Burlywood"
    HeaderStyle-Font-Bold="True"
    AutoGenerateColumns="false"   
    OnItemCommand
="Click_Grid"

    <Columns>
        <asp:HyperLinkColumn
            HeaderText="Employee ID"
            DataNavigateUrlField="EmployeeID"           
            DataNavigateUrlFormatString="./thispage.aspx?EmployeeID={0}"
            DataTextField="EmployeeID"
            Target="_self"
        />
        <asp:BoundColumn            
HeaderText
="Last Name"
            DataField="EmployeeLastName"
        />
        <asp:BoundColumn
            HeaderText="First Name"
            DataField="EmployeeFirstName"
        />
        <asp:BoundColumn
            HeaderText="Salary"
            DataField="Salary"
            DataFormatString="{0:C}"        />
        <asp:BoundColumn
            HeaderText="Birth Date"
            DataField="BirthDate"
            DataFormatString="{0:d}"
        />
        <asp:ButtonColumn
            HeaderText="Click for Fun"
            ButtonType="PushButton"
            Text="Click Me"
        />
    </Columns> 

</
asp:DataGrid>
</
Font>
</
Form>
</
BODY>
</
HTML>

Output:

            output-column.gif 

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor