Here is ours .aspx code:
<h2>DataList with two rows and Dynamic
Repeat Columns </h2>
     <asp:DataList id="dlDynamic"
           BorderColor="DarkGray"
           CellPadding="10"
           CellSpacing="4"
           RepeatDirection="Horizontal"
           RepeatLayout="Table"
           Width="100%"
           runat="server" Font-Names="arial" Font-Size="Small">
         <HeaderStyle BackColor="#cdcdcd">
         </HeaderStyle>
         <AlternatingItemStyle BackColor="LightGray">
         </AlternatingItemStyle>
         <HeaderTemplate>View
Your Details, Here </HeaderTemplate>
         <ItemTemplate>
            Name:  <%#Eval("Name")%>
            <br />
            Age: <%#Eval("Age")%>
            <br />
            Address : <%#Eval("Address")%>
            <hr />
         </ItemTemplate>
      </asp:DataList>
Here is ours .cs code:
On load event:
protected void Page_Load(object
sender, EventArgs e)
    {
        if
(!IsPostBack)
        {
            BindDataList();
        }
    }
protected void BindDataList()
    {
        //string
strPollQry = "SELECT * fROM pollOption";
        //SqlCommand
cmd = new SqlCommand(strPollQry);
        //cmd.Connection
= dbConn;
        //dbConn.Open();
        //SqlDataAdapter
da = new SqlDataAdapter(cmd);
        //DataTable
dt = new DataTable();
        //da.Fill(dt);
        // Here we
Are Declaring the Data Table and Data Row
        DataTable
dt = new DataTable();
        DataRow
dr = null;
        // Here we
Are Declaring the Column for Table and Data Type
        dt.Columns.Add("Name",
typeof(string));
        dt.Columns.Add("Age",
typeof(Int32));
        dt.Columns.Add("Address",
typeof(string));
        //Runing the
loop to add data into data row
        for (int i = 0; i < 7; i++)
        {
            string
strName = "ABC" + i;
            Int32
Age = 10 + i;
            string
strAddress = "XYZ" + i;
            dr = dt.NewRow();
            dr[0] = strName;
            dr[1] = Age;
            dr[2] = strAddress;
            dt.Rows.Add(dr);
            //Here we
are going to assign repeat columns as we wish (assuming two rows) DataList
should display
            if
(dt.Rows.Count > 0)
            {
                int
repeatColumns = 0;
                if
(dt.Rows.Count % 2 == 0)
                {
                    repeatColumns =
dt.Rows.Count / 2;
                }
                else
                {
                    repeatColumns =
(dt.Rows.Count / 2) + 1;
                }
                dlDynamic.RepeatColumns =
repeatColumns; // set the repeat columns by two
                dlDynamic.DataSource = dt;
                dlDynamic.DataBind();
            }
        }
    }
I hope you will enjoy.
Comments