get the values of grid bound column in insertcommand event

Posted by Community Admin on 04-Aug-2018 09:49

get the values of grid bound column in insertcommand event

All Replies

Posted by Community Admin on 17-Jun-2011 00:00

hello
     I am using telerik rad grid first time in my application there is one requirement that when click on add new record it should add new blank row in grid and when added values in row and hitting insert key  value should not insert into the real database first it should temporarily show in grid only after inserting all the records in grid and clicking save button  all the record should insert into the real data base
     please suggest me solution for that m unable to get the data from grid bound column of grid.

this is my cs file code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Data.SqlClient;

namespace MultiRecordEditinRadGrid

    public partial class _Default : System.Web.UI.Page
   


        SqlConnection Conn = new SqlConnection("Data Source=PEARL\\SQLEXPRESS;Initial Catalog=Project1;Integrated Security=True");
        DataTable dt;
        SqlCommand cmd;
        SqlDataAdapter sda;
        protected void Page_Load(object sender, EventArgs e)
       
            //if (Page.IsPostBack)
            //
            Conn.Open();
            String strsql1;
            strsql1 = "select * from TABLE_NAMES where 1=2";
            cmd = new SqlCommand(strsql1, Conn);
            sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
            RadGrid1.MasterTableView.DataSource = dt;
            RadGrid1.DataBind();
            Session.Add("dt", dt);
            Conn.Close();
            //

       
    

        protected void RadioCheckedChanged(object sender, System.EventArgs e)
       
            switch ((sender as RadioButton).ID)
           
                case "RadioButton1":
                   
                        RadGrid1.MasterTableView.EditMode = GridEditMode.EditForms;
                        RadioButton2.Checked = false;
                        break;
                   
                case "RadioButton2":
                   
                        RadGrid1.MasterTableView.EditMode = GridEditMode.InPlace;
                        RadioButton1.Checked = false;
                        break;
                   
           
            RadGrid1.Rebind();
       
        protected void CheckboxCheckedChanged(object sender, System.EventArgs e)
       

            RadGrid1.AllowMultiRowEdit = CheckBox1.Checked;

            RadGrid1.Rebind();
       

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
       
            DataTable dt = new DataTable();
            dt.Columns.Add("TABLE_ID");
            dt.Columns.Add("COLUMN_NAME");
            dt.Columns.Add("COLUMN_TYPE");
            dt.Columns.Add("DESCRIPTION");
            dt.Columns.Add("LIST_ID");
            if (ViewState["Table"] != null)
           
                dt = (DataTable)ViewState["Table"];
           
            RadGrid1.DataSource = dt;
            ViewState["Table"] = dt;

       


        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
       

            dt = (DataTable)ViewState["Table"];
            DataRow dr = dt.NewRow();
            GridEditableItem editItem = DirectCast[e.Item, GridEditableItem];
            TextBox  txtTABLE_ID = DirectCast(editItem("TABLE_ID").Controls(0), TextBox);
      

     
   


and this is my aspx code


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MultiRecordEditinRadGrid._Default" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register TagPrefix="sds" Namespace="Telerik.Web.SessionDS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/.../xhtml1-transitional.dtd">
<html xmlns="www.w3.org/.../xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>

    <script type="text/javascript">
var grid;

function RowSelected(rowObject)

  var selRow = this.GetCellByColumnUniqueName(rowObject,"ContactName");
  //here selRow.innerHTML will hold the value for the selected row contact name

function GetSelectedNames()

  for (var i = 0; i < grid.MasterTableView.SelectedRows.length; i++)
 
 var curRow = grid.MasterTableView.GetCellByColumnUniqueName(grid.MasterTableView.SelectedRows[i], "ContactName");
 //here curRow.innerHTML will hold the value for the selected rows contact names
 

