Radgrid date format

4 posts, 0 answers
  1. Tal
    Tal avatar
    22 posts
    Member since:
    May 2013

    Posted 21 Aug 2013 Link to this post

    Hi dear support,

    I have a radgrid to which I query from my db with date conversion.
    SelectCommand="SELECT [eventID], [eventName], CONVERT(VARCHAR(10),[date],103) as date, CONVERT(VARCHAR(5),[hour],108) as hour FROM [tblevents]"

    When I make an event of "search_button"  I can reload the new datasource to the radgrid but I can't change the date format back.
    I tried to use the "DataFormatString" property with the value {0:DD/MM/YYYY} but with not luck.

    Will appreciate your help.

    Thanks,
    Tal

    <%@ Page Title="" Language="C#" MasterPageFile="~/northTheatre.Master" AutoEventWireup="true"
        CodeBehind="eventsList.aspx.cs" Inherits="RadControlsWebApp2.eventsList" Culture="auto"
        meta:resourcekey="PageResource1" UICulture="auto" %>
         
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" UpdateInitiatorPanelsOnly="True"
            meta:resourcekey="RadAjaxManager1Resource1">
        </telerik:RadAjaxManager>
        <p>
             </p>
        <asp:Panel ID="Panel2" runat="server" Height="91px" Style="margin-right: 243px; padding-right: 40px;"
            Width="750px" meta:resourcekey="Panel2Resource1">
             
            <div style="text-align:center">
            <table>
            <tr>
                <td>שם מופע
                    <telerik:RadTextBox ID="eventName_t" runat="server">
                    </telerik:RadTextBox> <br />
                    מתאריך
                    <telerik:RadDatePicker ID="fromDate" runat="server">
                    </telerik:RadDatePicker><br />
                    עד תאריך
                    <telerik:RadDatePicker ID="toDate" runat="server">
                    </telerik:RadDatePicker>
                    <telerik:RadButton ID="searchButton" runat="server" Text="חפש"
                        onclick="searchButton_Click" >
                    </telerik:RadButton>
                </td>
                <td>
                <telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticInserts="True" Culture="he"
                    AllowAutomaticUpdates="True" AllowFilteringByColumn="True" AllowPaging="True"
                    AllowSorting="True" CellSpacing="0" DataSourceID="SqlDataSource1" GridLines="None"
                     meta:resourceKey="RadGrid1Resource1"
                    FilterMenu-RegisterWithScriptManager="True" >
                    <SortingSettings SortToolTip="לחץ כאן כדי למיין" />
                    <ExportSettings>
                        <Pdf PageHeight="297mm" PageWidth="210mm" PaperSize="A4" />
                    </ExportSettings>
                    <ClientSettings>
                        <Selecting AllowRowSelect="True" />
                    </ClientSettings>
                    <MasterTableView AutoGenerateColumns="False" DataKeyNames="eventID" DataSourceID="SqlDataSource1"
                        AllowAutomaticDeletes="True">
                        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                        <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                        </ExpandCollapseColumn>
                         
                        <Columns>
                         
                             <telerik:GridEditCommandColumn ButtonType="ImageButton" CancelText="ביטול" EditText="עריכה"
                                FilterControlAltText="Filter EditCommandColumn column" meta:resourceKey="GridEditCommandColumnResource1"
                                UpdateText="עדכון" EditImageUrl="Images/edit.png" HeaderStyle-BorderStyle=None UniqueName="aaa">
                            </telerik:GridEditCommandColumn>
                              <telerik:GridButtonColumn CommandName="Delete" ButtonType=ImageButton ImageUrl="Images/delete.gif" Display=true
                            ConfirmText="האם אתה בטוח שברצונך למחוק?"  HeaderStyle-BorderStyle=None></telerik:GridButtonColumn>
                          
                             
                            <telerik:GridBoundColumn DataField="eventID" DataType="System.Int32" FilterControlAltText="Filter eventID column"
                                HeaderText="מספר מופע" SortExpression="eventID" UniqueName="eventID" ReadOnly="True"
                                meta:resourceKey="GridBoundColumnResource1">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="eventName" FilterControlAltText="Filter eventName column"
                                HeaderText="שם מופע" SortExpression="eventName" UniqueName="eventName" meta:resourceKey="GridBoundColumnResource2">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="date" DataType="System.DateTime" FilterControlAltText="Filter date column"
                            DataFormatString="{0:dd/MM/yyyy}"
                                HeaderText="תאריך" SortExpression="date" UniqueName="date" meta:resourceKey="GridBoundColumnResource3">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="hour" DataType="System.TimeSpan" FilterControlAltText="Filter hour column"
                                HeaderText="שעה" SortExpression="hour" UniqueName="hour" meta:resourceKey="GridBoundColumnResource4">
                            </telerik:GridBoundColumn>
                        </Columns>
                        <EditFormSettings>
                            <EditColumn FilterControlAltText="Filter EditCommandColumn column" UniqueName="EditCommandColumn1">
                            </EditColumn>
                        </EditFormSettings>
                     <%--   <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>--%>
                    </MasterTableView>
                   <%-- <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>--%>
                    <FilterMenu EnableImageSprites="False">
                    </FilterMenu>
                </telerik:RadGrid>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:tm2013ConnectionString %>"
                    DeleteCommand="DELETE FROM [tblevents] where [eventID]=@eventID"
                    UpdateCommand="UPDATE [tblevents] SET [eventName]=@eventName,[date]=@date,[hour]=@hour WHERE [eventID]=@eventID"
                    SelectCommand="SELECT [eventID], [eventName], CONVERT(VARCHAR(10),[date],103) as date, CONVERT(VARCHAR(5),[hour],108) as hour FROM [tblevents]">
                    <DeleteParameters>
                        <asp:Parameter Name="eventID" Type="Int32"></asp:Parameter>
                    </DeleteParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="eventName" Type="String"></asp:Parameter>
                        <asp:Parameter Name="eventID" Type="Int32"></asp:Parameter>
                    </UpdateParameters>
                </asp:SqlDataSource>
                <br />
                </td>
                </tr>
                </table>
            </div>
            <telerik:RadButton ID="delButton" runat="server" OnClick="delButton_Click" Text="מחק רשומה נוכחית"
                 meta:resourceKey="delButtonResource1">
            </telerik:RadButton>
                  
            <telerik:RadButton ID="addNewEvent" runat="server" Text="הוסף מופע חדש" OnClick="addNewEvent_Click"
                meta:resourceKey="addNewEventResource1">
            </telerik:RadButton>
                   
            <telerik:RadButton ID="RadButton3" runat="server" Text="הצג מפת אולם"
                OnClick="RadButton3_Click" meta:resourceKey="RadButton3Resource1">
            </telerik:RadButton>
        </asp:Panel>
        </asp:Panel>
    </asp:Content>


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    namespace RadControlsWebApp2
    {
        public partial class eventsList : System.Web.UI.Page
        {
            public static Itheatre_Funcs tFuncs = new Theatre_Funcs();  // an instance to the interface with functions
     
            protected void Page_Load(object sender, EventArgs e)
            {
                addNewEvent.Attributes.Add("onclick", "popupWindow('AddEvents_new.aspx','Add new event')");
                for (int i = 0; i < RadGrid1.FilterMenu.Items.Count; i++)
                {
                    if (RadGrid1.FilterMenu.Items[i].Text == "EqualTo")
                    {
                        RadGrid1.FilterMenu.Items[i].Text = "שווה ל";
                    }
                }
            }
     
            protected void delButton_Click(object sender, EventArgs e)
            {
                int currentEventID = (int)RadGrid1.SelectedValue;
                tFuncs.removeEventFromSystem(currentEventID);
                RadGrid1.Rebind();
     
     
            }
     
            protected void addNewEvent_Click(object sender, EventArgs e)
            {
                addNewEvent.Attributes.Add("onclick", "popWin();return false;");
            }
     
            protected void RadButton3_Click(object sender, EventArgs e)
            {
                int currentEventID = (int)RadGrid1.SelectedValue;
                Response.Redirect("hallMapSpecific_new.aspx?eventID=" + currentEventID);
            }
     
            protected void RadGrid1_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
            {
                int currentEventID = (int)RadGrid1.SelectedValue; //event to update
                //      string eve = RadGrid1.MasterTableView.DataKeyValues[editeditems.
     
     
     
                //     var rows = RadGrid1.Rows;
                //    RadGrid1.
                //    int radGridClickedRowIndex = Convert.ToInt32(Request.Form["radGridClickedRowIndex"]);
                //int currentEventID= (int)RadGrid1.SelectedValue;
     
                //   int index = Convert.ToInt32(e.CommandArgument);
                //GridViewRow gvRow = (tblevent)RadGrid1.
                //   int eventName = RadGrid1.Columns.
                //   DataGridItem item = (DataGridItem)RadGrid1.MasterTableView.Items[radGridClickedRowIndex];
                // int eventID = gvRow.RowIndex;
     
     
                ////    tblevent event = tFuncs.getEventsTable().Where(x=>x.eventID =
                //   RadGrid1.MasterTableView.ClearEditItems();
                //    RadGrid1.Rebind();
     
     
            }
     
            protected void searchButton_Click(object sender, EventArgs e)
            {
                var events = tFuncs.GetAllEvents();
     
                IEnumerable<tblevent> resultsOfEventsSearch = null;
     
                string eventName;
                DateTime from=DateTime.MinValue;
                DateTime to = DateTime.MinValue;
     
     
                eventName = eventName_t.Text;
               
     
                if (fromDate.SelectedDate.HasValue)
                {
                    from = (DateTime)fromDate.SelectedDate.Value;
                }
     
                if (toDate.SelectedDate.HasValue)
                {
                    to = (DateTime)toDate.SelectedDate.Value;
                }
                RadGrid1.Rebind(); //refresh the grid
     
                if (!eventName.Equals("") && to != DateTime.MinValue && from != DateTime.MinValue)
                {
                    resultsOfEventsSearch = events.Where(x => x.eventName.Equals(eventName) && (x.date <= to && x.date >= from));
                }
                else if ((eventName.Equals("") && to != DateTime.MinValue && from != DateTime.MinValue))
                {
                    resultsOfEventsSearch = events.Where(x => x.date <= to && x.date >= from);
                }
                else if ((!eventName.Equals("") && to == DateTime.MinValue && from != DateTime.MinValue))
                {
                    resultsOfEventsSearch = events.Where(x => x.eventName.Equals(eventName) && x.date >= from);
                }
                else if ((eventName.Equals("") && to == DateTime.MinValue && from != DateTime.MinValue))
                {
                    resultsOfEventsSearch = events.Where(x => x.date >= from);
                }
                else if ((!eventName.Equals("") && to != DateTime.MinValue && from == DateTime.MinValue))
                {
                    resultsOfEventsSearch = events.Where(x => x.eventName.Equals(eventName) && x.date <= to);
                }
                else if ((eventName.Equals("") && to != DateTime.MinValue && from == DateTime.MinValue))
                {
                    resultsOfEventsSearch = events.Where(x => x.date <= to);
                }
                else if ((!eventName.Equals("") && to == DateTime.MinValue && from == DateTime.MinValue))
                {
                    resultsOfEventsSearch = events.Where(x => x.eventName.Equals(eventName));
                }
                else if ((eventName.Equals("") && to == DateTime.MinValue && from == DateTime.MinValue))
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "Error", "alert('יש למלא אחד מהשדות לפני החיפוש');", true);
                    RadGrid1.Rebind();
                    return;
                }
                 
     
                 
                RadGrid1.DataSource= resultsOfEventsSearch;
     
      
                RadGrid1.DataSourceID = String.Empty;
     
                RadGrid1.Rebind();
               
            }
     
     
        }
    }
  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 23 Aug 2013 Link to this post

    Hello Tal,

    How are you changing the DataFormatString? Modifying the property should do: just get hold of the column before rebinding the grid and set its DataFormatString property to the desired new value. I could not see in your code where you are attempting this.
     
    Regards,
    Tsvetoslav
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. Tal
    Tal avatar
    22 posts
    Member since:
    May 2013

    Posted 23 Aug 2013 Link to this post

    Hey,
    thanks for the answer.
    the property is in <telerik:RadGridID="RadGrid1" 
    I just didn't copied it when posted the question.

    How do I get a hold of the column before rebinding the grid ?

    Thank you,

    Tal

  4. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 28 Aug 2013 Link to this post

    Hi Tal,

    You have a GetColumnSafe method on the MaterTableView object. It accpets as paramter the unique name of the column.
     
    Regards,
    Tsvetoslav
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Back to Top