While binding datalist how to bind in next row based on condition

Last Reply on Mar 18, 2016 08:41 AM By Shashikant

Posted on Mar 18, 2016 08:37 AM

I am using Datalist to bind userdetails which contains department name. With RepeatColumns="2" & RepeatDirection=" Horizontal". Now i want to check a condition while binding.

for Example; If i am binding with 5 users having 3 users with same department and remaining 2 users with different departments then i want to show 1st 3 users having same department in two rows and remaining to users in next two rows.

Can anyone help on this?

<asp:DataList ID="dlHaysContacts" runat="server" RepeatColumns="2" RepeatDirection=" Horizontal">
    <ItemTemplate>
        <div class="dlSettings">
            <div>
                <asp:LinkButton ID="lnkHaysContactName" runat="server" Text='<%# Eval("HaysContactName") %>'
                    OnClientClick='<%# String.Format("EmailToServiceTeam(\"{0}\");", Eval("hays_contact_email")) %>'></asp:LinkButton>
            </div>
            <div>
                <asp:Label Style="text-align: justify" ID="lblServiceArea" runat="server" Text='<%# Eval("department_name") %>'></asp:Label>
            </div>
            <div>
                <asp:Label Style="text-align: justify" ID="lblRole" runat="server" Text='<%# Eval("hays_contact_title") %>'></asp:Label>
            </div>
            <div>
                <b>p: </b>
                <asp:Label ID="lblPhone" runat="server" Text='<%# Eval("hays_contact_phone") %>'></asp:Label>
            </div>
        </div>
    </ItemTemplate>
</asp:DataList>
Posted on Mar 18, 2016 08:41 AM

Please refer below Code.

HTML

<div>
    <asp:DataList ID="dlDepartments" runat="server" OnItemDataBound="ItemDataBound">
        <ItemTemplate>
            <asp:HiddenField ID="hfGrp" runat="server" Value='<%# Eval("department_name") %>' />
            <asp:DataList ID="dlHaysContacts" runat="server" RepeatColumns="2" RepeatDirection=" Horizontal">
                <ItemTemplate>
                    <div class="dlSettings">
                        <div>
                            <asp:LinkButton ID="lnkHaysContactName" runat="server" Text='<%# Eval("HaysContactName") %>'
                                OnClientClick='<%# String.Format("EmailToServiceTeam(\"{0}\");", Eval("hays_contact_email")) %>'></asp:LinkButton>
                        </div>
                        <div>
                            <asp:Label Style="text-align: justify" ID="lblServiceArea" runat="server" Text='<%# Eval("department_name") %>'></asp:Label>
                        </div>
                        <div>
                            <asp:Label Style="text-align: justify" ID="lblRole" runat="server" Text='<%# Eval("hays_contact_title") %>'></asp:Label>
                        </div>
                        <div>
                            <b>p: </b>
                            <asp:Label ID="lblPhone" runat="server" Text='<%# Eval("hays_contact_phone") %>'></asp:Label>
                        </div>
                    </div>
                </ItemTemplate>
            </asp:DataList>
        </ItemTemplate>
    </asp:DataList>
</div>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { 
                new DataColumn("HaysContactName"),
                new DataColumn("hays_contact_email"),
                new DataColumn("department_name"),
                new DataColumn("hays_contact_title"),
                new DataColumn("hays_contact_phone")
            });
        dt.Rows.Add("David", "david@gmail.com", "IT", "Emp", "111111111");
        dt.Rows.Add("Kevin", "Kevin@gmail.com", "Marketing", "Emp", "222222222");
        dt.Rows.Add("Jhon", "Jhon@gmail.com", "IT", "Emp", "3333333333");
        dt.Rows.Add("Todd", "Todd@gmail.com", "IT", "Emp", "444444444");
        dt.Rows.Add("Peter", "Peter@gmail.com", "Marketing", "Emp", "5555555555");
        ViewState["Contacts"] = dt;
        var result = dt.AsEnumerable().GroupBy(x => x["department_name"]).Select(x => new { department_name = x.Key }).ToArray();
        dlDepartments.DataSource = result;
        dlDepartments.DataBind();
    }

}

protected void ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string group = (e.Item.FindControl("hfGrp") as HiddenField).Value;
        DataList lst = e.Item.FindControl("dlHaysContacts") as DataList;
        if (ViewState["Contacts"] != null)
        {
            DataTable dt = ViewState["Contacts"] as DataTable;
            DataTable resultDt = dt.Clone();
            lst.DataSource = dt.AsEnumerable().ToLookup(x => x["department_name"])[group].CopyToDataTable();
            lst.DataBind();
        }
    }
}

Screenshot

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html