Inserting Values Using FormTemplate if No Records are Found from Filter Template (Add New Records event only).

4 posts, 0 answers
  1. gc_0620
    gc_0620 avatar
    246 posts
    Member since:
    Feb 2008

    Posted 14 May Link to this post

    Folks

    Using Telerik UI for ASP.Net Ajax R1 2019 with Visual Studio. Below link is my prototype and assuming Filtering is unabled in the Form.

    https://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/edit-form-types/defaultcs.aspx

    I would like to Perform the Following:

    1) End User Filter The Form to Find if the Row Exists based on Filter Conditions of 'First and Last Name'.
    2) If no rows are found, they click 'Add new Record' Button.
    3) When Insert Form Opens it should automatically Populate 'First and Last Name' in GridEditableItem from Filter Template Conditions ''First and Last Name'. Item #1 above. I came up with below codes from RadGrid1_ItemDataBound event.

     

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
      
     if (e.Item.OwnerTableView.IsItemInserted && e.Item is GridEditableItem && e.Item.IsInEditMode && e.Item.OwnerTableView.Name == "Master")
        {
     
                     
        }
     
    }

     

    4) Attached is my desired Screen Shot.

    Any Help will be appreciated.

    gc_0620

  2. Attila Antal
    Admin
    Attila Antal avatar
    427 posts

    Posted 17 May Link to this post

    Hi Ghulam,

    There are several ways you can auto-populate the Form controls during inserting a new record.

    1. Setting the DefaultInsertValue property of the GridColumn in the markup:

    <telerik:GridBoundColumn DefaultInsertValue="Some value"></telerik:GridBoundColumn>

    2. Setting the DefaultInsertValue property of the GridColumn in the code behind using the PreRender event:

    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        RadGrid grid = sender as RadGrid;
        (grid.MasterTableView.GetColumn("ShipName") as GridBoundColumn).DefaultInsertValue = "Firstname";
        (grid.MasterTableView.GetColumn("ShipCountry") as GridBoundColumn).DefaultInsertValue = "Lastname";
    }

    3. Access the required controls in Edit-Insert form and set their text accordingly:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
        {
            // insert item
            GridEditFormInsertItem insertitem = e.Item as GridEditFormInsertItem;
            (insertitem["shipname"].Controls[0] as TextBox).Text = "first name";
            (insertitem["shipcountry"].Controls[0] as TextBox).Text = "last name";
        }
        else
        {
            //edit item
        }
    }

    Please let me know if I have missed something or if you have any questions.

    Kind regards,
    Attila Antal
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.

    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        RadGrid grid = sender as RadGrid;
        (grid.MasterTableView.GetColumn("ShipName"as GridBoundColumn).DefaultInsertValue = "Firstname";
        (grid.MasterTableView.GetColumn("ShipCountry"as GridBoundColumn).DefaultInsertValue = "Lastname";
    }

  3. gc_0620
    gc_0620 avatar
    246 posts
    Member since:
    Feb 2008

    Posted 17 May in reply to Attila Antal Link to this post

    Hi Attila, I am sorry to say that you misunderstood me. All I wanted is to populate Controls First/Last Names in RadGrid1_ItemDataBound event and if e.item is GridEditFormInsertItem from FilterItem search values First/Last Names.

    Conditions:

    1) If no records are found based on Filter Columns Conditions

    2) Followed by users clicked 'Add new record' button.

     

    Attached is my step by step work flow.

     

    Also below is my source codes.

    <%@ Page Language="C#" CodeFile="DefaultCS.aspx.cs" Inherits="Grid.Examples.DataEditing.TemplateFormUpdate.DefaultCS"
        MasterPageFile="~/MasterPage.master" %>
     
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
            <script type="text/javascript">
                function RowDblClick(sender, eventArgs) {
                    sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
                }
                  function MenuShowing(sender, args) {
                    if (column == null)
                        return;
                    var menu = sender;
                    var items = menu.get_items();
                    if (column.get_dataType() == "System.String") {
                        var i = 0;
                        while (i < items.get_count()) {
                            if (!(items.getItem(i).get_value() in { 'NoFilter': '', 'StartsWith': '', 'EndsWith': '', 'Contains': '', 'IsNull': '' })) {
     
                                //  if (!(items.getItem(i).get_value() in { 'NoFilter': '', 'StartsWith': '', 'EndsWith': '','Contains': '', 'IsNull': '' })) {
                                var item = items.getItem(i);
                                if (item != null)
                                    item.set_visible(false);
                            }
                            else {
                                var item = items.getItem(i);
                                if (item != null)
                                    item.set_visible(true);
                            }
                            i++;
                        }
                    }
                    if (column.get_dataType() == "System.DateTime") {
                        var j = 0;
                        while (j < items.get_count()) {
     
                            if (!(items.getItem(j).get_value() in { 'NoFilter': '', 'EqualTo': '', 'GreaterThanOrEqualTo': '', 'LessThanOrEqualTo': '', 'IsNull': '' })) {
     
                                var item = items.getItem(j);
                                if (item != null)
                                    item.set_visible(false);
                            }
                            else {
                                var item = items.getItem(j);
                                if (item != null)
                                    item.set_visible(true);
                            }
                            j++;
                        }
                    }
     
                    if (column.get_dataType() == "System.Int32" || column.get_dataType() == "System.Int16") {
                        var j = 0;
                        while (j < items.get_count()) {
                            if (!(items.getItem(j).get_value() in { 'NoFilter': '', 'EqualTo': '' })) {
                                var item = items.getItem(j);
                                if (item != null)
                                    item.set_visible(false);
                            }
                            else {
                                var item = items.getItem(j);
                                if (item != null)
                                    item.set_visible(true);
                            }
                            j++;
                        }
                    }
                    column = null;
                }
                  function filterMenuShowing(sender, eventArgs) {
                    column = eventArgs.get_column();
                }
            </script>
        </telerik:RadCodeBlock>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceholder1" runat="Server">
        <p id="divMsgs" runat="server">
            <asp:Label ID="Label1" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#FF8080">
            </asp:Label>
            <asp:Label ID="Label2" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#00C000">
            </asp:Label>
        </p>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                        <telerik:AjaxUpdatedControl ControlID="divMsgs"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
        </telerik:RadAjaxLoadingPanel>
        <telerik:RadFormDecorator RenderMode="Lightweight" ID="RadFormDecorator1" runat="server" DecorationZoneID="demo" DecoratedControls="All" EnableRoundedCorners="false" />
        <div id="demo" class="demo-container no-bg">
            <telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" CssClass="RadGrid" GridLines="None"
                AllowPaging="True" PageSize="20" AllowSorting="True" AutoGenerateColumns="False"
                ShowStatusBar="true" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowFilteringByColumn="True"
                AllowAutomaticUpdates="True" DataSourceID="SqlDataSource1" OnItemDeleted="RadGrid1_ItemDeleted"
                OnItemInserted="RadGrid1_ItemInserted" OnItemUpdated="RadGrid1_ItemUpdated" OnItemCommand="RadGrid1_ItemCommand"
                OnPreRender="RadGrid1_PreRender">
                 <ClientSettings>
                <Selecting AllowRowSelect="True" />
                <ClientEvents OnFilterMenuShowing="filterMenuShowing" />
            </ClientSettings>
                <MasterTableView CommandItemDisplay="TopAndBottom" DataSourceID="SqlDataSource1"
                    DataKeyNames="EmployeeID">
                    <Columns>
                        <telerik:GridEditCommandColumn>
                        </telerik:GridEditCommandColumn>
                        <telerik:GridBoundColumn UniqueName="EmployeeID" HeaderText="ID" DataField="EmployeeID">
                            <HeaderStyle Width="70px"></HeaderStyle>
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="TitleOfCourtesy" HeaderText="TOC" DataField="TitleOfCourtesy">
                            <HeaderStyle Width="80px"></HeaderStyle>
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="FirstName" HeaderText="FirstName" DataField="FirstName">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="LastName" HeaderText="LastName" DataField="LastName">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="HireDate" HeaderText="Hire Date" DataField="HireDate"
                            DataFormatString="{0:d}">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="Title" HeaderText="Title" DataField="Title">
                        </telerik:GridBoundColumn>
                        <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="column">
                        </telerik:GridButtonColumn>
                    </Columns>
                    <EditFormSettings EditFormType="Template">
                        <FormTemplate>
                            <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
                                style="border-collapse: collapse;">
                                <tr class="EditFormHeader">
                                    <td colspan="2">
                                        <b>Employee Details</b>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <table id="Table3" width="450px" border="0" class="module">
                                            <tr>
                                                <td class="title" style="font-weight: bold;" colspan="2">Company Info:</td>
                                            </tr>
                                            <tr>
                                                <td>Country:
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Country") %>'>
                                                    </asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>City:
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("City") %>' TabIndex="1">
                                                    </asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Region:
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Region") %>' TabIndex="2">
                                                    </asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2">
                                                    <b>Personal Info:</b>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Title Of Courtesy
                                                </td>
                                                <td>
                                                    <asp:DropDownList ID="ddlTOC" runat="server" SelectedValue='<%# Bind("TitleOfCourtesy") %>'
                                                        DataSource='<%# (new string[] { "Dr.", "Mr.", "Mrs.", "Ms." }) %>' TabIndex="7"
                                                        AppendDataBoundItems="True">
                                                        <asp:ListItem Selected="True" Text="Select" Value="">
                                                        </asp:ListItem>
                                                    </asp:DropDownList>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>FirstName:
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="TextBox2" Text='<%# Bind( "FirstName") %>' runat="server" TabIndex="8">
                                                    </asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Last Name:
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="TextBox3" Text='<%# Bind( "LastName") %>' runat="server" TabIndex="9">
                                                    </asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Birth Date:
                                                </td>
                                                <td>
                                                    <telerik:RadDatePicker RenderMode="Lightweight" ID="BirthDatePicker" runat="server" MinDate="1/1/1900" DbSelectedDate='<%# Bind("BirthDate") %>'
                                                        TabIndex="4">
                                                    </telerik:RadDatePicker>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Hire Date:
                                                </td>
                                                <td>
                                                    <telerik:RadDatePicker RenderMode="Lightweight" ID="HireDatePicker" DbSelectedDate='<%# Bind( "HireDate") %>'
                                                        runat="server" TabIndex="10">
                                                    </telerik:RadDatePicker>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Title:
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="TextBox4" Text='<%# Bind( "Title") %>' runat="server" TabIndex="11">
                                                    </asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Home Phone:
                                                </td>
                                                <td>
                                                    <telerik:RadMaskedTextBox RenderMode="Lightweight" ID="HomePhoneBox" runat="server" SelectionOnFocus="SelectAll"
                                                        Text='<%# Bind("HomePhone") %>' PromptChar="_" Mask="(###) ###-####"
                                                        TabIndex="3">
                                                    </telerik:RadMaskedTextBox>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                    <td style="vertical-align: top">
                                        <table id="Table1" cellspacing="1" cellpadding="1" width="250" border="0" class="module">
                                            <tr>
                                                <td>Notes:
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <asp:TextBox ID="TextBox1" Text='<%# Bind("Notes") %>' runat="server" TextMode="MultiLine"
                                                        Rows="5" Columns="40" TabIndex="5">
                                                    </asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Address:
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <asp:TextBox ID="TextBox6" Text='<%# Bind("Address") %>' runat="server" TextMode="MultiLine"
                                                        Rows="2" Columns="40" TabIndex="6">
                                                    </asp:TextBox>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2"></td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td align="right" colspan="2">
                                        <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                            runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button
                                        <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                            CommandName="Cancel"></asp:Button>
                                    </td>
                                </tr>
                            </table>
                        </FormTemplate>
                    </EditFormSettings>
                </MasterTableView>
                <FilterMenu OnClientShown="MenuShowing" />
                <ClientSettings>
                    
                    <ClientEvents OnRowDblClick="RowDblClick"></ClientEvents>
                </ClientSettings>
            </telerik:RadGrid>
        </div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TelerikConnectionString35 %>"
            DeleteCommand="DELETE FROM [Employees_Northwind] WHERE [EmployeeID] = @EmployeeID"
            InsertCommand="INSERT INTO [Employees_Northwind] ([LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [Country], [HomePhone], [Notes], [ReportsTo]) VALUES (@LastName, @FirstName, @Title, @TitleOfCourtesy, @BirthDate, @HireDate, @Address, @City, @Region, @Country, @HomePhone, @Notes, @ReportsTo)"
            SelectCommand="SELECT * FROM [Employees_Northwind]" UpdateCommand="UPDATE [Employees_Northwind] SET [LastName] = @LastName, [FirstName] = @FirstName, [Title] = @Title, [TitleOfCourtesy] = @TitleOfCourtesy, [BirthDate] = @BirthDate, [HireDate] = @HireDate, [Address] = @Address, [City] = @City, [Region] = @Region, [Country] = @Country, [HomePhone] = @HomePhone, [Notes] = @Notes WHERE [EmployeeID] = @EmployeeID">
            <DeleteParameters>
                <asp:Parameter Name="EmployeeID" Type="Int32"></asp:Parameter>
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="LastName" Type="String"></asp:Parameter>
                <asp:Parameter Name="FirstName" Type="String"></asp:Parameter>
                <asp:Parameter Name="Title" Type="String"></asp:Parameter>
                <asp:Parameter Name="TitleOfCourtesy" Type="String"></asp:Parameter>
                <asp:Parameter Name="BirthDate" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="HireDate" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="Address" Type="String"></asp:Parameter>
                <asp:Parameter Name="City" Type="String"></asp:Parameter>
                <asp:Parameter Name="Region" Type="String"></asp:Parameter>
                <asp:Parameter Name="Country" Type="String"></asp:Parameter>
                <asp:Parameter Name="HomePhone" Type="String"></asp:Parameter>
                <asp:Parameter Name="Notes" Type="String"></asp:Parameter>
                <asp:Parameter Name="ReportsTo" Type="Int32" DefaultValue=""></asp:Parameter>
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="LastName" Type="String"></asp:Parameter>
                <asp:Parameter Name="FirstName" Type="String"></asp:Parameter>
                <asp:Parameter Name="Title" Type="String"></asp:Parameter>
                <asp:Parameter Name="TitleOfCourtesy" Type="String"></asp:Parameter>
                <asp:Parameter Name="BirthDate" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="HireDate" Type="DateTime"></asp:Parameter>
                <asp:Parameter Name="Address" Type="String"></asp:Parameter>
                <asp:Parameter Name="City" Type="String"></asp:Parameter>
                <asp:Parameter Name="Region" Type="String"></asp:Parameter>
                <asp:Parameter Name="Country" Type="String"></asp:Parameter>
                <asp:Parameter Name="HomePhone" Type="String"></asp:Parameter>
                <asp:Parameter Name="Notes" Type="String"></asp:Parameter>
                <asp:Parameter Name="EmployeeID" Type="Int32"></asp:Parameter>
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
    </asp:Content>
     
    __________________

    ___________

    using System;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
     
     
    namespace Grid.Examples.DataEditing.TemplateFormUpdate
    {
        public partial class DefaultCS : System.Web.UI.Page
        {
            protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
            {
                if (e.Exception != null)
                {
                    e.KeepInEditMode = true;
                    e.ExceptionHandled = true;
                    DisplayMessage(true, "Employee " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"] + " cannot be updated. Reason: " + e.Exception.Message);
                }
                else
                {
                    DisplayMessage(false, "Employee " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"] + " updated");
                }
            }
     
            protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
            {
                if (e.Exception != null)
                {
                    e.ExceptionHandled = true;
                    e.KeepInInsertMode = true;
                    DisplayMessage(true, "Employee cannot be inserted. Reason: " + e.Exception.Message);
                }
                else
                {
                    DisplayMessage(false, "Employee inserted");
                }
            }
     
            protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
            {
                if (e.Exception != null)
                {
                    e.ExceptionHandled = true;
                    DisplayMessage(true, "Employee " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"] + " cannot be deleted. Reason: " + e.Exception.Message);
                }
                else
                {
                    DisplayMessage(false, "Employee " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"] + " deleted");
                }
            }
     
            private void DisplayMessage(bool isError, string text)
            {
                Label label = (isError) ? this.Label1 : this.Label2;
                label.Text = text;
            }
     
            protected void Page_Load(object sender, System.EventArgs e)
            {
                GridFilterMenu menu = RadGrid1.FilterMenu;
     
                foreach (RadMenuItem item in menu.Items)
                { //change the text for the "StartsWith" menu item 
                    if (item.Text == "StartsWith")
                    {
                        item.Text = "Starts with";
                    }
                    else if (item.Text == "NoFilter")
                    {
                        item.Text = "Clear";
                    }
                    else if (item.Text == "IsNull")
                    {
                        item.Text = "Is Null";
                    }
                    else if (item.Text == "EndsWith")
                    {
                        item.Text = "Ends with";
                    }
                    else if (item.Text == "EqualTo")
                    {
                        item.Text = "Equal to";
                    }
                    else if (item.Text == "NotEqualTo")
                    {
                        item.Text = "Not equal to";
                    }
                    else if (item.Text == "DoesNotContain")
                    {
                        item.Text = "Does not contain";
                    }
                    //
                    else if (item.Text == "GreaterThan")
                    {
                        item.Text = "Greater than";
                    }
                    else if (item.Text == "LessThan")
                    {
                        item.Text = "Less than";
                    }
                    else if (item.Text == "GreaterThanOrEqualTo")
                    {
                        item.Text = "Greater than or equal to";
                    }
                    else if (item.Text == "LessThanOrEqualTo")
                    {
                        item.Text = "Less than or equal to";
                    }
                    else if (item.Text == "IsNull")
                    {
                        item.Text = "Is Null";
                        //  item.Text = "Empty";
                    }
                    else if (item.Text == "NotIsNull")
                    {
                        item.Text = "Not empty";
                    }
                }
     
     
            }
     
            protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
            {
                if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked
                {
                    GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
                    editColumn.Visible = false;
                }
                else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)
                {
                    e.Canceled = true;
                }
                else
                {
                    GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
                    if (!editColumn.Visible)
                        editColumn.Visible = true;
                }
            }
            protected void RadGrid1_PreRender(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    RadGrid1.EditIndexes.Add(0);
                    RadGrid1.Rebind();
                }
            }
    }
    }

     

    I modified the following:

    1) Rad Grid Allow Filtering by Columns = true.

    2) Page load event changed the filter conditions Texts to meaningful Text. i.e.

    if (item.Text == "StartsWith")
       {
                        item.Text = "Starts with";
     }
     
     else if (item.Text == "NoFilter")
      {
                        item.Text = "Clear";
      }
     
    ''''''

    3) Inside RadCodeBlock1 Script in .aspx page reduced the filter item values.

     

    I hope my explanation are clear.

     

    Thanks

     

    gc_0620

     

     

     

     

  4. Attila Antal
    Admin
    Attila Antal avatar
    427 posts

    Posted 24 May Link to this post

    Hi gc_0620,

    Thank you for the additional information. I do have a better understanding on the requirements now. 

    Here is a demo GIF animation about the scenario I am understanding.

    RadGrid_Insert_Search_Values_as_Default_Insertvalue

    The above scenario was achieved using the following code snippets:

    Assuming there are the Firstname and Lastname columns in the markup:

    <telerik:GridBoundColumn DataField="Firstname"
        FilterControlAltText="Filter Firstname column" HeaderText="FirstName"
        SortExpression="Firstname" UniqueName="Firstname">
    </telerik:GridBoundColumn>
    <telerik:GridBoundColumn DataField="Lastname"
        FilterControlAltText="Filter Lastname column" HeaderText="LastName"
        SortExpression="Lastname" UniqueName="Lastname">
    </telerik:GridBoundColumn>

    In the ItemDataBound event of the Grid:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
        {
            //insert item
            GridEditFormInsertItem insertitem = e.Item as GridEditFormInsertItem;
            // cast sender to grid
            RadGrid grid = sender as RadGrid;
            // check for filter results, no items means no results
            if (grid.Items.Count < 1)
            {
                // get reference to the firstname and lastname columns
                GridBoundColumn firstNameCol = grid.MasterTableView.GetColumn("Firstname") as GridBoundColumn;
                GridBoundColumn lastNameCol = grid.MasterTableView.GetColumn("Lastname") as GridBoundColumn;
                // access the Firstname and Lastname controls in the Editform and set their values to the values specified in the Filter control
                (insertitem["Firstname"].Controls[0] as TextBox).Text = firstNameCol.CurrentFilterValue;
                (insertitem["Lastname"].Controls[0] as TextBox).Text = lastNameCol.CurrentFilterValue;
            }
        }
        else
        {
            //edit item
        }
    }

    Please let me know if I have missed something.

    Kind regards,
    Attila Antal
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top