mrfreester
mrfreester

Reputation: 1991

Adding String Array to listView Row Control C# asp.net

This is my first attempt at asp.net and webforms, windowsforms, all of it. I originally populated data in a listbox, but I could only get one column and thought the listView sounded like it would do what I wanted.

The most promising solution I found was this:

    DataSet listData = new DataSet();
    CancellationsControls cancelCtrl = new CancellationsControls();
    listData = cancelCtrl.GetScheduledReleaseDataSet();

    DataTable dtable = listData.Tables[0];

    scheduledReleasesTag.Items.Clear();
    foreach (DataRow row in dtable.Rows)
    {
        string[] ar = Array.ConvertAll(row.ItemArray, p => p.ToString());
        scheduledReleasesTag.Items.Add(new ListViewDataItem(ar));
    }

The dtable is a custom table of a query joining a number of tables.

in the foreach loop the ar string array succesfully shows the colums of data that I want, but the ListViewDataItem requires two int arguments instead of a string array like the example I pulled this from.

I've tried to figure out more about how the listView control works, but this is as close as I have been able to get to getting anything. Any help with explanations would be very appreciated.

Thank you :)

Upvotes: 0

Views: 4067

Answers (1)

Krzysztof Cieslinski
Krzysztof Cieslinski

Reputation: 924

I'm pretty beginner in asp, but to bind ListView control with data, smth like this should work:

DataSet listData = new DataSet();
CancellationsControls cancelCtrl = new CancellationsControls();
listData = cancelCtrl.GetScheduledReleaseDataSet();

DataTable dtable = listData.Tables[0];
ListView1.DataSource = dtable;
ListView1.DataBind();

Now we have to create an ItemTemplate. Your ListView control should look like:

<asp:ListView ID="ListView1" runat="server">
       <ItemTemplate>
      <tr id="Tr1" class="item" runat="server">
        <td>
          <asp:Label ID="column_name" runat="server" Text='<%# Eval("column_name") %>' />
        </td>
        </tr>
        <tr id="Tr2" class="item" runat="server">
        <td>
          <asp:Label ID="another_column_name" runat="server" Text='<%# Eval("another_column_name") %>' />
        </td>
        </tr>
        </ItemTemplate> 
</asp:ListView>

Just replace markers(column_name,another_column_name) with names of columns, that contains data you want to display. This template displays pairs of rows values from two columns one by one.

Upvotes: 2

Related Questions