A sample code to Create Bound Columns Dynamically- The demonstration shows the Dropdownlist poulated with the Tables in Database pubs on Page_Load event - On selection of the table from the dropdown it display the fieldnames in the listbox. - If no date is selected from the listbox the requiredfieldvalidator shows the error accordingly - As there are two buttons and the requiredfield validator should show error only when the button to show column data is clicked button showing the column names has the causevalidation = false - For multiselect of the listbox the listitem is being used.[bold]Step 1: main.aspx[/bold]
<TABLE id="Table1" style="Z-INDEX: 108; LEFT: 44px; POSITION: absolute; TOP: 24px" cellSpacing="1" cellPadding="1" width="100%" border="1">
<TR>
<TD> <asp:Label id="Label1" runat="server"> Tables</asp:Label></TD>
<TD> <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList></TD>
<TD> <asp:Button id="btnShow" runat="server" Text="Show Column Info" CausesValidation="False"></asp:Button></TD>
</TR>
<TR>
<TD style="HEIGHT: 29px"> <asp:Label id="Label3" runat="server">Column Information</asp:Label></TD>
<TD style="HEIGHT: 29px"> <asp:ListBox id="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox> <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Select Item(s) from List box" ControlToValidate="ListBox1"></asp:RequiredFieldValidator></TD>
<TD style="HEIGHT: 29px"> <asp:Button id="Button1" runat="server" Text="Show Data"></asp:Button></TD>
</TR>
<TR>
<TD></TD>
<TD> <asp:DataGrid id="DataGrid1" AutoGenerateColumns="false" runat="server" Height="100px"></asp:DataGrid></TD>
<TD></TD>
</TR>
</TABLE>
[bold]Step 2:main.aspx.vb[/bold] Dim myconnection As SqlClient.SqlConnection
Dim myda As SqlClient.SqlDataAdapter
Dim ds As DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
myconnection = New SqlClient.SqlConnection("Server=localhost;uid=sa;password=;database=pubs;")
If Not Page.IsPostBack Then
myda = New SqlClient.SqlDataAdapter("Select * from sysobjects where xtype='u'", myconnection)
ds = New DataSet()
myda.Fill(ds, "AllTables")
DropDownList1.DataTextField = "name"
DropDownList1.DataSource = ds.Tables(0)
DropDownList1.DataBind()
End If
End Sub
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
ListBox1.Items.Clear()
myda = New SqlClient.SqlDataAdapter("Select * from " & DropDownList1.SelectedItem.Text, myconnection)
ds = New DataSet()
myda.Fill(ds, "TableName")
Dim dc As DataColumn
For Each dc In ds.Tables(0).Columns
ListBox1.Items.Add(dc.ColumnName)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myda = New SqlClient.SqlDataAdapter("Select * from " & DropDownList1.SelectedItem.Text, myconnection)
ds = New DataSet()
myda.Fill(ds, "TableName")
Dim i As Integer
Dim li As ListItem
For Each li In ListBox1.Items
If li.Selected Then
Dim objbc As New BoundColumn()
objbc.DataField = li.Text
objbc.HeaderText = li.Text
DataGrid1.Columns.Add(objbc)
DataGrid1.DataSource = ds.Tables(0)
DataGrid1.DataBind()
End If
Next
End Sub
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP.NET Code Articles More By Sushila Bowalekar developerWorks - FREE Tools! | Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
| | | | Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations. FREE! Go There Now!
| | | | Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
| | | | Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available. FREE! Go There Now!
| | | | The Eclipse SOA Tools Platform (STP) plug-in and Apache Tuscany simplifies services development through the use of the popular Eclipse development environment. Apache Tuscany has also been integrated with the STP to provide a Service Component Architecture (SCA) Java run time for the services you create, allowing you to annotate your service using the SCA standard and Apache Tuscany annotations. In this tutorial, you will see STP and Apache Tuscany in action, through the creation of a Remote Method Invocation (RMI) service. FREE! Go There Now!
| | | | It's a good time to be a Web developer. You've never had more choices in terms of technologies. There are so many great open source Web servers, databases, programming languages, and development frameworks. No matter what combination of technologies you prefer to work with, there is an integrated development environment (IDE) that can increase your productivity: Eclipse. In this tutorial, Part 1 of a three-part "Web development with Eclipse Europa" series on how to use Eclipse for Web development with Java technology, PHP, and Ruby, we'll see how the latest release of Eclipse -- Europa -- can be used to rapidly develop Java Web applications. We'll use Java Platform, Enterprise Edition 5 (Java EE) for Eclipse to build a Web application for tracking and calculating baseball statistics. FREE! Go There Now!
| | | | No matter what combination of technologies you prefer to work with as a Web developer, Eclipse is a single integrated development environment (IDE) that can increase your productivity. In Part 2, we'll see how easy it is to develop PHP applications using a different set of Eclipse plug-ins, collectively known as the PHP Development Toolkit (PDT.) FREE! Go There Now!
| | | | In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
| | | | Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |