Generate Pyramid with DataList using C# and VB.Net in ASP.Net

davut5
 
on Sep 25, 2017 11:03 PM
Sample_232723.zip
1692 Views

i want to set RepeatColums property 1,2,3,4,5,6,7,8,9,10.

Finally datalist will display like a triangel or pyramid. You can show me diffrent way. No problem. i want to change RepeatColums. First Record first row, second and third records in second row, 4-5-6 records in third row.

<asp:DataList runat="server" ID="datalist1" RepeatLayout="Flow" RepeatColumns="2">
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" CssClass="link" NavigateUrl='PlayerDetails.aspx?id=<%# Eval("Id") %>' runat="server"> <%# Eval("Sira") %>. <%# Eval("AdSoyad") %></asp:HyperLink>
    </ItemTemplate>
</asp:DataList>

 

public void pyramidlist()
{
    SqlConnection path= new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
    path.Open();
    SqlDataAdapter da = new SqlDataAdapter("Select * from Players", yol);
    DataTable dt = new DataTable();
    da.Fill(dt);
    List<Players> playerList= new List<Players>();
    playerList= (from DataRow dr in dt.Rows
                     select new Players()
                     {
                         Name= (dr["Name"].ToString()),
                         Order= int.Parse(dr["Order"].ToString()),
                         ID = int.Parse(dr["ID"].ToString())
 
                     }).ToList();
 
    playerList= playerList.OrderBy(x => x.ID).ToList();
 
 
    datalist1.DataSource = playerList;
    datalist1.DataBind();
 
public class Players
{
    public string Name{ get; set; }
    public int Order{ get; set; }
    public int ID { get; set; }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
kalpesh
 
on Sep 26, 2017 05:54 AM
on Sep 26, 2017 06:19 AM

Hi davut5,

You cannot change RepeatColumns ones you bind the data to DataList you can only assign RepeatColumns value ones for his population.

But you can do one think just add one Repeater or Grid and in ItemTemplatefiled of Repeater or Grid  you can add DataList and on OnItemDataBound or OnRowDataBound you can sets its RepeatColumn Property for each iteration of Repeater or Grid for inner DataList.

Refer Below Code for your reference and try to implement in your code as per your coding logic.

HTML

<asp:Repeater ID="rptCount" runat="server" OnItemDataBound="rptCount_ItemDataBound">
    <HeaderTemplate>
        <table cellpadding="5" cellspacing="0">
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:HiddenField ID="rowId" Value='<% #Eval("rowId") %>' runat="server" />
                <div align="center">
                    <asp:DataList runat="server" ID="datalist1" RepeatLayout="Flow" RepeatColumns="2">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" CssClass="link" NavigateUrl='<%# string.Format("~/PlayerDetails.aspx?id={0}",Eval("Id")) %>'
                                runat="server" Style="color: Blue"> <%# Eval("Name")%>. <%# Eval("Order")%></asp:HyperLink>
                        </ItemTemplate>
                    </asp:DataList>
                </div>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

C#

private List<Players> Players
{
    get
    {
        return (List<Players>)ViewState["Players"];
    }
    set
    {
        ViewState["Players"] = value;
    }
}    

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        // Add Your DataTable Binding Code 
        DataTable dtCount = new DataTable();
        dtCount.Columns.AddRange(new DataColumn[1] { new DataColumn("rowId", typeof(string)) });
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Name",typeof(string))
                                                , new DataColumn("Order",typeof(string))
                                            , new DataColumn("ID",typeof(string))});
        dt.Rows.Add("User 1", "1", 11);
        dt.Rows.Add("User 2", "2", 12);
        dt.Rows.Add("User 3", "3", 13);
        dt.Rows.Add("User 4", "4", 14);
        dt.Rows.Add("User 5", "5", 15);
        dt.Rows.Add("User 6", "6", 16);
        dt.Rows.Add("User 7", "7", 17);
        dt.Rows.Add("User 8", "8", 18);
        dt.Rows.Add("User 9", "9", 19);
        dt.Rows.Add("User 10", "10", 20);
        dt.Rows.Add("User 11", "11", 21);
        dt.Rows.Add("User 12", "12", 22);
        dt.Rows.Add("User 13", "13", 23);
        dt.Rows.Add("User 14", "14", 24);
        dt.Rows.Add("User 15", "15", 25);
        // Your DataTable Binding Code            

        List<Players> PlayerList = new List<Players>();
        PlayerList = (from DataRow dr in dt.Rows
                        select new Players()
                        {
                            Name = (dr["Name"].ToString()),
                            Order = int.Parse(dr["Order"].ToString()),
                            ID = int.Parse(dr["ID"].ToString())
                        }).ToList();

        PlayerList = PlayerList.OrderBy(x => x.ID).ToList();

        int IncrementCount = 1;
        int ListCount = PlayerList.Count;
        bool IsValid = true;
        while (ListCount > 0)
        {
            if (ListCount < IncrementCount)
            {
                IsValid = false;
            }
            ListCount = ListCount - IncrementCount;
            if (IsValid)
            {
                dtCount.Rows.Add(IncrementCount);
                IncrementCount++;
            }
        }

        int playerListIndexCount = 1;
        int TempIncrement = 1;

        for (int i = 0; i < PlayerList.Count; i++)
        {
            PlayerList[i].ListId = playerListIndexCount;
            if (playerListIndexCount == TempIncrement)
            {
                playerListIndexCount++;
                TempIncrement = 1;
            }
            else
            {
                TempIncrement++;
            }
        }

        this.Players = PlayerList;
            
        rptCount.DataSource = dtCount;
        rptCount.DataBind();
    }
}
protected void rptCount_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        int rowId = Convert.ToInt32((e.Item.FindControl("rowId") as HiddenField).Value);
        DataList datalist1 = (e.Item.FindControl("datalist1") as DataList);
        List<Players> PlayerList = new List<Players>();
        PlayerList = Players.FindAll(Player => Player.ListId == rowId);
        datalist1.RepeatColumns = rowId;
        datalist1.DataSource = PlayerList;
        datalist1.DataBind();
    }
}

