ASP.NET(vb.net) & ListView - DataSet,DataTable,TableRow Example scripts how to use ListView control in asp.net , Binding the datasource (DataSet,DataTable,TableRow) to ListView control.
ShotDev Focus:
- ASP.NET(vb.net) & ListView - DataSet,DataTable,TableRow
Example
ListViewDataSetDataTable.aspx
<br> <%@ Import Namespace="System.Data"%><br> <%@ Import Namespace="System.Data.OleDb"%><br> <%@ Page Language="VB" %><br> <script runat="server"></p> <p>Sub Page_Load(sender As Object, e As EventArgs)<br> Dim dt As DataTable</p> <p>'*** DataSet To DataTable ***'<br> 'dt = CreateDsToDt()</p> <p>'*** DataTable ***'<br> dt = CreateDataTable()</p> <p>'*** DataSet ***'<br> 'dt = CreateDataSet.Tables(0) '*** Convert DataSet to DataTable ***'</p> <p>'*** TableRows ***'<br> 'dt = DataTableRows()</p> <p>'*** BindData to ListView ***'<br> myListView.DataSource = dt<br> myListView.DataBind()<br> End Sub</p> <p>'*** DataTable ***'<br> Function CreateDataTable() As DataTable<br> Dim objConn As New OleDbConnection<br> Dim dtAdapter As OleDbDataAdapter<br> Dim dt As New DataTable</p> <p>Dim strConnString As String<br> strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"<br> objConn = New OleDbConnection(strConnString)<br> objConn.Open()</p> <p>Dim strSQL As String<br> strSQL = "SELECT * FROM customer"</p> <p>dtAdapter = New OleDbDataAdapter(strSQL, objConn)<br> dtAdapter.Fill(dt)</p> <p>dtAdapter = Nothing</p> <p>objConn.Close()<br> objConn = Nothing</p> <p>Return dt '*** Return DataTable ***'</p> <p>End Function</p> <p>'*** DataSet ***'<br> Function CreateDataSet() As DataSet<br> Dim objConn As New OleDbConnection<br> Dim objCmd As New OleDbCommand<br> Dim dtAdapter As New OleDbDataAdapter</p> <p>Dim ds As New DataSet<br> Dim strConnString,strSQL As String</p> <p>strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"<br> strSQL = "SELECT * FROM customer"</p> <p>objConn.ConnectionString = strConnString<br> With objCmd<br> .Connection = objConn<br> .CommandText = strSQL<br> .CommandType = CommandType.Text<br> End With<br> dtAdapter.SelectCommand = objCmd</p> <p>dtAdapter.Fill(ds)</p> <p>dtAdapter = Nothing<br> objConn.Close()<br> objConn = Nothing</p> <p>Return ds '*** Return DataSet ***'</p> <p>End Function</p> <p>'*** DataSet to DataTable ***'<br> Function CreateDsToDt() As DataTable<br> Dim objConn As New OleDbConnection<br> Dim objCmd As New OleDbCommand<br> Dim dtAdapter As New OleDbDataAdapter</p> <p>Dim ds As New DataSet<br> Dim dt As DataTable<br> Dim strConnString,strSQL As String</p> <p>strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"<br> strSQL = "SELECT * FROM customer"</p> <p>objConn.ConnectionString = strConnString<br> With objCmd<br> .Connection = objConn<br> .CommandText = strSQL<br> .CommandType = CommandType.Text<br> End With<br> dtAdapter.SelectCommand = objCmd</p> <p>dtAdapter.Fill(ds)<br> dt = ds.Tables(0)</p> <p>dtAdapter = Nothing<br> objConn.Close()<br> objConn = Nothing</p> <p>Return dt '*** Return DataTable ***'</p> <p>End Function</p> <p>'*** TableRows (DataTable) ***'<br> Function DataTableRows() As DataTable<br> Dim dt As New DataTable<br> Dim dr As DataRow</p> <p>'*** Column ***'<br> dt.Columns.Add("CustomerID")<br> dt.Columns.Add("Name")<br> dt.Columns.Add("Email")<br> dt.Columns.Add("CountryCode")<br> dt.Columns.Add("Budget")<br> dt.Columns.Add("Used")</p> <p>'*** Rows ***'<br> dr = dt.NewRow<br> dr("CustomerID") = "C001"<br> dr("Name") = "Win Weerachai"<br> dr("Email") = "win.weerachai@shotdev.com"<br> dr("CountryCode") = "TH"<br> dr("Budget") = "1000000"<br> dr("Used") = "600000"<br> dt.Rows.Add(dr)</p> <p>'*** Rows ***'<br> dr = dt.NewRow<br> dr("CustomerID") = "C002"<br> dr("Name") = "Jake Sully"<br> dr("Email") = "jake.sully@shotdev.com"<br> dr("CountryCode") = "EN"<br> dr("Budget") = "2000000"<br> dr("Used") = "800000"<br> dt.Rows.Add(dr)</p> <p>'*** Rows ***'<br> dr = dt.NewRow<br> dr("CustomerID") = "C003"<br> dr("Name") = "Tony Stark"<br> dr("Email") = "tony.stark@shotdev.com"<br> dr("CountryCode") = "US"<br> dr("Budget") = "3000000"<br> dr("Used") = "600000"<br> dt.Rows.Add(dr)</p> <p>'*** Rows ***'<br> dr = dt.NewRow<br> dr("CustomerID") = "C004"<br> dr("Name") = "Peter Parker"<br> dr("Email") = "peter.parker@shotdev.com"<br> dr("CountryCode") = "US"<br> dr("Budget") = "4000000"<br> dr("Used") = "100000"<br> dt.Rows.Add(dr)</p> <p>Return dt '*** Return DataTable ***'<br> End Function</p> <p></script><br> <html><br> <head><br> <title>ShotDev.Com Tutorial</title><br> </head><br> <body><br> <form id="form1" runat="server"><br> <asp:ListView ID="myListView" runat="server" DataKeyNames="CustomerID"></p> <p><LayoutTemplate><br> <table><br> <tr><br> <td><br> <table runat="server" border="1"><br> <tr><br> <th runat="server"><br> CustomerID</th><br> <th runat="server"><br> Name</th><br> <th runat="server"><br> Email</th><br> <th runat="server"><br> CountryCode</th><br> <th runat="server"><br> Budget</th><br> <th runat="server"><br> Used</th><br> </tr><br> <tr ID="itemPlaceholder" runat="server"><br> </tr><br> </table><br> </td><br> </tr><br> </table><br> </LayoutTemplate></p> <p><ItemTemplate><br> <tr><br> <td><br> <asp:Label ID="lblCustomerID" runat="server" Text='<%# Eval("CustomerID") %>' /><br> </td><br> <td><br> <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' /><br> </td><br> <td><br> <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>' /><br> </td><br> <td><br> <asp:Label ID="lblCountryCode" runat="server" Text='<%# Eval("CountryCode") %>' /><br> </td><br> <td><br> <asp:Label ID="lblBudget" runat="server" Text='<%# Eval("Budget") %>' /><br> </td><br> <td><br> <asp:Label ID="lblUsed" runat="server" Text='<%# Eval("Used") %>' /><br> </td><br> </tr><br> </ItemTemplate></p> <p></asp:ListView><br> </form><br> </body><br> </html><br>
Create a asp.net file and save to path root-path/dotnet/
Run
http://localhost/dotnet/ListViewDataSetDataTable.aspx
Screenshot
3qualification…
…