How to add custom command with advanced data binding

5 posts, 1 answers
  1. Al
    Al avatar
    203 posts
    Member since:
    Oct 2006

    Posted 14 Jan 2015 Link to this post

    Hi,
    I am using the NeedDataSource event to populate my grid from my DB & I use a custom popup form for editing. The edit form is driven by a link created using 'AutoGenerateEditColumn' and then handling the OnCommand event.

    I would like to add my own button to the grid to popup another form so it could be either a 'custom' command (if such a thing exists) or I need to add a GridButtonColumn from code-behind.

    I couldn't find an example of how to do this - any ideas?
  2. Answer
    Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 19 Jan 2015 Link to this post

    Hello Al,

    In order to fire custom command for RadGrid you can use either GridButtonColumn or GridTemplateColumn. In order to handle the command you should use the ItemCommand event of RadGrid. Check out the following code snippets that illustrate the approach:

    RadGrid markup:

    <telerik:RadGrid runat="server" ID="RadGrid1"
        OnNeedDataSource="RadGrid1_NeedDataSource"
        AutoGenerateColumns="false"
        AutoGenerateEditColumn="true"
        AllowPaging="true" PageSize="10"
        OnItemCommand="RadGrid1_ItemCommand">
     
        <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID" CommandItemSettings-ShowExportToExcelButton="true">
             
            <Columns>
                <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" ReadOnly="true" InsertVisiblityMode="AlwaysVisible">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Description" HeaderText="Description" UniqueName="Description">
                </telerik:GridBoundColumn>
                <telerik:GridButtonColumn ButtonType="LinkButton" Text="Custom Text" CommandName="CustomCommand"></telerik:GridButtonColumn>
     
                <telerik:GridTemplateColumn UniqueName="TemplateColumnUniqueName">
                    <ItemTemplate>
                        <asp:LinkButton Text="Click" runat="server" ID="LinkButton1" CommandName="AnotherCustomCommand" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    Event handler:

    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName=="CustomCommand")
        {
            // code to execute on custom command
        }
     
        if (e.CommandName == "AnotherCustomCommand")
        {
            // code to execute on another custom command
        }
    }



    Regards,
    Viktor Tachev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Andy
    Andy avatar
    54 posts
    Member since:
    Jun 2009

    Posted 17 Mar Link to this post

    Hi Victor,

    In my grid I have GridButtonColumn with custom command name "EditRecord" .When ItemCommand event fires, e.CommandArgument shows the row index, but e.Item is always the same - the one with zero index, no matter which row initiated the event.I tried setting commandName to "Edit" and using GridEditCommandColumn and in both cases it works correctly.

    Any ideas what's wrong here? Thanks!

  5. Andy
    Andy avatar
    54 posts
    Member since:
    Jun 2009

    Posted 18 Mar Link to this post

    I guess the solution is to use row index provided by command argument and look for correct item in table view items collection.
  6. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 21 Mar Link to this post

    Hello Andy,

    I am attaching a sample project that illustrates how you can access data relevant to the item where the button is clicked.

    Give the attached sample a try and see how it works for you. If you would like additional information on accessing grid cells you would find the following article interesting.



    Regards,
    Viktor Tachev
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017