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

Multiple ImageButtom CommandArguments to GridItemDataBound

3 Answers 120 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tommy
Top achievements
Rank 1
Tommy asked on 15 Dec 2016, 01:55 PM

Hello

My Grid

 

<telerik:RadGrid RenderMode="Lightweight" ID="catGrid" Width="944" AllowPaging="false" runat="server" GridLines="None" Skin="Office2010Blue"
                                                         AllowSorting="false" AutoGenerateColumns="False" OnNeedDataSource="CatGridNeedDataSource" ShowFooter="false" ShowStatusBar="false"
                                                         OnItemCommand="CatGridItemCommand" OnItemDataBound="CatGridItemDataBound">
                                            <MasterTableView DataKeyNames="ID" ClientDataKeyNames="ID">
                                                <Columns>
                                                    <telerik:GridCalculatedColumn  DataFields="TestName, TestNameDetail" Expression="{0}+{1}" HeaderText="TEST NAME">
                                                    </telerik:GridCalculatedColumn>
                                                    <telerik:GridCalculatedColumn  DataFields="Standard, StandardDetail" Expression="{0}+{1}"  HeaderText="STANDARD">
                                                    </telerik:GridCalculatedColumn>
                                                    <telerik:GridBoundColumn DataField="Pricing" HeaderText="PRICING" DataFormatString="{0:c}" HtmlEncode="false" ItemStyle-HorizontalAlign="right">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridTemplateColumn HeaderText="DETAIL" UniqueName="DETAIL">
                                                        <ItemTemplate>
                                                            <telerik:RadImageButton ID="detailBtn" runat="server" Image-Url="~/App_Themes/Site/Core/DetailIcon.png" Width="32" Height="32" CommandName="Detail" CommandArgument='<%#Eval("CategoryID") +","+ Eval("Details")%>'></telerik:RadImageButton>
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn HeaderText="REPORT" UniqueName="REPORT">
                                                        <ItemTemplate>
                                                            <telerik:RadImageButton ID="ReportBtn" runat="server" Image-Url="~/App_Themes/Site/Core/Reporticon.png" Width="32" Height="32" CommandName="Report" Value="ID"></telerik:RadImageButton>
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                </Columns>
                                            </MasterTableView>
                                            <ClientSettings EnableRowHoverStyle="true">
                                            </ClientSettings>
                                        </telerik:RadGrid>

 

My Code Behind

protected void CatGridItemDataBound(object sender, GridItemEventArgs e)
{
    
    //Is it a GridDataItem
    if (e.Item is GridDataItem)
    {
        var oRow = (DataRowView) e.Item.DataItem;
        var detr = (RadImageButton) oRow["DETAIL"];
        string detailTF = (detr).CommandArgument.ToString();
        var arg = new string[2];
        char[] splitter = { ',' };
        arg = detailTF.Split(splitter);
        HasDetail = Convert.ToBoolean(arg);
        if (HasDetail == true)
        {
            catGrid.MasterTableView.GetColumn(columnUniqueName: "DETAIL").Display = true;
        }
        else
        {
            catGrid.MasterTableView.GetColumn(columnUniqueName: "DETAIL").Display = false;
        }
    }
}

 

I am trying to get the second CommandArgument value which is true or false of the ImageButton. Once I have that value if false I want to set that row item to display.false. Currently I am receiving an error:

Unable to cast object of type 'DynamicClass1' to type 'System.Data.DataRowView'.

How can I achieve my goal?

3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 20 Dec 2016, 12:16 PM
Hello,

The exception you are seeing is most likely because e.Item.DataItem is not a DataRowView type. Please debug the code and see if that is the case.

With that said, you mention that you would like to hide the rows when a command is fired. However, in the code you are setting the Display property for the Columns in the Grid.

If you would like not to show some of the rows when a button is clicked I would recommend using the following approach.
  • Add a field in the data source that would indicate whether a record should be displayed (e.g. IsVisible)
  • Handle the ItemCommand event for RadGrid. In the handler you can check what is the currently fired command.
  • When the appropriate command is fired you can update the IsVisible field in the data source and rebind the Grid

This way you can use the IsVisible field when building the query for the data and only the relevant records will be displayed in the grid.



Regards,
Viktor Tachev
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Tommy
Top achievements
Rank 1
answered on 20 Dec 2016, 02:46 PM
Thank you for your post Viktor however I am not trying to accomplish this on a button or link but rather in itemdatabound when that row equals a false value from the database. The command argument detailButton will have 2 values CategoryID and Details which is a Boolean value. If Details = false then hide that row. Other rows could be true.
0
Viktor Tachev
Telerik team
answered on 23 Dec 2016, 10:29 AM
Hello,

Using the ItemDataBound event of the Grid to hide the whole row can result in unexpected behavior. Moreover, since some of the items will not be displayed there will be unnecessary sent from the server.

The recommended approach for your scenario is to add the Details field to the query that returns the data from the data source. This way only the items where Details=true will be returned. With this approach you will also have better performance as there will be less records that are retrieved.


Regards,
Viktor Tachev
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Tommy
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Tommy
Top achievements
Rank 1
Share this question
or