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

Dynamic GridEditCommandColumn : how to ?

4 Answers 76 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RadTony
Top achievements
Rank 1
RadTony asked on 31 Mar 2010, 10:48 AM
I have added GridBoundColumns to MasterTableView dynamically.

for (int i = 0; i < dtTable2.Columns.Count - 2; i++)  
{  
       GridBoundColumn boundColumnOthers = new GridBoundColumn();  
       boundColumnOthers.DataField = dtTable2.Columns[i].ColumnName;  
       RadGrid2.MasterTableView.Columns.Add(boundColumnOthers);  
}  
 
GridEditCommandColumn editColumn = new GridEditCommandColumn();         RadGrid2.MasterTableView.Columns.Add(editColumn); 

How to link programmatically the GridEditColumn ? What events must be generated ?
Can you give me a sample with Northwind database, in which we add "GridBoundColumns" dynamically and then make them edit programmatically ?

thanks,
RadTony

4 Answers, 1 is accepted

Sort by
0
RadTony
Top achievements
Rank 1
answered on 31 Mar 2010, 01:23 PM

Experts,

      I searched for a solution in all the forum and seems as if we can't update dynamically generated columns ...
      Please help. This must be a simple thing to do ...
    
      I'm unable to update a Radgrid by attaching updateEvent : grid.UpdateCommand += new GridCommandEventHandler(grid_UpdateCommand);

 

 

      Otherwise, with a normal RadGrid, I can access "grid_UpdateCommand" event ...

 

      

<%@ 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();  
    RadGrid grid = new RadGrid();  
 
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
    }  
 
    protected void Page_Init(object sender, System.EventArgs e)  
    {  
        PopulateGridOnPageInit();   
          
        if (grid != null)  
            grid.UpdateCommand += new GridCommandEventHandler(grid_UpdateCommand);  
        
    }  
 
    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);  
        grid.NeedDataSource +=new GridNeedDataSourceEventHandler(grid_NeedDataSource);  
 
        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_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
    {  
        grid.DataSource = LoadData();  
    }  
 
    protected void grid_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
        //Get the GridEditableItem of the RadGrid         
        GridEditableItem eeditedItem = 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;";  
    }    
   

     

 

 

     

0
Rosen
Telerik team
answered on 01 Apr 2010, 06:49 AM
Hi,

You should not populate  the control on every postback. Therefore can you please remove the DataSource assignment and call to DataBind from within the PopulateGridOnPageInit method.

All the best,
Rosen
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.
0
RadTony
Top achievements
Rank 1
answered on 01 Apr 2010, 09:48 AM
Hi Rosen,
 
      Can you modify the sample code I pasted above and make it fire the Update event ?
      I actually done it with a gridview using this link : 
      http://aspalliance.com/1125_Dynamically_Templated_GridView_with_Edit_Delete_and_Insert_Options.all
      I'll try to adapt it to a RadGrid ...

cheers,
RadTony
0
Rosen
Telerik team
answered on 01 Apr 2010, 04:06 PM
Tony,

Please find attached a modified version of the code you have pasted. Note that you should watch out for the usage of the member variable grid as it seems to be error prone.

Best wishes,
Rosen
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
RadTony
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or