This is a migrated thread and some comments may be shown as answers.
Is it possible to update a dynamic Grid ?
1 Answer 19 Views
This is a migrated thread and some comments may be shown as answers.
RadTony
Top achievements
Rank 1
RadTony asked on 31 Mar 2010, 02:07 PM
Telerik experts,

       Here is my code. I can't update my dynamic RadGrid.
       Otherwise no problem to update a normally generated RadGrid. 

       I must be missing something very basic ...

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Northwind2.aspx.cs" Inherits="Northwind2" %>    
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">    
    
<html xmlns="http://www.w3.org/1999/xhtml">     
<head runat="server">     
    <title>Northwind2</title>    
</head>    
<body>    
    <form id="form1" runat="server">     
          <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />    
        <!-- content start -->    
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">     
            <AjaxSettings>       
                <telerik:AjaxSetting AjaxControlID="RadGrid2">        
                </telerik:AjaxSetting>       
            </AjaxSettings>       
        </telerik:RadAjaxManager>    
             
    <div>    
             
    <br />    
    <asp:PlaceHolder ID="PlaceHolder1" runat="server" />    
    <br />    
            <telerik:RadGrid ID="RadGrid2" EnableViewState="true" AllowAutomaticUpdates="True" OnUpdateCommand="grid_UpdateCommand"       
                OnNeedDataSource="RadGrid2_NeedDataSource" Width="400px"    
                runat="server" AutoGenerateColumns="false" AllowSorting="False" AllowMultiRowSelection="False" GridLines="None">                                    
            <MasterTableView ItemStyle-Wrap="false" Name="Employees" ShowHeader="true" DataKeyNames="EmployeeID"      
                EditMode="InPlace" BorderWidth="0" EnableColumnsViewState="true" Width="400px">                          
                <HeaderStyle Wrap="false" Height="20px" Font-Size="X-Small" Font-Bold="true"/>                   
                <AlternatingItemStyle Height="20px" BackColor="BlanchedAlmond" Wrap="false" BorderWidth="0" Font-Size="X-Small"/>        
                <ItemStyle Height="20px" BackColor="Azure" Wrap="false" BorderWidth="0" Font-Size="X-Small"/>                
                <EditItemStyle Height="20px" Wrap="false" BorderWidth="0" Font-Size="X-Small"/>          
                <Columns>       
                      <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" ItemStyle-Height="20px" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="150" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>                      
                      <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName" ItemStyle-Height="20px" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="150" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>                      
                      <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName" ItemStyle-Height="20px" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="150" HeaderStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>                      
                      <telerik:GridEditCommandColumn ></telerik:GridEditCommandColumn>    
                </Columns>                                          
            </MasterTableView>       
            <clientsettings allowexpandcollapse="false">                       
                    <Scrolling AllowScroll="false" UseStaticHeaders="true"></Scrolling>                                                                                          
            </clientsettings>        
        </telerik:RadGrid>      
    </div>    
    </form>    
</body>    
</html>    
    
'-------------------------------------------------------------     
'-------------------------------------------------------------     
'-------------------------------------------------------------     
    
using System;     
    
using System.Collections;     
using System.Collections.Generic;     
using System.Linq;     
    
using System.Web;     
using System.Web.UI;     
using System.Web.UI.WebControls;     
using System.Web.UI.HtmlControls;     
    
using System.Data;     
using System.Data.Sql;     
using System.Data.SqlClient;     
    
using Telerik.Web.UI;     
using System.Reflection;     
    
public partial class Northwind2 : System.Web.UI.Page     
{       
    //Declare a global DataTable dtTable1            
    DataTable dtTable1 = new DataTable();     
 
    
    protected void Page_Load(object sender, EventArgs e)     
    {     
    
    }     
    
    protected void Page_Init(object sender, System.EventArgs e)     
    {     
        PopulateGridOnPageInit();     
           
    }     
    
