This is a migrated thread and some comments may be shown as answers.

Item.Text is empty in ItemDataBound

3 Answers 188 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Asti
Top achievements
Rank 1
Asti asked on 14 Aug 2008, 09:09 AM
   public partial class _Default : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!Page.IsPostBack) 
            { 
                Calendar.SelectedDate = DateTime.Now; 
              
            } 
 
 
        } 
 
        private void GetDays() 
        { 
             
        } 
 
        private void CreateSelectedCollection(Telerik.Web.UI.Calendar.Collections.DateTimeCollection coll, bool rebindgrid) 
        { 
 
            Dictionary<DateTime, Entry> dict = new Dictionary<DateTime, Entry>(); 
 
            StringBuilder b = new StringBuilder(); 
            for (int i = 0; i < coll.Count; i++) 
            { 
                dict.Add(coll[i].Date, new Entry(coll[i].Date, true, -1)); 
 
                b.Append("'"); 
                b.Append(coll[i].Date.ToString("yyyy-MM-dd")); 
                b.Append("'"); 
                if (i != coll.Count - 1) 
                    b.Append(","); 
            } 
            checkdates(b.ToString(), dict, rebindgrid); 
        } 
 
        private void checkdates(string p, Dictionary<DateTime, Entry> dict, bool rebindgrid) 
        { 
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TimesheetConnectionString"].ConnectionString)) 
            { 
                conn.Open(); 
                SqlCommand comm = new SqlCommand("SELECT * FROM tblEntries WHERE Date IN( " + p + " )", conn); 
                SqlDataReader reader = comm.ExecuteReader(); 
 
                while (reader.Read()) 
                { 
                    if (dict.ContainsKey((DateTime)reader["Date"])) 
                    { 
                        dict[(DateTime)reader["Date"]].From = (DateTime)reader["From_"]; 
                        dict[(DateTime)reader["Date"]].To = (DateTime)reader["To_"]; 
                        dict[(DateTime)reader["Date"]].IsNewRecord = false
                        dict[(DateTime)reader["Date"]].ID = int.Parse(reader["ID"].ToString()); 
                    } 
 
                } 
            } 
            grid.DataSource = dict.Values; 
 
            if (rebindgrid) 
                grid.Rebind(); 
        } 
 
        protected void update(object source, Telerik.Web.UI.GridCommandEventArgs e) 
        {  
            Hashtable t = new Hashtable(); 
 
            GridEditableItem editedItem = e.Item as GridEditableItem; 
 
            e.Item.OwnerTableView.ExtractValuesFromItem(t, editedItem); 
 
 
        } 
 
 
        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
        { 
            if (e.CommandName == "UpdateAll"
            { 
                foreach (GridEditableItem editedItem in grid.EditItems) 
                { 
                    Hashtable newValues = new Hashtable(); 
 
                    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem); 
 
                    updateorinsert((string)newValues["To"], (string)newValues["From"], (bool)newValues["IsNewRecord"], (string)newValues["Date"], (string)newValues["ID"]); 
 
                    editedItem.Edit = false
                } 
            } 
            grid.Rebind(); 
        } 
 
        private void updateorinsert(string to, string from, bool isnew, string date, string id) 
        { 
 
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TimesheetConnectionString"].ConnectionString)) 
            { 
 
                conn.Open(); 
 
 
                if ((isnew)) 
                { 
                    SqlCommand comm = new SqlCommand("INSERT INTO tblEntries (Date, To_, From_) VALUES (@Date, @To, @From)", conn); 
                    comm.Parameters.AddWithValue("@Date", DateTime.Parse(date).ToString("yyyy-MM-dd")); 
                    comm.Parameters.AddWithValue("@To", DateTime.Parse(to).ToString("yyyy-MM-dd HH:mm")); 
                    comm.Parameters.AddWithValue("@From", DateTime.Parse(from).ToString("yyyy-MM-dd HH:mm")); 
                    int result = comm.ExecuteNonQuery(); 
 
                } 
                else 
                { 
 
                    SqlCommand comm = new SqlCommand("UPDATE tblEntries SET Date = @Date, To_ = @To, From_ = @From WHERE ID = @Id", conn); 
 
                    comm.Parameters.AddWithValue("@Date", DateTime.Parse(date).ToString("yyyy-MM-dd")); 
                    comm.Parameters.AddWithValue("@To", DateTime.Parse(to).ToString("yyyy-MM-dd HH:mm")); 
                    comm.Parameters.AddWithValue("@From", DateTime.Parse(from).ToString("yyyy-MM-dd HH:mm")); 
                    comm.Parameters.AddWithValue("@Id"int.Parse(id)); 
 
                    int result = comm.ExecuteNonQuery(); 
 
                } 
 
                conn.Close(); 
            } 
        } 
 
 
        TimeSpan a; 
        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
        { 
 
            if (e.Item is GridDataItem) 
            { 
 
 
                GridDataItem dataItem = e.Item as GridDataItem; 
                TimeSpan tmp; 
                if (TimeSpan.TryParse(dataItem["Span"].Text, out tmp)) 
                    a += (tmp); 
            } 
            if (e.Item is GridFooterItem) 
            { 
                GridFooterItem footerItem = e.Item as GridFooterItem; 
                footerItem["Span"].Text = "total: " + a.Hours + " h" + a.Minutes + " m"
            } 
        } 
 
        protected void RadGrid1_PreRender(object sender, System.EventArgs e) 
        { 
            // Force all rows to be in edit mode (rebind is called)  
 
            foreach (GridItem item in grid.MasterTableView.Items) 
                { 
                    if (item is GridEditableItem) 
                    { 
                        GridEditableItem editableItem = item as GridDataItem; 
                        editableItem.Edit = true
                    } 
                } 
                grid.Rebind(); 
             
        } 
 
 
        /// <summary> 
        /// Needdatasource event handler 
        /// </summary> 
        /// <param name="source"></param> 
        /// <param name="e"></param> 
         
        protected void need(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
        { 
             
 
             CreateSelectedCollection(Calendar.SelectedDates, false); 
 
             
        } 
 
     
        /// <summary> 
        /// Dateselected event handler 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        protected void fire(object sender, SelectedDatesEventArgs e) 
        { 
             
            
             CreateSelectedCollection(e.SelectedDates, true); 
        } 
 
 
        //protected void save() 
        //{ 
        
 
        //} 
 
 
 
 
 
    } 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AJAXEnabledWebApplication1._Default" %> 
