Hello All,

I am binding a datalist to some table 0 of a dataset from the database. When I check the number of rows in the dataset table 0 it's coming as 129 but when I check the count of the datalist items(using Datalist_Name.Items.Count) inside the datalist item created event I am getting a maximum count of 128.Due, to this I am unable to assign values to the last label control(last as in the last row) present inside the datalist dynamically. I am not sure where I am going wrong. I even tried to replicate the same code inside the ItemDataBound event but even then I am getting the same error.Any ideas will be greatly appreciated.


Thanks.

Dani AI

Generated

Short answer: the DataList builds its items as it enumerates the data source, so calling DataList.Items.Count from inside the per-item events will usually show the number of items created so far (not the final total). That explains why you saw 128 while the DataSet table had 129 — the collection is still being built while ItemCreated/ItemDataBound fire. (learn.microsoft.com)

A reliable pattern is to capture the data-row count before binding and use that stored count when you need to detect the “last” item inside ItemDataBound. Store the source count (ViewState, a private field, or a page-level variable) immediately before calling DataBind, then in ItemDataBound check e.Item.ItemIndex == total - 1 and update the label only for data items (Item or AlternatingItem). Example (C#):

/* store count and bind */
ViewState["TotalRows"] = ds.Tables[0].Rows.Count;
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();

/* inside ItemDataBound */
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
    int total = (int)ViewState["TotalRows"];
    if (e.Item.ItemIndex == total - 1)
    {
        var lbl = (Label)e.Item.FindControl("lblMyLabel");
        if (lbl != null) lbl.Text = "value for last row";
    }
}

If you prefer to work after the whole control is built, handle the DataList.DataBound event — at that point DataList.Items contains every data item and you can address the last item directly (for example: DataList1.Items[DataList1.Items.Count - 1]). (learn.microsoft.com)

Notes and troubleshooting:

  • Always check e.Item.ItemType because header/footer/separator items also raise data-binding events. (learn.microsoft.com)
  • As noted, manually constructing the list works too, but capturing the source count or using DataBound is simpler and less error-prone. ’s symptom fits the incremental-binding behavior above; using the stored-count or DataBound approach will let you safely update the last label.

Recommended Answers

All 2 Replies

personally when populating from a dataset to any other type of container I use the following loop to ensure I get all the values. Since you didn't include any code to see what your current binding logic looks like I'll give an example below:

for (int a = 0; a < dataSetName.Tables["0"].Rows.Count; a++)
{
    DataRow dataRowName = dataSetName.Tables["0"].Rows[a];
    insert data item setup here
    dataListName.Items.Add(dataItemName);
}

Once the loop terminates all items from the original dataset table should be within the new datalist. If the list is only to be populated once then I would ensure that the procedure for this load is within a segment such as:

if (Page.IsPostBack == false)
{
    datalistGenerator();
}

Or... failing that, include a

dataListName.Items.Clear();

line pre-loading to ensure that you don't duplicate your entries.

Hope this helps, please mark solved if it does :)

personally when populating from a dataset to any other type of container I use the following loop to ensure I get all the values. Since you didn't include any code to see what your current binding logic looks like I'll give an example below:

for (int a = 0; a < dataSetName.Tables["0"].Rows.Count; a++)
{
    DataRow dataRowName = dataSetName.Tables["0"].Rows[a];
    insert data item setup here
    dataListName.Items.Add(dataItemName);
}

Once the loop terminates all items from the original dataset table should be within the new datalist. If the list is only to be populated once then I would ensure that the procedure for this load is within a segment such as:

if (Page.IsPostBack == false)
{
    datalistGenerator();
}

Or... failing that, include a

dataListName.Items.Clear();

line pre-loading to ensure that you don't duplicate your entries.

Hope this helps, please mark solved if it does :)

Thanks for the reply.I figured out the issue with my code :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.