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

Automatically insert week of the year when date is entered in edit form

4 Answers 168 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 20 Mar 2014, 08:32 AM
Hi,
I have the following challenge and I do not know how to solve this.
When the user enter the date in a dateTime column "Date" in edit mode, I need the week number to be automatically inserted in a bound column and saved to the db.
The week number is calculated in a method from code behind:

private static int WeekOfYear(DateTime date)
    {
        var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date);
        return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    }

This is my actual grid:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" Culture="it-IT" DataSourceID="SqlDataSource1" PageSize="20" CellSpacing="-1" GridLines="Both">
                        <ExportSettings>
                            <Pdf PageHeight="297mm" PageWidth="" PaperSize="A4">
                            </Pdf>
                        </ExportSettings>
                        <ClientSettings>
                            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
                        </ClientSettings>
                        <MasterTableView CommandItemDisplay="Top" DataKeyNames="Id" DataSourceID="SqlDataSource1" Caption="Working Hours" >
                            <Columns>
                                <telerik:GridEditCommandColumn ButtonType="ImageButton">
                                    <HeaderStyle Width="25px" />
                                </telerik:GridEditCommandColumn>
                                <telerik:GridBoundColumn DataField="Id" DataType="System.Int32" FilterControlAltText="Filter Id column" HeaderText="Id" ReadOnly="True" SortExpression="Id" UniqueName="Id">
                                    <ColumnValidationSettings>
                                        <ModelErrorMessage Text="" />
                                    </ColumnValidationSettings>
                                    <HeaderStyle HorizontalAlign="Left" Width="35px" />
                                </telerik:GridBoundColumn>
                                <telerik:GridDateTimeColumn DataField="Date" DataFormatString="{0:dd/MM/yyyy}" FilterControlAltText="Filter Date column" HeaderText="Date" SortExpression="Date" UniqueName="Date">
                                    <ColumnValidationSettings>
                                        <ModelErrorMessage Text="" />
                                    </ColumnValidationSettings>
                                    <HeaderStyle HorizontalAlign="Center" Width="85px" />
                                </telerik:GridDateTimeColumn>
 
                                <telerik:GridTemplateColumn DataField="Pnum" FilterControlAltText="Filter Pnum column" HeaderText="P Number" SortExpression="Pnum" UniqueName="Pnum">
                                    <EditItemTemplate>
                                        <telerik:RadComboBox ID="PnumRadComboBox" runat="server" DataSourceID="SqlDataSource2" DataTextField="Pnum" DataValueField="Pnum" SelectedValue='<%# Bind("Pnum") %>'>
                                        </telerik:RadComboBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="PnumLabel" runat="server" Text='<%# Eval("Pnum") %>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" Width="65px" />
                                </telerik:GridTemplateColumn>
 
                                <telerik:GridDateTimeColumn DataField="WorkedTime" FilterControlAltText="Filter column column" PickerType="TimePicker" DataFormatString="{0:hh\:mm}" HeaderText="Worked Time" SortExpression="WorkedTime" UniqueName="column">
                                    <ColumnValidationSettings>
                                        <ModelErrorMessage Text="" />
                                    </ColumnValidationSettings>
                                    <HeaderStyle HorizontalAlign="Center" Width="65px" />
                                </telerik:GridDateTimeColumn>
 
                                <telerik:GridBoundColumn DataField="Note" FilterControlAltText="Filter Note column" HeaderText="Note" SortExpression="Note" UniqueName="Note">
                                    <ColumnValidationSettings>
                                        <ModelErrorMessage Text="" />
                                    </ColumnValidationSettings>
                                    <HeaderStyle HorizontalAlign="Center" />
                                </telerik:GridBoundColumn>
                                <telerik:GridClientDeleteColumn ButtonType="ImageButton" FilterControlAltText="Filter column1 column" UniqueName="column1">
                                    <HeaderStyle Width="25px" />
                                </telerik:GridClientDeleteColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>
I would appreciate your help to solve this.
Thanks you,
Felice

4 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 20 Mar 2014, 10:15 AM
Hi Felice,

Please try the following c# code snippet to achieve your scenario.

