0

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 :)

5
  • What does the markup for your listview look like? Also, not sure if you've looked at the examples on MSDN, but they might be helpful. Commented Feb 1, 2012 at 19:25
  • Sounds almost like a Binding issue at first glance Commented Feb 1, 2012 at 19:26
  • Unfortunately I'm new to terminology as well. Here is what is in the .aspx file if that's what you meant. <tr> <asp:ListView ID="scheduledReleasesTag" runat="server" /> </tr> Commented Feb 1, 2012 at 19:28
  • @DJKRAZE yeah I don't need to necessarily bind the data using the string array. I've got the dtable object working the way I want, I just don't know how to bind that data to the listView Commented Feb 1, 2012 at 19:34
  • It should have binding as well.. this would be done with templating Commented Feb 1, 2012 at 19:38

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, that's exactly what I needed to know. I didn't know about setting the template stuff up. thank you :)
Im glad :).I see that you are new on StackOverflow, it's nice to give a tick near the post that resolves your question and vote it up ;). Greets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.