[Serializable]
public class Players
{
    public string Name { get; set; }
    public int Order { get; set; }
    public int ID { get; set; }
    public int ListId { get; set; }
}

VB.Net

Private Property Players() As List(Of Players)
	Get
		Return DirectCast(ViewState("Players"), List(Of Players))
	End Get
	Set
		ViewState("Players") = value
	End Set
End Property

Protected Sub Page_Load(sender As Object, e As EventArgs)
	If Not Me.IsPostBack Then
		' Add Your DataTable Binding Code 
		Dim dtCount As New DataTable()
		dtCount.Columns.AddRange(New DataColumn(0) {New DataColumn("rowId", GetType(String))})
		Dim dt As New DataTable()
		dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Name", GetType(String)), New DataColumn("Order", GetType(String)), New DataColumn("ID", GetType(String))})
		dt.Rows.Add("User 1", "1", 11)
		dt.Rows.Add("User 2", "2", 12)
		dt.Rows.Add("User 3", "3", 13)
		dt.Rows.Add("User 4", "4", 14)
		dt.Rows.Add("User 5", "5", 15)
		dt.Rows.Add("User 6", "6", 16)
		dt.Rows.Add("User 7", "7", 17)
		dt.Rows.Add("User 8", "8", 18)
		dt.Rows.Add("User 9", "9", 19)
		dt.Rows.Add("User 10", "10", 20)
		dt.Rows.Add("User 11", "11", 21)
		dt.Rows.Add("User 12", "12", 22)
		dt.Rows.Add("User 13", "13", 23)
		dt.Rows.Add("User 14", "14", 24)
		dt.Rows.Add("User 15", "15", 25)
		' Your DataTable Binding Code            

		Dim PlayerList As New List(Of Players)()
		PlayerList = (From dr In dt.Rows _
                              Select New Players() With { _
		.Name = (dr("Name").ToString()), _
		.Order = Integer.Parse(dr("Order").ToString()), _
		.ID = Integer.Parse(dr("ID").ToString()) _
		}).ToList()

		PlayerList = PlayerList.OrderBy(Function(x) x.ID).ToList()

		Dim IncrementCount As Integer = 1
		Dim ListCount As Integer = PlayerList.Count
		Dim IsValid As Boolean = True
		While ListCount > 0
			If ListCount < IncrementCount Then
				IsValid = False
			End If
			ListCount = ListCount - IncrementCount
			If IsValid Then
				dtCount.Rows.Add(IncrementCount)
				IncrementCount += 1
			End If
		End While

		Dim playerListIndexCount As Integer = 1
		Dim TempIncrement As Integer = 1

		For i As Integer = 0 To PlayerList.Count - 1
			PlayerList(i).ListId = playerListIndexCount
			If playerListIndexCount = TempIncrement Then
				playerListIndexCount += 1
				TempIncrement = 1
			Else
				TempIncrement += 1
			End If
		Next

		Me.Players = PlayerList

		rptCount.DataSource = dtCount
		rptCount.DataBind()
	End If
End Sub
Protected Sub rptCount_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
	If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
		Dim rowId As Integer = Convert.ToInt32(TryCast(e.Item.FindControl("rowId"), HiddenField).Value)
		Dim datalist1 As DataList = TryCast(e.Item.FindControl("datalist1"), DataList)
		Dim PlayerList As New List(Of Players)()
		PlayerList = Players.FindAll(Function(Player) Player.ListId = rowId)
		datalist1.RepeatColumns = rowId
		datalist1.DataSource = PlayerList
		datalist1.DataBind()
	End If
End Sub

<Serializable> _
Public Class Players
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Property Order() As Integer
        Get
            Return m_Order
        End Get
        Set
            m_Order = Value
        End Set
    End Property
    Private m_Order As Integer
    Public Property ID() As Integer
        Get
            Return m_ID
        End Get
        Set
            m_ID = Value
        End Set
    End Property
    Private m_ID As Integer
    Public Property ListId() As Integer
        Get
            Return m_ListId
        End Get
        Set
            m_ListId = Value
        End Set
    End Property
    Private m_ListId As Integer
End Class

OutPut