将一行数据添加到 ListView VB.Net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24209718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:31:51  来源:igfitidea点击:

Add row of data to ListView VB.Net

vb.netlistviewlistviewitemitemtemplate

提问by GivenPie

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 ListViewItemas 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.

我正在尝试将一行数据添加到五列列表视图中。我在将字符串数组传递到 a 时遇到问题,ListViewItem因为它不接受数据数组。我几乎没有使用这些视图的经验,所以如果有人能指出我做错了什么,那就太好了。

My listview is lvChanges

我的列表视图是 lvChanges



I have the following setup, is my current implementation utilizing ListView correctly?

我有以下设置,我当前的实现是否正确使用 ListView?

Column 1 to 4: String

第 1 到 4 列:字符串

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"?

第 5 列:LinkBut​​ton,CommandName="ApproveChange" - 在代码中处理,但是,我不确定如何在CommandArgument="?" 中传递行的标识符。. 我见过很多示例使用<%# Eval('someKey') %>它是如何工作的?我怎样才能传递钥匙?我需要只读属性吗?只要我的页面后面的代码有对象,属性就可以直接“评估”吗?



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"

我正在尝试在行中添加有错误的项目:“类型‘一维字符串数组’的值无法转换为‘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

上面的数据,应该进入 中的五个<td>标签ItemTemplate,如下

<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.

对于CommandArgument,如何才能让行知道利益相关者 ID 以完成这块的功能。

回答by Eddy Jawed

Save it as a datatable and use the following code (make sure there are no NULLs in your dataset)

将其另存为数据表并使用以下代码(确保数据集中没有 NULL)

   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

回答by GivenPie

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.

然后在aspx页面中,使用Eval()选择列表中元素的索引,即一行字符串数据。

       <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

好的。简单的。Eval()很直接