Reputation: 1489
I'm, trying to add a row of data to a five columned list view. I have an issue with passing an array of strings into a ListViewItem
as it does not accept the array of data. I have little experience using these views so if someone could point out what I'm doing wrong, that would be great.
My listview is lvChanges
I have the following setup, is my current implementation utilizing ListView correctly?
Column 1 to 4: String
Column 5: LinkButton, CommandName="ApproveChange" - which is handled in code, however, I'm not sure how to pass an identifier for the row in CommandArgument="?". I've seen many example use <%# Eval('someKey') %>
How does that work? How can I pass the key? Do I need a read only property? So long as my code behind page has objects, can the attributes be directly "Eval'd"?
I'm trying to add items with error on the line: "Value of type '1-dimentional array of String' cannot be converted to 'System.Web.UI.WebControls.ListViewItemType"
For i As Integer = 0 To addressList.Count
Dim item As ListViewItem
Dim data(5) As String
data(0) = "Adress Change"
data(1) = stakeholderList(i).ToString
data(2) = stakeholderList(i).Address.ToString
data(3) = addressList(i).ToString
data(4) = "need the link button here!"
item = New ListViewItem(data) ' <-- Error
lvChanges.Items.Add(item)
Next
The data above, should go into five <td>
tags in the ItemTemplate
, as below
<asp:ListView ID="lvChanges" runat="server">
<LayoutTemplate>
<table cellpadding="2" width="640px" border="1" ID="tbl1" runat="server">
<tr id="Tr1" runat="server" style="background-color: #dfdbdf">
<th runat="server"><%= Type %></th>
<th runat="server"><%= Requester %></th>
<th runat="server"><%= OldAddress%></th>
<th runat="server"><%= NewAddress%></th>
<th runat="server"><%= Decision%></th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server" >
<td></td>
<td></td>
<td></td>
<td>
<asp:LinkButton runat="server"
ID="btnApprove"
Text="Approve"
CommandName="ApproveChange"
CommandArgument='<%# Eval("?") %>'/>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
For the CommandArgument
, how can it so that row knows the stakeholder ID in order to complete the functionality of this piece.
Upvotes: 0
Views: 30741
Reputation: 457
Save it as a datatable and use the following code (make sure there are no NULLs in your dataset)
Public Sub ShowDataInLvw(ByVal data As DataTable, ByVal lvw As ListView)
lvw.View = View.Details
lvw.GridLines = True
lvw.Columns.Clear()
lvw.Items.Clear()
For Each col As DataColumn In data.Columns
lvw.Columns.Add(col.ToString)
Next
For Each row As DataRow In data.Rows
Dim lst As ListViewItem
lst = lvw.Items.Add(row(0))
For i As Integer = 1 To data.Columns.Count - 1
Debug.Print(row(i).ToString)
lst.SubItems.Add(row(i))
Next
Next
End Sub
Upvotes: 2
Reputation: 1489
Trick is to add rows into a list and set data source as the list.
Private result As New List(Of String())
For i As Integer = 0 To addressList.Count
Dim data(5) As String
data(0) = "Adress Change"
data(1) = stakeholderList(i).ToString
data(2) = stakeholderList(i).Address.ToString
data(3) = addressList(i).ToString
data(4) = stakeholderList(i).ID
result.Add(data)
Next
lvChanges.DataSource = result
lvChanges.DataBind()
Then in aspx page, use Eval()
to select the index of the elements in the list, which is a row of string data.
<ItemTemplate>
<tr runat="server" >
<td><asp:Label ID="lblType" runat="server" Text='<%# Eval("[0]") %>'></asp:Label></td>
<td><asp:Label ID="lblRequester" runat="server" Text='<%# Eval("[1]") %>'></asp:Label></td>
<td><asp:Label ID="lblOld" runat="server" Text='<%# Eval("[2]") %>'></asp:Label></td>
<td><asp:Label ID="lblNew" runat="server" Text='<%# Eval("[3]") %>'></asp:Label></td>
<td>
<asp:LinkButton runat="server"
ID="btnApprove"
Text="Approve"
CommandName="ApproveChange"
CommandArgument='<%# Eval("[4]") %>'/>
</td>
</tr>
</ItemTemplate>
ok. simple. Eval()
is pretty straight forward
Upvotes: 0