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

Only show SELECT command button if column value has certain value

2 Answers 124 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 10 Jul 2012, 05:31 PM
Hello,

I have a RadGrid data bound using an EntityDataSource.

Functionality I am attempting to accomplish: The user should only be able to select a row if a column in that row contains a specific value. 

The column unique name which contains the value is PrpAnswerType.
When this rows' databound value is 6 for 'PrpAnswerType', that row should be selectable. Otherwise, there should be no select button.

I can't figure out how to hide/show the SELECT LinkButton based on another columns value for each row.

Here is my grid

<telerik:RadGrid ID="grdProperties" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
    AllowAutomaticUpdates="True" AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True"
    AutoGenerateHierarchy="True" CellSpacing="0" DataSourceID="edsProps" GridLines="None"
    SkinID="FewColumnsFewRecords">
    <ClientSettings>
        <Selecting CellSelectionMode="None"></Selecting>
    </ClientSettings>
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="PrpID" DataSourceID="edsProps">
        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
        <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
        </RowIndicatorColumn>
        <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridBoundColumn DataField="PrpID" DataType="System.Int32" FilterControlAltText="Filter PrpID column"
                HeaderText="PrpID" ReadOnly="True" SortExpression="PrpID" UniqueName="PrpID"
                Visible="False">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="PrpName" FilterControlAltText="Filter PrpName column"
                HeaderText="Prop. Name" SortExpression="PrpName" UniqueName="PrpName">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="PrpQuestion" FilterControlAltText="Filter PrpQuestion column"
                HeaderText="Question Text" SortExpression="PrpQuestion" UniqueName="PrpQuestion">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn DataField="PrpAnswerType" FilterControlAltText="Filter PrpAnswerType column"
                HeaderText="Answer Type" SortExpression="PrpAnswerType" UniqueName="PrpAnswerType">
                <EditItemTemplate>
                    <telerik:RadComboBox ID="RadComboBox1" runat="server" SelectedValue='<%# Bind("PrpAnswerType") %>'>
                        <Items>
                            <telerik:RadComboBoxItem runat="server" Text="Integer" Value="1" />
                            <telerik:RadComboBoxItem runat="server" Text="Decimal" Value="2" />
                            <telerik:RadComboBoxItem runat="server" Text="Currency" Value="3" />
                            <telerik:RadComboBoxItem runat="server" Text="Boolean" Value="4" />
                            <telerik:RadComboBoxItem runat="server" Text="Text" Value="5" />
                            <telerik:RadComboBoxItem runat="server" Text="Custom Options" Value="6" />
                        </Items>
                    </telerik:RadComboBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="PrpAnswerTypeLabel" runat="server" OnDataBinding="PrpAnswerTypeLabel_DataBinding"
                        Text='<%# Eval("PrpAnswerType") %>'></asp:Label>
                      
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridButtonColumn CommandName="Select" FilterControlAltText="Filter AddColumnOption column"
                Text="Add Column Option" UniqueName="AddColumnOption">
            </telerik:GridButtonColumn>
        </Columns>
        <EditFormSettings>
            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
            </EditColumn>
        </EditFormSettings>
    </MasterTableView>
    <FilterMenu EnableImageSprites="False">
    </FilterMenu>
</telerik:RadGrid>
<asp:EntityDataSource ID="edsProps" runat="server" ConnectionString="name=kdEntities"
    DefaultContainerName="kdEntities" EnableDelete="True" EnableFlattening="False"
    EnableInsert="True" EnableUpdate="True" EntitySetName="Properties">
</asp:EntityDataSource>

Thank you
Kevin

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Jul 2012, 06:46 PM
Hello,
function RowSelecting(sender, eventArgs) {
               if (eventArgs.getDataKeyValue("PrpAnswerType") == "6") {
                   eventArgs.set_cancel(true);
               }
           }
<MasterTableView DataKeyNames="PrpAnswerType" ClientDataKeyNames="PrpAnswerType" >
<ClientEvents OnRowSelecting="RowSelecting" />



Thanks,
Jayesh Goyani
0
Shinu
Top achievements
Rank 2
answered on 11 Jul 2012, 05:26 AM
Hi,

Please try the following code snippet to make the ButtonColumn visible only for the row whose 'PrpAnswerType' value is 6.

C#:
protected void grdProperties_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem) 
    {
        GridDataItem ditem = (GridDataItem)e.Item;
        LinkButton lnk=(LinkButton)ditem["AddColumnOption"].Controls[0];
        Label lbl = (Label)ditem.FindControl("PrpAnswerTypeLabel");
        if (lbl.Text == "6")
        {
            lnk.Visible=true;
        }
        else
        lnk.Visible=false;
    }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Share this question
or