|
xxx
DataGrid Trick: Code Only
by Charles Carroll
This grid example demonstrates creating a grid entirely in code. It is identical to the Tag based example at /freebook/learn/columnbinding.aspx.
filename=/experiments/datagridtricks/booksfancy_code.aspx
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Drawing"%>
<html>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
Dim ds As New DataSet
Dim FS As New FileStream(Server.MapPath("/experiments/data/books.xml"), FileMode.Open)
ds.ReadXml(FS)
dim MyDataGrid as new datagrid
with MyDataGrid
'.Width=Unit.Pixel(800)
.width=Unit.Percentage(100)
.BorderColor=Color.FromName("black")
.ShowFooter=false
.CellPadding=3
.CellSpacing=0
.Font.Name="Verdana"
.Font.Size=new FontUnit(12)
.HeaderStyle.BackColor=ColorTranslator.FromHtml("#aaaadd")
end with
MyDataGrid.DataSource = new DataView(ds.Tables(0))
FS.close()
dim temp1 as new hyperlinkcolumn
temp1.DataNavigateUrlField="isbn"
temp1.DataNavigateUrlFormatString="http://www.amazon.com/exec/obidos/ISBN={0}/learnasp"
temp1.Text="Buy!"
MyDataGrid.columns.Add(temp1)
dim temp2 as new boundcolumn
temp2.HeaderText="BookCover"
temp2.DataField="isbn"
temp2.DataFormatString="<img src='http://images.amazon.com/images/P/{0}.01.MZZZZZZZ.jpg' height='60' width='45'>"
MyDataGrid.columns.Add(temp2)
MyDataGrid.DataBind()
dim whatever as control
whatever=Page.FindControl("gridgoeshere")
whatever.controls.Add(MyDataGrid)
End Sub
</script>
<body>
<h3>The Best Books Ever</h3>
<ASP:placeholder id="gridgoeshere" runat="server"/>
</body>
</html>
|