Search code examples
c#linqtelerik-grid

Updating Telerik Rad Grid?


So, I have a RadGrid generated automatically, populated through LINQ. The Update command does nothing. Cancel works but when I click Edit, the edit column opens but the changes don't "take". What am I doing wrong? Other RadGrids on the same page seem to work fine.

Here's the grid:

  <telerik:RadGrid ID="DailyHoursGrid" runat="server" 
                    AutoGenerateEditColumn="True" CellSpacing="0" DataSourceID="LinqDataSource3" 
                    GridLines="None">                       
                    <ClientSettings>
                        <Selecting CellSelectionMode="None" />
                    </ClientSettings>
                    <MasterTableView AutoGenerateColumns="False" DataSourceID="LinqDataSource3">
                        <CommandItemSettings ExportToPdfText="Export to PDF" />
                        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" 
                            Visible="True">
                            <HeaderStyle Width="20px" />
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" 
                            Visible="True">
                            <HeaderStyle Width="20px" />
                        </ExpandCollapseColumn>
                        <Columns>
                            <telerik:GridBoundColumn DataField="Employee" DataType="System.Int32" 
                                FilterControlAltText="Filter Employee column" HeaderText="Employee" 
                                ReadOnly="True" SortExpression="Employee" UniqueName="Employee">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="EventType" 
                                FilterControlAltText="Filter EventType column" HeaderText="EventType" 
                                ReadOnly="True" SortExpression="EventType" UniqueName="EventType">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Time" DataType="System.DateTime" 
                                FilterControlAltText="Filter Time column" HeaderText="Time" ReadOnly="False" 
                                SortExpression="Time" UniqueName="Time">
                            </telerik:GridBoundColumn>
                        </Columns>
                        <EditFormSettings>
                            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                            </EditColumn>
                        </EditFormSettings>
                    </MasterTableView>
                    <FilterMenu EnableImageSprites="False">
                    </FilterMenu>

                </telerik:RadGrid>

And the LINQ:

  <asp:LinqDataSource ID="LinqDataSource3" runat="server" 
                    ContextTypeName="TimeClock.TimeClockEntities2" EntityTypeName="" OrderBy="Time" 
                    Select="new (Employee, EventType, Time)" TableName="Events" 


                    Where="Time &gt;= @Time &amp;&amp; Time &lt;= @Time1 &amp;&amp; Employee == @Employee" 
                    EnableUpdate="True" EnableInsert="True">
                    <WhereParameters>
                        <asp:ControlParameter ControlID="StartDatePicker" DefaultValue="0:00" Name="Time" 
                            PropertyName="SelectedDate" Type="DateTime" />
                        <asp:ControlParameter ControlID="EndDatePicker" DefaultValue="0:00" Name="Time1" 
                            PropertyName="SelectedDate" Type="DateTime" />
                        <asp:ControlParameter ControlID="HourlyReportEmployeeCombo" DefaultValue="0" 
                            Name="Employee" PropertyName="SelectedValue" Type="Int32" />
                    </WhereParameters>
                </asp:LinqDataSource>

Solution

  • RadGrid provides 2 types of CRUD operations: Automatic and Manual.

    For Automatic you can enable these grid properties targeting their own operation, respectively: AllowAutomaticUpdates, AllowAutomaticInserts, AllowAutomaticDeletes

    The other requirement is that the LinqDataSource should contain corresponding parameters to allow the grid to recognize the values which will be passed to the database:

    <asp:LinqDataSource ID="LinqDataSource1" runat="server">
      <UpdateParameters>
        ...
      </UpdateParameters>
    </asp:LinqDataSource>
    

    You can find a helpful similar sample with SqlDataSource provided in the first code-snippet of this article: Automatic DataSource Operations

    For more control, complex cases or StoredProcedures, it is a better idea to use Manual CRUD operations making avail of the Update-/Insert-/DeleteCommand or ItemCommand event handlers provided by the grid.