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

How to add custom command with advanced data binding

4 Answers 578 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Al
Top achievements
Rank 1
Iron
Iron
Iron
Al asked on 14 Jan 2015, 10:01 AM
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?

4 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Tachev
Telerik team
answered on 19 Jan 2015, 08:57 AM
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.

 
0
Andy
Top achievements
Rank 1
answered on 17 Mar 2016, 08:44 PM

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!

0
Andy
Top achievements
Rank 1
answered on 18 Mar 2016, 11:58 AM
I guess the solution is to use row index provided by command argument and look for correct item in table view items collection.
0
Viktor Tachev
Telerik team
answered on 21 Mar 2016, 01:26 PM
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.
Tags
Grid
Asked by
Al
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Viktor Tachev
Telerik team
Andy
Top achievements
Rank 1
Share this question
or