<%@ Register TagPrefix="telerik" Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
    <div> 
     
      <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true"
    
            <ContentTemplate> 
             
    <telerik:RadCalendar MultiViewColumns="6"  AutoPostBack="true" runat="server" ID="Calendar" UseColumnHeadersAsSelectors="false" UseRowHeadersAsSelectors="true" OnSelectionChanged="fire"    ShowRowHeaders="true" EnableMultiSelect="true"   /> 
     
    </ContentTemplate> 
    </asp:UpdatePanel> 
     
     
     
      
    
 
        <asp:UpdatePanel runat="server" ChildrenAsTriggers="true"
   
            <ContentTemplate> 
                <telerik:RadGrid  Skin="Vista" OnItemDataBound="RadGrid1_ItemDataBound" ShowFooter="true" AllowSorting="true" 
                    OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender" OnNeedDataSource="need" 
                      OnUpdateCommand="update" AutoGenerateColumns="false" runat="server" ID="grid" 
                    AllowMultiRowEdit="true"
                     
                     
                    <ExportSettings> 
                        <Excel FileExtension="xls" Format="ExcelML" /> 
                        <Csv FileExtension="txt" RowDelimiter="NewLine" ColumnDelimiter="semicolon" /> 
                        <Pdf Title="Timerapport" Producer="TimeSys" PaperSize="a4" PageTitle="TimeRapport" 
                            AllowPrinting="true" AllowCopy="true" AllowAdd="true" FontType="Embed" /> 
                    </ExportSettings> 
                    <MasterTableView EditMode="inplace" CommandItemDisplay="topAndBottom"
                        <Columns> 
                            
                            <telerik:GridDateTimeColumn DataField="Date" HeaderText="Dato" ReadOnly="false"
                            </telerik:GridDateTimeColumn> 
                            <telerik:GridDateTimeColumn DataField="From" HeaderText="Fra" PickerType="TimePicker"
                            </telerik:GridDateTimeColumn> 
                            <telerik:GridDateTimeColumn  DataField="To" HeaderText="Til" PickerType="TimePicker"
                            </telerik:GridDateTimeColumn> 
                            <telerik:GridCheckBoxColumn ReadOnly="false" DataField="IsNewRecord" Visible="false" /> 
                            <telerik:GridBoundColumn ReadOnly="false" DataField="ID" Visible="false" /> 
                            <telerik:GridBoundColumn ReadOnly="true" DataField="Span" UniqueName="Span"  Visible="true" HeaderText="Antall timer" /> 
                            <telerik:GridTemplateColumn DataField="TimeSp"
                                <ItemTemplate> 
                                    <asp:Literal Text='<%# Eval("TimeSp") %>' runat="server" /> 
                                </ItemTemplate> 
                                <FooterTemplate> 
                                </FooterTemplate> 
                            </telerik:GridTemplateColumn> 
                        </Columns> 
                        <CommandItemTemplate> 
                            <asp:Button runat="server" ID="UpdateAll" Text="Update 
All" CommandName="UpdateAll" />                 
                      
                        </CommandItemTemplate> 
                    </MasterTableView> 
     
    </telerik:RadGrid> 
    </ContentTemplate> 
     
    </asp:UpdatePanel> 
     
     
     
    </div> 
    </form> 
</body> 
</html> 
 

Hello, I'm creating a small timesheet application, it has a custom total row which sums up the rows in the grid. The problem is that even though the itemdatabound event is fired, the .Text property is empty so i end up getting the wrong totals.

3 Answers, 1 is accepted

Sort by
0
Missing User
answered on 15 Aug 2008, 02:04 PM
Hi Asti,

Take a look at these online resources to see how to render the sum in the column footer:




Sincerely yours,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Asti
Top achievements
Rank 1
answered on 16 Aug 2008, 04:13 PM
hello,


i tried following the code sample but things do not seem to work properly in my case, the itemdatabound event is raised but the items do not have values in the event hence making it impossible to calculate a total.

But the grid itself shows values properly.
0
Missing User
answered on 20 Aug 2008, 11:44 AM
Hi Asti,


Please find attached a sample web application that shows the needed approach.



Best wishes,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Asti
Top achievements
Rank 1
Answers by
Missing User
Asti
Top achievements
Rank 1
Share this question
or