C#:
void picker_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
{
    //selected date
    DateTime date = (DateTime)e.NewDate;
    int number = GetWeekNumber(date);
    RadDatePicker picker = (RadDatePicker)sender;
    //accessing the bound colum to update with week number
    GridEditableItem item=(GridEditableItem)picker.NamingContainer;
    TextBox text = (TextBox)item["uniquename"].Controls[0];
    text.Text = number.ToString();
}
public int GetWeekNumber(DateTime dtDate)
{
    //return weeknumber of selected date
    CultureInfo ciCurr = CultureInfo.CurrentCulture;
    int weekNum = ciCurr.Calendar.GetWeekOfYear(dtDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    return weekNum;
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        RadDatePicker picker = (RadDatePicker)item["OrderDate"].Controls[0];
        picker.AutoPostBack = true;
        picker.SelectedDateChanged += new Telerik.Web.UI.Calendar.SelectedDateChangedEventHandler(picker_SelectedDateChanged);
    }
}

Thanks,
Shinu.
0
Felice
Top achievements
Rank 1
answered on 20 Mar 2014, 02:33 PM
Thank you Shinu. I will make some change to adapt it to my scenario but it is grate.
Thanks
Felice
0
Felice
Top achievements
Rank 1
answered on 14 Apr 2014, 10:48 AM
Hi Shinu,
sorry, it took me some time to get back on this one.
I implemented the snippet you suggested and I get an error on this line:

TextBox text = (TextBox)item["week"].Controls[0];

the error says:" Item in insert mode does implement indexer only when the edit form is autogenerated"

My grid is like this (I am pasting only the relevant columns)
<Columns>...
 
<telerik:GridBoundColumn DataField="Date" DataFormatString="{0:dd/MM/yyyy}" FilterControlAltText="Filter column6 column"
                                     HeaderText="Date" UniqueName="Date_column">
                                    <ColumnValidationSettings>
                                        <ModelErrorMessage Text="" />
                                    </ColumnValidationSettings>
                                    <HeaderStyle Width="80px" />
<telerik:GridBoundColumn DataField="Date" DataFormatString="{0:dd/MM/yyyy}" FilterControlAltText="Filter column6 column"
                                     HeaderText="Date" UniqueName="Date_column">
                                    <ColumnValidationSettings>
                                        <ModelErrorMessage Text="" />
                                    </ColumnValidationSettings>
                                    <HeaderStyle Width="80px" />
                                </telerik:GridBoundColumn>
.....
</Columns>
 
<EditFormSettings EditFormType="Template">
<FormTemplate>
.....
 <telerik:RadDatePicker ID="RadDatePicker1" runat="server" OnSelectedDateChanged="RadDatePicker1_SelectedDateChanged" MinDate="1/1/2010" DbSelectedDate='<%# Bind("Date") %>'>
                                                </telerik:RadDatePicker>
 </FormTemplate>
</EditFormSettings>

and what I am trying to achieve here is to get the week of the year saved to the db on insert of the new record and show it in the relevant column week.
The snippet you sent me has been changed as following toreflect the name of the columns and controls in this scenario:

protected void RadDatePicker1_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
   {
       //selected date
       DateTime date = (DateTime)e.NewDate;
       int number = GetWeekNumber(date);
       RadDatePicker picker = (RadDatePicker)sender;
       //accessing the bound colum to update with week number
       GridEditableItem item = (GridEditableItem)picker.NamingContainer;
       TextBox text = (TextBox)item["week"].Controls[0];
       text.Text = number.ToString();
   }
   public int GetWeekNumber(DateTime dtDate)
   {
       //return weeknumber of selected date
       CultureInfo ciCurr = CultureInfo.CurrentCulture;
       int weekNum = ciCurr.Calendar.GetWeekOfYear(dtDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
       return weekNum;
   }
   protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
           GridEditableItem item = (GridEditableItem)e.Item;
           RadDatePicker picker = (RadDatePicker)item["Date"].Controls[0];
           picker.AutoPostBack = true;
           picker.SelectedDateChanged += new Telerik.Web.UI.Calendar.SelectedDateChangedEventHandler(RadDatePicker1_SelectedDateChanged);
       }
   }
I am not coming right with this and I would really appreciate your support to fix it.
Thank you a lot,
Felice
0
Konstantin Dikov
Telerik team
answered on 16 Apr 2014, 06:31 AM
Hello Felice,

The error that you are receiving is due to the fact that you are using FormTemplate for editing/inserting and you could not get reference to the control in the FormTemplate in such manner. Instead, the FindControl() method should be used:
TextBox text = item.FindControl("WeekOfYear") as TextBox;
*Please note that WeekOfYear is the ID of the TextBox control

For your convenience I am attaching a sample page with similar to your scenario.

Hope that helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Felice
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Felice
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or