serkan sendur 821 Postaholic Banned

When you use datalist setting its repeatColumn property to a constant number, its height depends on its datasource. Sometimes we want to display 10 item in one column and start a new column next to it. For these occasions i created a function as follows :

// in my case i have a generic list as the datasource, if you use datatable then you have to add empty rows instead of empty list items. If you don't add empty items or rows, datalist will distribute the items proportionately to each column. let say you have 24 items, it will put 8 item for each of the 3 columns, so you need to add 6 empty columns in order for the datalist to add 10 to first 10 to second and 4 to third column(the empty 6 item will not be displayed(empty cells are not displayed in html tables))

private List<entCategory> FillMenuDatalists(DataList dtl, List<entCategory> _datasource)
{
    int _count = _datasource.Count;
    if (_count > 10)
    {
        int _remainder = _count % 10;
        int _itemsToAppend;
        if (_remainder > 0)
        {
            _itemsToAppend = 10 - _remainder;
        }
        else
        {
            _itemsToAppend = 0;
        }
        for (int i = 0; i < _itemsToAppend; i++)
        {
            entCategory item = new entCategory();
            _datasource.Add(item);
        }
        int _repeatColumns = (_datasource.Count) / 10;
        dtl.RepeatColumns = _repeatColumns;
    }
    return _datasource;
}
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.