function GridCreated()

  grid = this;

    </script>

    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <div>
        <%--<telerik:Header runat="server" ID="Header1" NavigationLanguage="CS"></telerik:Header>
       <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />--%>
        <!-- content start -->
        <%-- <telerik:ConfiguratorPanel runat="server" ID="ConfigurationPanel1" Title="Configurator" Expanded="true">
         
            Switch the edit modes:<br />--%>
        <div style="float: left; margin-left: 30px;">
            <asp:RadioButton ID="RadioButton1" AutoPostBack="True" Text="In-forms editing mode"
                runat="server" Checked="True" OnCheckedChanged="RadioCheckedChanged"></asp:RadioButton>
        </div>
        <div style="float: left;">
            <asp:RadioButton ID="RadioButton2" AutoPostBack="True" Text="In-line editing mode"
                runat="server" OnCheckedChanged="RadioCheckedChanged"></asp:RadioButton>
        </div>
        <div style="float: left; margin-left: 20px;">
            <asp:CheckBox ID="CheckBox1" Text="Allow multi-row edit" AutoPostBack="True" runat="server"
                OnCheckedChanged="CheckboxCheckedChanged"></asp:CheckBox>
        </div>
    
        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">

            <script type="text/javascript">
            function RowDblClick(sender, eventArgs)
           
              sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
           
            </script>

        </telerik:RadCodeBlock>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
            
                <telerik:AjaxSetting AjaxControlID="RadioButton1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                        <telerik:AjaxUpdatedControl ControlID="RadioButton2" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="RadioButton2">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                        <telerik:AjaxUpdatedControl ControlID="RadioButton1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="CheckBox1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
       
        <telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
            PageSize="10" AllowAutomaticUpdates="True" AllowPaging="True" AutoGenerateColumns="False"
            Height="464px" CellSpacing="0" GridLines="None" Skin="Sunset" OnInsertCommand="RadGrid1_InsertCommand"
            OnNeedDataSource="RadGrid1_NeedDataSource"
            onupdatecommand="RadGrid1_UpdateCommand">
            <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
            </HeaderContextMenu>
            <MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" DataKeyNames="TABLE_ID"
                HorizontalAlign="NotSet" AutoGenerateColumns="False">
                <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <ItemStyle CssClass="MyImageButton" />
                    </telerik:GridEditCommandColumn>
                   
                   <telerik:GridBoundColumn DataField="TABLE_ID" DataType="System.Decimal" FilterControlAltText="Filter TABLE_ID column"
                        HeaderText="TABLE_ID" SortExpression="TABLE_ID" UniqueName="TABLE_ID">
                  </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="COLUMN_NAME" HeaderText="COLUMN_NAME" UniqueName="COLUMN_NAME">
                    </telerik:GridBoundColumn>
                    <telerik:GridNumericColumn DataField="COLUMN_LENGTH" HeaderText="COLUMN_LENGTH" UniqueName="COLUMN_LENGTH">
                    </telerik:GridNumericColumn>
                    <telerik:GridBoundColumn DataField="COLUMN_TYPE" HeaderText="COLUMN_TYPE" UniqueName="COLUMN_TYPE">
                    </telerik:GridBoundColumn>
                   
                    <telerik:GridBoundColumn DataField="DESCRIPTION" HeaderText="DESCRIPTION" UniqueName="DESCRIPTION">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="LIST_ID" HeaderText="LIST_ID" UniqueName="LIST_ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridButtonColumn ConfirmText="Delete this record?" ConfirmDialogType="RadWindow"
                        ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
                        UniqueName="DeleteColumn">
                        <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                    </telerik:GridButtonColumn>
                </Columns>
              
            </MasterTableView>
            <FilterMenu EnableImageSprites="False">
            </FilterMenu>
        </telerik:RadGrid>
        <telerik:GridDropDownListColumnEditor ID="GridDropDownColumnEditor1" runat="server"
            DropDownStyle-Width="110px" />
      
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Project1ConnectionString %>"
            SelectCommand="SELECT [COLUMN_NAME] FROM [TABLE_STRUCTURE]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>


please reply as early as possible

Thanks in advance


Posted by Community Admin on 17-Jun-2011 00:00

Hi Chetana,

In the template of the RadGrid you have to use GridButtonColumn with a CommandName and create
<ClientEvents OnCommand="gridCommand" /> which will call js code that will return the clicked button. Inside the gridCommand you have to get commandName and commandArgument.

sample code

<telerik:RadScriptBlock runat="server"ID="block1">
        <script type="text/javascript">
            functiongridCommand(sender, args)
                if(args.get_commandName() == "CommentClick")
                    varitemIndexHierarhical = +args.get_commandArgument();
                    varitem = args.get_tableView().get_dataItems()[itemIndexHierarhical];
                    varname = item.getDataKeyValue("Name");
                    alert(name);
                
            
        </script>
        
     </telerik:RadScriptBlock>


Best wishes,
Ivan Dimitrov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed