Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datalist in which have a label that shows the datetime and have a grid inside the datalist .The problem is that i get the desire format of time (for e.g 1:48 AM) and data inside the grid only on odd no of record for example 1 3 5 7 records are correct and 2 4 6 7 show the empty record in the grid and the label also show the incorrect format(10/21/2015 1:48:20 AM). Here is the code. onitemDataBound event

C#
protected void dataListOrder_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if(e.Item.ItemType==ListItemType.Item)
            {
                Label date = e.Item.FindControl("lblOrderDate") as Label;
                string[] datesplit = date.Text.Split(' ');
                string time =string.Concat(datesplit[1]+" "+datesplit[2]);
                date.Text = time.ToString();

                int order_ID = Convert.ToInt32((e.Item.FindControl("orderid") as HiddenField).Value);
                GridView innerDataList = e.Item.FindControl("datalistOrderDetails") as GridView;
                string cs = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
                using (SqlConnection con = new SqlConnection(cs))
                {
                    DataTable orderDetailDT = new DataTable();
                    con.Open();
                    string orderDetail = "select ItemName,Quantity from Order_Details where Order_ID=" + order_ID + "";
                    SqlCommand cmd = new SqlCommand(orderDetail, con);
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter orderDetailDA = new SqlDataAdapter(cmd);
                    orderDetailDA.Fill(orderDetailDT);
                    innerDataList.DataSource = orderDetailDT;
                    innerDataList.DataBind();
                }
            }
        }

NOTE: I also try to use string format in aspx file to achieve my desired time format but it didn't work

ASP.NET
<asp:Label ID="lblOrderDate" runat="server" Text='<%#Eval("OrderDate","{0:t}") %>'.

Please guide me where the problem in the code why its only show data in odd no. Thankyou
Posted
Updated 20-Oct-15 11:17am
v2

1 solution

Change this line:
C#
if(e.Item.ItemType==ListItemType.Item)

to include
C#
e.Item.ItemType == ListItemType.AlternateItem
 
Share this answer
 
Comments
F.mas27 21-Oct-15 13:33pm    
THankyou i just change this line to
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
Sinisa Hajnal 22-Oct-15 2:19am    
Exactly. Good work!
F.mas27 22-Oct-15 7:23am    
Thankyou for the help sir.

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