    protected void PopulateGridOnPageInit()     
    {            
        RadGrid grid = new RadGrid();     
        grid.ID = "grid";     
        grid.Width = Unit.Pixel(400);     
        grid.AllowSorting = true;     
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;     
        grid.AllowPaging = true;     
        grid.AllowAutomaticUpdates = false;     
        grid.Skin = "Sunset";     
    
        grid.MasterTableView.AutoGenerateColumns = false;     
        grid.MasterTableView.EnableColumnsViewState = false;     
        grid.MasterTableView.EditMode = GridEditMode.InPlace;     
        grid.MasterTableView.EnableViewState=true ;     
    
        grid.UpdateCommand +=new GridCommandEventHandler(grid_UpdateCommand);      
    
        GridBoundColumn boundColumn;     
             
        boundColumn = new GridBoundColumn();     
        boundColumn.HeaderText = "EmployeeID";     
        boundColumn.DataField = "EmployeeID";     
        //boundColumn.UniqueName = "EmployeeID";     
        grid.MasterTableView.Columns.Add(boundColumn);     
        boundColumn = new GridBoundColumn();     
        boundColumn.HeaderText = "LastName";     
        boundColumn.DataField = "LastName";     
        //boundColumn.UniqueName = "LastName";     
        grid.MasterTableView.Columns.Add(boundColumn);     
        boundColumn = new GridBoundColumn();     
        boundColumn.HeaderText = "FirstName";     
        boundColumn.DataField = "FirstName";     
        //boundColumn.UniqueName = "FirstName";     
        grid.MasterTableView.Columns.Add(boundColumn);     
    
    
             
        GridEditCommandColumn gcEdit = new GridEditCommandColumn();     
        grid.MasterTableView.Columns.Add(gcEdit);     
    
        grid.DataSource = LoadData();     
        grid.DataBind();     
    
        PlaceHolder1.Controls.Add(grid);     
    }     
    
    protected void RadGrid2_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)     
    {     
        RadGrid2.DataSource = LoadData();     
    }     
    
 
    
    protected void grid_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)     
    {     
        //Get the GridEditableItem of the RadGrid            
        GridEditableItem eeeditedItem = e.Item as GridEditableItem;     
        //Get the primary key value using the DataKeyValue.            
        string EmployeeID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["EmployeeID"].ToString();     
        //Access the textbox from the edit form template and store the values in string variables.            
        string LastName = (editedItem["LastName"].Controls[0] as TextBox).Text;     
        string FirstName = (editedItem["FirstName"].Controls[0] as TextBox).Text;     
    
        try     
        {     
            SqlConnection SqlConnection = new SqlConnection(GetConnectionString());     
            SqlCommand SqlCommand1 = new SqlCommand();     
            //Open the SqlConnection            
            SqlConnection.Open();     
            //Update Query to update the Datatable             
            string updateQuery = " UPDATE Employees " +     
                                 " SET    LastName='" + LastName + "'," +     
                                 "        FirstName='" + FirstName + "'" +      
                                 " WHERE  EmployeeID=" + EmployeeID ;     
            SqlCommand1.CommandText = updateQuery;     
            SqlCommand1.Connection = SqlConnection;     
            SqlCommand1.ExecuteNonQuery();     
            //Close the SqlConnection            
            SqlConnection.Close();     
    
    
        }     
        catch (Exception ex)     
        {     
            RadGrid2.Controls.Add(new LiteralControl("Unable to update Employee. Reason: " + ex.Message));     
            e.Canceled = true;     
        }     
        finally     
        {     
            RadGrid2.EditIndexes.Clear();     
        }     
    }     
    
    
    protected DataTable LoadData()     
    {     
        string connectionString = GetConnectionString();     
        SqlConnection SqlConnection = new SqlConnection(connectionString);     
    
        DataTable dtTable = new DataTable();     
        //Declare a global SqlDataAdapter SqlDataAdapter1            
        SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter();     
        //Declare a global SqlCommand SqlCommand            
        SqlCommand SqlCommand1 = new SqlCommand();     
    
        //Open the SqlConnection            
        SqlConnection.Open();     
        try     
        {     
            //Select Query to populate the RadGrid with data from table Employees.            
            string selectQuery = "SELECT EmployeeID, LastName, FirstName, Title, ReportsTo from Employees";     
            SqlDataAdapter1.SelectCommand = new SqlCommand(selectQuery, SqlConnection);     
            SqlDataAdapter1.Fill(dtTable);     
            return dtTable;     
        }     
        finally     
        {     
            //Close the SqlConnection            
            SqlConnection.Close();     
        }      
    }     
       
    static private string GetConnectionString()     
    {     
        return "server=STATION00374;database=Northwind;Integrated Security=SSPI;";     
    }       
      
}    
 

1 Answer, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 02 Apr 2010, 01:23 PM
Hi Anthony,

Instead of binding the dynamic grid conditionally using simple binding with DataBind() calls, you should transform your code to take advantage of the advanced binding with NeedDataSource handling:
http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html

Please, give this suggestion a try and let me know about the result.

Kind regards,
Pavlina
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
RadTony
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Share this question
or