Friday, 23 August 2013

Dynamic Formview from Datatable

Dynamic Formview from Datatable

I am trying to create a dynamic formview and bind a datatable to it, it
seems to create the formview but when i try to bind the datatable to it,
it doesn't display anything. Below is the code that i have:
test3.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class test3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = GetTable();
Response.Write("Back - " + table.Rows.Count);
FormView frm = new FormView();
frm.ID = "FormView1";
frm.DataSource = table;
frm.DataBind();
DynamicControlsHolder1.Controls.Add(frm);
}
static DataTable GetTable()
{
//
// Here we create a DataTable with four columns.
//
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
//
// Here we add five DataRows.
//
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
}
}
test3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test3.aspx.cs"
Inherits="test3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="DynamicControlsHolder1"
runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>

No comments:

Post a Comment