Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to find last item index in datalist???
Posted

Hi,


try this line to find last index of datalist.

C#
int lstindex = DataList1.Items.Count - 1;
 
Share this answer
 
How about an extension method or two

C#
using System.Web.UI.WebControls;

namespace Dummy
{
    public static class DatalistExtension
    {
        public static DataListItem GetLastItem(this DataList list)
        {
            if (list.HasItems())
            {
                return list.Items[list.Items.Count - 1];
            }

            return null;
        }
     
        public static bool HasItems(this DataList list)
        {
            return list.Items.Count > 0;
        }
    }
}

and an example of its usage
C#
DataList dl = new DataList();
dl.DataSource = new List<string>() { "One", "Two" };
dl.DataBind();

DataListItem dli = dl.GetLastItem();
</string>
 
Share this answer
 
try:try the following


int count = DataList1.Items.Count - 1;
 
Share this answer
 
v4
Comments
nagendrathecoder 20-Sep-11 5:27am    
Answer is wrong and the syntax of statement is also wrong.
You are fetching count as a string and assigning it to int.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900