Multirowedit and LINQ save problem

2 posts, 0 answers
  1. Bruce
    Bruce avatar
    33 posts
    Member since:
    Oct 2006

    Posted 13 Sep 2009 Link to this post

    I'm trying to set up a grid that start in edit mode for all rows.  I'm also using LINQ.  What do I need to do to save the data?
    Does anyone have an example?

    Thanks!


    My code:
    <telerik:RadGrid ID="RdGrid" runat="server"
         DataSourceID="dsProducts"
         AllowFilteringByColumn="True"
         AllowPaging="True"
         AllowSorting="True"
         AllowMultiRowEdit = "True"
         AllowMultiRowSelection = "True"
         AllowAutomaticInserts="True"
         AllowAutomaticUpdates="True"
         AllowAutomaticDeletes="True"
         AutoGenerateColumns = "False"
         GridLines="None"
         PageSize="20"
         Skin="Office2007"
         ClientSettings-EnableDragToSelectRows="True"
         ClientSettings-Selecting-AllowRowSelect="True"
         PagerStyle-Mode = "NextPrevAndNumeric"
         PagerStyle-NextPageText ="Next"
         PagerStyle-PrevPageText ="Prev"
         OnItemCommand="RdGrid_ItemCommand"
         OnItemDataBound="RdGrid_ItemDataBound"
                OnItemUpdated="RdGrid_ItemUpdated"
               OnItemInserted="RdGrid_ItemInserted"
         OnItemDeleted="RdGrid_ItemDeleted"
      >
      <HeaderContextMenu>
        <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
      </HeaderContextMenu>
      <MasterTableView
         autogeneratecolumns="False"
         datakeynames="ProductID"
         CommandItemDisplay="Top"
         AllowMultiColumnSorting="True"
         EditMode="InPlace"
      >
        <RowIndicatorColumn>
         <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
        <ExpandCollapseColumn>
         <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <CommandItemTemplate >
          <div style="padding:10px 0px;">
                  <asp:LinkButton ID="lnkcmdEditAllRows"    runat="server"   CommandName="UpdateAll" ><img style="border:0px;vertical-align:middle;" alt="" src="Images/Update.gif" /> Save Changes</asp:LinkButton>
                  <asp:LinkButton ID="lnkcmdRefresh" runat="server" visible="true" CommandName="RebindGrid"><img style="border:0px;vertical-align:middle;" alt="" src="Images/Refresh.gif" /> Refresh grid</asp:LinkButton>
         </div>
       </CommandItemTemplate>
       <Columns>
          <telerik:GridBoundColumn
            DataField="ProductID"
            DataType="System.Int32"
            HeaderText="Product ID"
            SortExpression="ProductID"
            UniqueName="ProductID"
            ItemStyle-HorizontalAlign="Right"
            DataFormatString="{0:N0}"
          >
          </telerik:GridBoundColumn>
           <telerik:GridTemplateColumn
             HeaderText="Product Name"
             UniqueName="unqProductName"
           >
            <ItemTemplate>
             <asp:TextBox ID="txtProductName"  runat="server"
                  Columns="50"
                  MaxLength="80"
                  Text='<%# Bind("ProductName") %>'/>
            </ItemTemplate>
            <EditItemTemplate>
             <asp:TextBox ID="txtProductName2"  runat="server"
                  Columns="50"
                  MaxLength="80"
                  Text='<%# Bind("ProductName") %>'/>
            </EditItemTemplate>
          </telerik:GridTemplateColumn>
        </Columns>
       </MasterTableView>
       <FilterMenu>
         <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
       </FilterMenu>
    </telerik:RadGrid>
    <asp:LinqDataSource
        ID="dsProducts"
        runat="server"
        ContextTypeName="NorthwindDataContext"
        TableName="Products"
        OrderBy="ProductName"
        EnableDelete="True"
        EnableInsert="True"
        EnableUpdate="True"
        OnInserting="LinqDataSourceProducts_Inserting"
        OnUpdating="LinqDataSourceProducts_Updating"
        
     >
     </asp:LinqDataSource>

    public partial class Prod : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e) {
     }
     protected void RdGrid_ItemCommand(object source, GridCommandEventArgs e) {
         if (e.CommandName == "UpdateAll")
         {
             foreach (GridEditableItem editedItem in RdGrid.EditItems)
             {
                 //Example for SQLDataSource
                 // What's needed for a LINQ data source???
                 Hashtable newValues = new Hashtable();
                 //The GridTableView will fill the values from all editable columns in the hash  
                 e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
                // SqlDataSource1.UpdateCommand = String.Format("Update Customers SET ContactName='{0}' WHERE CustomerIDCustomerID='{1}'", newValues["ContactName"], editedItem["CustomerID"].Text);
                // SqlDataSource1.Update();
                 editedItem.Edit = false;
             }
         }
         RdGrid.Rebind();
     }
    protected void RdGrid_ItemDataBound(object source, GridItemEventArgs e)  {
    }
    protected void LinqDataSourceProducts_Inserting(object sender, LinqDataSourceInsertEventArgs e)
     {
    }
    protected void LinqDataSourceProducts_Updating(object sender, LinqDataSourceUpdateEventArgs e)
     {
    }
     protected void RdGrid_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
     {
     }
     protected void RdGrid_ItemInserted(object source, GridInsertedEventArgs e)
     {
     }
     protected void RdGrid_ItemDeleted(object source, GridDeletedEventArgs e)
     {
     }
     private void SetMessage(string message,string exceptionMessage, Boolean Append)
     {
     }
    }

  2. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 16 Sep 2009 Link to this post

    Hello Bruce,

    Since you enabled automatic operations for your grid and the LinqDataSource control, you do not need to implement manual updates/inserts/deletes from the code-behind (see this demo for details).

    In case you would like to use LINQ queries to perform editing operations, examine the code from this example.

    Best regards,
    Sebastian
    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top