Wednesday, September 8, 2010

example on how to add datatable to dropdown dynamically

by Narendra Sammeta, .Net Developer

protected void Page_Load(object sender, EventArgs e)
{
ddl.DataSource = CreateTable();
ddl.DataBind();


}
public static DataTable CreateTable()
{
// Creating the datatable with four columns.
DataTable Customersdetails = new DataTable();
Customersdetails.Columns.Add("Name", typeof(String));
Customersdetails.Columns.Add("Regno", typeof(String));
Customersdetails.Columns.Add("Phoneno", typeof(Int32));
Customersdetails.Columns.Add("Address", typeof(String));

// Filling the Customersdetails datatable with 5 rows
Customersdetails.Rows.Add("Powell", 10, 1111, "Corkstreet");
Customersdetails.Rows.Add("Martin", 11, 2222, "Abbeystreet");
Customersdetails.Rows.Add("Bond", 12, 3333, "Graftonstreet");
Customersdetails.Rows.Add("Jimmy", 13, 4444, "Henrystreet");
Customersdetails.Rows.Add("Chris", 14, 5555, "Parnellstreet");

return Customersdetails;
}


-- ==================

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:Label Text="Example on how to add Datatable to dropdownlist dynamically" ForeColor="Blue" Font-Bold="true" Font-Size="Large" runat="server" ></asp:Label><hr /><br /><br />
<asp:DropDownList ID="ddl" runat="server" DataTextField="Name"
DataValueField="Name" ondatabinding="ddl_DataBinding"
onselectedindexchanged="ddl_SelectedIndexChanged">
</asp:DropDownList>
<asp:GridView ID="GV" runat="server" ></asp:GridView>
</center>
</div>
</form>
</body>
</html>

0 comments:

Post a Comment

Related Posts with Thumbnails