ASP.Net/Asp Control/DataGrid

Материал из .Net Framework эксперт
Версия от 11:53, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add button to asp:DataGrid (VB.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 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>



Add data to datagrid (VB.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) = "A"
        MyRow(2) = "M"
        MyRow(3) = "5/15/65"
        MyRow(4) = "40000"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 2
        MyRow(1) = "J"
        MyRow(2) = "F"
        MyRow(3) = "7/22/75"
        MyRow(4) = "73050"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 3
        MyRow(1) = "L"
        MyRow(2) = "S"
        MyRow(3) = "12/8/71"
        MyRow(4) = "62500"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 4
        MyRow(1) = "S"
        MyRow(2) = "M"
        MyRow(3) = "3/15/54"
        MyRow(4) = "43222"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 5
        MyRow(1) = "J"
        MyRow(2) = "P"
        MyRow(3) = "3/12/47"
        MyRow(4) = "52825"
        MyDT.Rows.Add(MyRow)
        dg1.DataSource = MyDT
        dg1.DataBind()
    End If
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>DataGrid 1 Control Sample Page</TITLE>
</HEAD>
<BODY >
<form runat="server">
<Font Face="Tahoma">
<ASP:DataGrid 
    id="dg1" 
    runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>



Add hyperlink a asp:datagrid (VB.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 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>



Add hyperlink to asp:datagrid table column (VB.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 Sort_Grid(ByVal Sender as Object, _
    ByVal E as DataGridSortCommandEventArgs)
    lblMessage.Text = "You want to sort by the " _
        & E.SortExpression.ToString() & "<BR>"
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>DataGrid 4 Control Sample Page</TITLE>
</HEAD>
<BODY >
<form 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"
    AllowSorting="True"
    OnSortCommand="Sort_Grid"
>
    <Columns>
        <asp:HyperLinkColumn
            HeaderText="Employee ID"
            DataNavigateUrlField="EmployeeID"
            DataNavigateUrlFormatString="./test.aspx?EmployeeID={0}"
            DataTextField="EmployeeID"
            Target="_self"
            SortExpression="Employee ID"
        />
        <asp:BoundColumn 
            HeaderText="Last Name" 
            DataField="EmployeeLastName"
            SortExpression="Last Name"
        />
        <asp:BoundColumn 
            HeaderText="First Name" 
            DataField="EmployeeFirstName"
        />
        <asp:BoundColumn 
            HeaderText="Salary"
            DataField="Salary"
            DataFormatString="{0:C}"
            SortExpression="Salary"
        />
        <asp:BoundColumn 
            HeaderText="Birth Date"
            DataField="BirthDate"
            DataFormatString="{0:d}"
            SortExpression="Birth Date"
        />
    </Columns>  
</asp:DataGrid>
</Font>
</Form>
</BODY>
</HTML>



Add navigator to asp:datagrid (VB.net)

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<script runat=server>
Private MyDT As New DataTable
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    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 Sub
Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
    dg1.CurrentPageIndex = e.NewPageIndex
    dg1.DataSource = MyDT
    dg1.DataBind()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>DataGrid 5 Control Sample Page</TITLE>
</HEAD>
<BODY >
<form 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"
    PagerStyle-BackColor="White"
    PagerStyle-ForeColor="DarkGreen"
    PagerStyle-Font-Bold="True"
    AllowPaging="True"
    PageSize=2
    OnPageIndexChanged="Page_Change"
>
    <Columns>
        <asp:BoundColumn
            HeaderText="Employee ID"
            DataField="EmployeeID"
        />
        <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}"
        />
    </Columns>  
</asp:DataGrid>
</Font>
</Form>
</BODY>
</HTML>



AlternatingItemStyle-Font-Name, AlternatingItemStyle-Font-Italic (VB.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
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>DataGrid 2 Control Sample Page</TITLE>
</HEAD>
<BODY  >
<form runat="server">
<Font Face="Tahoma">
<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"
/>
</Font>
</Form>
</BODY>
</HTML>



AlternatingItemStyle-ForeColor, AlternatingItemStyle-BackColor for ASP:DataGrid (VB.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
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>DataGrid 2 Control Sample Page</TITLE>
</HEAD>
<BODY  >
<form runat="server">
<Font Face="Tahoma">
<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"
/>
</Font>
</Form>
</BODY>
</HTML>



BorderColor, CellPadding, CellSpacing for ASP:DataGrid (VB.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
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>DataGrid 2 Control Sample Page</TITLE>
</HEAD>
<BODY  >
<form runat="server">
<Font Face="Tahoma">
<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"
/>
</Font>
</Form>
</BODY>
</HTML>



Font-Name, Font-Size for ASP:DataGrid (VB.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
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>DataGrid 2 Control Sample Page</TITLE>
</HEAD>
<BODY  >
<form runat="server">
<Font Face="Tahoma">
<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"
/>
</Font>
</Form>
</BODY>
</HTML>



ForeColor, BackColor for ASP:DataGrid (VB.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
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>DataGrid 2 Control Sample Page</TITLE>
</HEAD>
<BODY  >
<form runat="server">
<Font Face="Tahoma">
<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"
/>
</Font>
</Form>
</BODY>
</HTML>



HeaderStyle-BackColor, HeaderStyle-Font-Bold (VB.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
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>DataGrid 2 Control Sample Page</TITLE>
</HEAD>
<BODY  >
<form runat="server">
<Font Face="Tahoma">
<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"
/>
</Font>
</Form>
</BODY>
</HTML>



Set item style, header style and alternating item style for the asp datagrid (C#)

<%@Page Language="C#"%>
<html>
<body>
<asp:datagrid  id="dgrTest" runat="server"
               forecolor="#000000"
               backcolor="#ffffff"
               cellpadding="3"
               gridlines="none"
               width="50%"
               font-name="tahoma,arial,sans-serif"
               font-size="14px">
  <headerstyle forecolor="#ffffff"
               backcolor="#808080"
               font-name="tahoma,arial,sans-serif"
               font-size="12px"
               font-bold="true"
               wrap="false" />
  <itemstyle   font-name="tahoma,arial,sans-serif"
               font-size="10px" />
  <alternatingitemstyle font-name="tahoma,arial,sans-serif"
               font-size="16px"
               backcolor="#ffff00" />
</asp:datagrid>
</body>
</html>
<script Language="C#" runat="server">
void Page_Load() {
   // create an ArrayList of values to bind to
   ArrayList arrValues = new ArrayList(4);
   arrValues.Add("A");
   arrValues.Add("B");
   arrValues.Add("C");
   arrValues.Add("D");
   arrValues.Add("E");
   // set the DataSource property of the control to the
   // array and bind it to display the values
   dgrTest.DataSource = arrValues;
   dgrTest.DataBind();
}
</script>