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

Setting default value on row creation

4 Answers 163 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 25 Jan 2010, 07:43 PM
I have a radgrid that displays a date from a record in a database along with the other information in that record.  What I want to do is automatically populate that date with the current date when a user enters a new row - AND I want that date to show up on the screen before they insert it.  I know how to populate the column at the time of insert, but what I would like is when a user presses "Add new record" that a Label shows in the Edit form with the current date.

Here is my code:

 

 

 <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"   
        DataSourceID="LogBookODS" GridLines="None" AllowFilteringByColumn="True"   
        AllowPaging="True" AllowSorting="True" AllowAutomaticDeletes="True"   
        AllowAutomaticInserts="True" AllowAutomaticUpdates="True"   
        Skin="Office2007">  
<MasterTableView DataKeyNames="LogBookID" DataSourceID="LogBookODS"   
            CommandItemDisplay="Top">  
<RowIndicatorColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</RowIndicatorColumn> 
 
<ExpandCollapseColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</ExpandCollapseColumn> 
    <Columns> 
        <telerik:GridBoundColumn DataField="LogBookID" DataType="System.Int32"   
            HeaderText="LogBookID" ReadOnly="True" SortExpression="LogBookID"   
            UniqueName="LogBookID" Visible="False">  
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="PatientID" DataType="System.Int32"   
            HeaderText="PatientID" SortExpression="PatientID" UniqueName="PatientID"   
            Visible="False" ReadOnly="True">  
        </telerik:GridBoundColumn> 
        <telerik:GridTemplateColumn DataField="LogEntryDate" DataType="System.DateTime"   
            HeaderText="Entry Date" SortExpression="LogEntryDate"   
            UniqueName="LogEntryDate">  
            <EditItemTemplate> 
            <asp:Label ID="CurrDateLbl" runat="server"></asp:Label> 
            <asp:TextBox ID="CurrDateTxtBx" runat="server" Visible="false" Text='<%# Bind("LogEntryDate") %>'></asp:TextBox> 
            </EditItemTemplate> 
            <ItemTemplate> 
                <asp:Label ID="LogEntryDateLabel" runat="server"   
                    Text='<%# Eval("LogEntryDate","{0:d}") %>'></asp:Label> 
            </ItemTemplate> 
        </telerik:GridTemplateColumn> 
          
        <telerik:GridBoundColumn DataField="LogDescription" HeaderText="Description "   
            MaxLength="100" UniqueName="LogDescription">  
        </telerik:GridBoundColumn> 
          
        <telerik:GridTemplateColumn DataField="LogEntry" HeaderText="Log Entry"   
            SortExpression="LogEntry" UniqueName="LogEntry" AllowFiltering="False">  
            <EditItemTemplate> 
                <br /> 
                <telerik:RadEditor ID="RadEditor1" Runat="server"   
                    Content='<%# Bind("LogEntry") %>'>  
                    <Content> 
</Content> 
                </telerik:RadEditor> 
            </EditItemTemplate> 
            <ItemTemplate> 
                
                <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Edit" ImageUrl="~/images/Edit.gif" ToolTip="Click to view the log entry" /> 
               
            </ItemTemplate> 
        </telerik:GridTemplateColumn> 
                   <telerik:GridButtonColumn ConfirmText="Delete this Entry?" ConfirmDialogType="RadWindow" 
                        ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete" 
                        UniqueName="DeleteColumn">  
                        <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> 
                    </telerik:GridButtonColumn> 
 
    </Columns> 
 
<EditFormSettings> 
<EditColumn UniqueName="EditCommandColumn1" ButtonType="ImageButton"></EditColumn> 
</EditFormSettings> 
</MasterTableView> 
    </telerik:RadGrid> 
   

 

What I would like is for  CurrDateLbl to have the current date in it when the user clicks "Add New Record"

What event do I put that in?  ItemCommand fires at the right time, but I don't seem to be able to reference CurrDateLbl in that event.

Eric

 

4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 26 Jan 2010, 07:21 PM
Hello Eric,

Please examine the "Setting predefined values for different column editors" section in the following help topic:
Inserting values in-place and EditForms

Let me know if you need more information.

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Pam
Top achievements
Rank 2
answered on 12 Sep 2012, 10:33 PM
I currently needed the function stated above, wherein there should be a default value whenever I click the add records. I tried to click the link but there was an error for that page. Is there any fix for this problem?thanks in advance
0
Princy
Top achievements
Rank 2
answered on 13 Sep 2012, 04:55 AM

Hi pam,

Please check the sample code snippet I  tried to insert Default value to a TextBox(BoundColumn) when 'Add New Record' is pressed and EditMode is InPlace.

ASPX:

<telerik:RadGrid AutoGenerateColumns="false" ID="RadGrid1" DataSourceID="SqlDataSource1"
 AllowFilteringByColumn="True" runat="server" EnableLinqExpressions="false">
    <MasterTableView EditMode="InPlace" CommandItemDisplay="Top">
            <Columns>
                    <telerik:GridBoundColumn  DataField="ShipName" HeaderText="ShipName" UniqueName="ShipName">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" UniqueName="OrderID">
                    </telerik:GridNumericColumn>
            </Columns>
    </MasterTableView>  
</telerik:RadGrid>

VB:

Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If (e.CommandName = RadGrid.InitInsertCommandName) Then
        e.Canceled = True
        Dim newValues As New System.Collections.Specialized.ListDictionary()
        newValues("ShipName") = "default ShipName"
        newValues("OrderID") = " default OrderID"
        e.Item.OwnerTableView.InsertItem(newValues)
    End If
End Sub

Thanks,
Princy.

0
Pam
Top achievements
Rank 2
answered on 14 Sep 2012, 05:05 PM
Hi Princy,

Thank you very much for the reply. At first I thought it is not working but when I added the handler it worked fine.  Thanks again.
Tags
Grid
Asked by
Eric
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Pam
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or