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!
| | | | Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right." FREE! Go There Now!
| | | | Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
| | | | Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
| | | | Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
| | | | Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
| | | | Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. FREE! Go There Now!
| | | | Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
| | | | Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |