Copying the row details into an Insert form

Thread is closed for posting
1 posts, 0 answers
  1. E69AACB4-3C5B-42BC-9BDD-D0F081D8FE34
    E69AACB4-3C5B-42BC-9BDD-D0F081D8FE34 avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Sep 2007 Link to this post

    Requirements

    RadControls version

    RadGrid 5.0.0

    .NET version

    2.0

    Visual Studio version

    2005

    programming language

    C#,VB

    browser support

    all browsers supported by RadControls


     
    PROJECT DESCRIPTION
    This  sample project explains how to provide option for copying the row details into an Insert form and also implements insert/update/delete operations  with sql statements from the code-behind. It uses a button column, containing a Copy button, which copies the content of the row, opens the Insert form, and prepopulates it with the values from the original row. The relevant code is posted below, and a working project is attached to the thread:

    ASPX:
     <radG:RadGrid ID="RadGrid1" runat="server" Skin="Ice" GridLines="None" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
                Width="97%" OnNeedDataSource="RadGrid1_NeedDataSource" OnDeleteCommand="RadGrid1_DeleteCommand" OnInsertCommand="RadGrid1_InsertCommand" OnUpdateCommand="RadGrid1_UpdateCommand" EnableAJAX="True" OnItemCommand="RadGrid1_ItemCommand" > 
                <ExportSettings> 
                    <Pdf PageBottomMargin="" PageFooterMargin="" PageHeaderMargin="" PageHeight="11in" 
                        PageLeftMargin="" PageRightMargin="" PageTopMargin="" PageWidth="8.5in" /> 
                </ExportSettings> 
                 <MasterTableView DataKeyNames="EmployeeID" GridLines="None" Width="100%" CommandItemDisplay ="Top" > 
                  
                    <Columns> 
                        <radG:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="Delete">  
                        </radG:GridButtonColumn> 
                        <radG:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID" ReadOnly="True">  
                        </radG:GridBoundColumn> 
                        <radG:GridBoundColumn DataField="LastName" HeaderText="LastName" UniqueName="LastName">  
                        </radG:GridBoundColumn> 
                        <radG:GridBoundColumn DataField="FirstName" HeaderText="FirstName" UniqueName="FirstName">  
                        </radG:GridBoundColumn> 
                        <radG:GridBoundColumn DataField="Title" HeaderText="Title" UniqueName="Title">  
                        </radG:GridBoundColumn> 
                        <radG:GridBoundColumn DataField="Address" HeaderText="Address" UniqueName="Address">  
                        </radG:GridBoundColumn> 
                        <radG:GridBoundColumn DataField="City" HeaderText="City" UniqueName="City">  
                        </radG:GridBoundColumn> 
                        <radG:GridEditCommandColumn> 
                        </radG:GridEditCommandColumn> 
                        <radG:GridButtonColumn ButtonType="PushButton" CommandName="CopyToInsertForm" Text="Copy To InsertForm" 
                            UniqueName="column">  
                        </radG:GridButtonColumn> 
                    </Columns> 
                    <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit details for employee with ID {0}" 
                        CaptionDataField="EmployeeID">  
                        <FormTableItemStyle Wrap="False"></FormTableItemStyle> 
                        <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle> 
                        <FormMainTableStyle CellSpacing="0" CellPadding="3" Width="100%" /> 
                        <FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module" 
                            Height="110px" Width="100%" /> 
                        <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> 
                        <FormStyle Width="100%" BackColor="#EEF2EA"></FormStyle> 
                        <EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit">  
                        </EditColumn> 
                        <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle> 
                    </EditFormSettings> 
                    <ExpandCollapseColumn Visible="False">  
                        <HeaderStyle Width="19px"></HeaderStyle> 
                    </ExpandCollapseColumn> 
                    <RowIndicatorColumn Visible="False">  
                        <HeaderStyle Width="20px" /> 
                    </RowIndicatorColumn> 
                </MasterTableView> 
            </radG:RadGrid> 

    CS:
    using System;  
    using System.Data;  
    using System.Configuration;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using System.Web.UI.HtmlControls;  
    using System.Data.SqlClient;  
    using Telerik.WebControls;  
    using System.Configuration;  
     
    public partial class Default2 : System.Web.UI.Page  
    {  
        //Declare a global DataTable dtTable    
        public static DataTable dtTable;  
        //Declare a global SqlConnection SqlConnection    
        public static string connectionString = ConfigurationManager.AppSettings["ConnectionString"];  
        public SqlConnection SqlConnection = new SqlConnection(connectionString);  
        //Declare a global SqlDataAdapter SqlDataAdapter    
        public SqlDataAdapter SqlDataAdapter = new SqlDataAdapter();  
        //Declare a global SqlCommand SqlCommand    
        public SqlCommand SqlCommand = new SqlCommand();  
     
        protected void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)  
        {  
            //Populate the Radgrid    
            dtTable = new DataTable();  
            //Open the SqlConnection    
            SqlConnection.Open();  
            try  
            {  
                //Select Query to populate the RadGrid with data from table Customers.    
                string selectQuery = "SELECT * FROM Employees";  
                SqlDataAdapter.SelectCommand = new SqlCommand(selectQuery, SqlConnection);  
                SqlDataAdapter.Fill(dtTable);  
                RadGrid1.DataSource = dtTable;  
            }  
            finally  
            {  
                //Close the SqlConnection    
                SqlConnection.Close();  
            }  
     
        }  
        protected void RadGrid1_DeleteCommand(object source, Telerik.WebControls.GridCommandEventArgs e)  
        {  
            //Get the GridDataItem of the RadGrid    
            GridDataItem item = (GridDataItem)e.Item;  
            //Get the primary key value using the DataKeyValue.    
            string EmployeeID = item.OwnerTableView.DataKeyValues[item.ItemIndex]["EmployeeID"].ToString();  
            try  
            {  
                //Open the SqlConnection    
                SqlConnection.Open();  
                string deleteQuery = "DELETE from Employees where EmployeeID='" + EmployeeID + "'";  
                SqlCommand.CommandText = deleteQuery;  
                SqlCommand.Connection = SqlConnection;  
                SqlCommand.ExecuteNonQuery();  
                //Close the SqlConnection    
                SqlConnection.Close();  
     
            }  
            catch (Exception ex)  
            {  
                RadGrid1.Controls.Add(new LiteralControl("Unable to delete Customers. Reason: " + ex.Message));  
                e.Canceled = true;  
            }  
     
        }  
        protected void RadGrid1_UpdateCommand(object source, Telerik.WebControls.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;  
            string Title = (editedItem["Title"].Controls[0] as TextBox).Text;  
            string Address = (editedItem["Address"].Controls[0] as TextBox).Text;  
            string City = (editedItem["City"].Controls[0] as TextBox).Text;  
     
            try  
            {  
                //Open the SqlConnection    
                SqlConnection.Open();  
                //Update Query to update the Datatable     
                string updateQuery = "UPDATE Employees set LastName='" + LastName + "',FirstName='" + FirstName + "',Title='" + Title + "',Address='" + Address + "',City='" + City + "' where EmployeeID='" + EmployeeID + "'";  
                SqlCommand.CommandText = updateQuery;  
                SqlCommand.Connection = SqlConnection;  
                SqlCommand.ExecuteNonQuery();  
                //Close the SqlConnection    
                SqlConnection.Close();  
     
     
            }  
            catch (Exception ex)  
            {  
                RadGrid1.Controls.Add(new LiteralControl("Unable to update Customers. Reason: " + ex.Message));  
                e.Canceled = true;  
            }  
     
        }  
        protected void RadGrid1_InsertCommand(object source, Telerik.WebControls.GridCommandEventArgs e)  
        {  
            //Get the GridEditFormInsertItem of the RadGrid    
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;  
     
            //string EmployeeID = (insertedItem["EmployeeID"].Controls[0] as TextBox).Text;  
     
            string LastName = (insertedItem["LastName"].Controls[0] as TextBox).Text;  
            string FirstName = (insertedItem["FirstName"].Controls[0] as TextBox).Text;  
            string Title = (insertedItem["Title"].Controls[0] as TextBox).Text;  
            string Address = (insertedItem["Address"].Controls[0] as TextBox).Text;  
            string City = (insertedItem["City"].Controls[0] as TextBox).Text;  
     
            try  
            {  
                //Open the SqlConnection    
                SqlConnection.Open();  
                //Update Query to insert into  the database     
                string insertQuery = "INSERT into  Employees(LastName,FirstName,Title,Address,City) values('" + LastName + "','" + FirstName + "','" + Title + "','" + Address + "','" + City + "')";  
                SqlCommand.CommandText = insertQuery;  
                SqlCommand.Connection = SqlConnection;  
                SqlCommand.ExecuteNonQuery();  
                //Close the SqlConnection    
                SqlConnection.Close();  
     
     
            }  
            catch (Exception ex)  
            {  
                RadGrid1.Controls.Add(new LiteralControl("Unable to insert Customers. Reason: " + ex.Message));  
                e.Canceled = true;  
            }  
     
        }  
     
     
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)  
        {  
            if (e.CommandName == "CopyToInsertForm")  
            {  
                GridDataItem item = (GridDataItem)e.Item;  
                //Put the RadGrid in the insert mode   
                RadGrid1.MasterTableView.IsItemInserted = true;  
                RadGrid1.Rebind();  
                //Get the inserformItem using the GetInsertItem Method  
                GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)RadGrid1.MasterTableView.GetInsertItem();  
                //Finding the textbox in the insert form and setting it's value  
                (insertedItem["FirstName"].Controls[0] as TextBox).Text = item["FirstName"].Text;  
                (insertedItem["LastName"].Controls[0] as TextBox).Text = item["LastName"].Text;  
                (insertedItem["Title"].Controls[0] as TextBox).Text = item["Title"].Text;  
                (insertedItem["Address"].Controls[0] as TextBox).Text = item["Address"].Text;  
                (insertedItem["City"].Controls[0] as TextBox).Text = item["City"].Text;  
            }  
     
        }  
    }  
     
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.