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

Hide/Display Command button depending on the GridBoundColumn DataField

2 Answers 326 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Debashis Pyne
Top achievements
Rank 1
Debashis Pyne asked on 29 Jun 2010, 03:54 PM

Hi,

In my web application, I am trying to integrate RadGrid from code behind and depending on the GridBoundColumn DataField “Status” (having value 1 and 0), I would like to show/hide “Deactivate”/”Activate” image command button written under ItemTemplate, After lot of Googling, I couldn’t find similar solution. If there is any way out to achieve this, please suggest.

My code is as followes,

<telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowSelection="True"  AllowPaging="True" AllowSorting="True" AllowAutomaticDeletes="True" OnItemCommand="RadGrid1_ItemCommand"> 

    <MasterTableView EditMode="PopUp" PageSize="20"                                AutoGenerateColumns="False"  DataKeyNames="Consequence_Id" ClientDataKeyNames="Consequence_Id">

     <PagerStyle Mode="NextPrevAndNumeric" />

        <Columns>

        <telerik:GridBoundColumn DataField="Module_Content_Text"

            HeaderText="Consequence_Name" SortExpression="Module_Content_Text"

            UniqueName="Module_Content_Text">

        </telerik:GridBoundColumn>

 

        <telerik:GridBoundColumn DataField="Status" Visible="false"

            UniqueName="Status">

        </telerik:GridBoundColumn>

<telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">

<HeaderTemplate>

               <table width="100%">

                  <tr>

                    <td>

                       Options</td>

                     </tr>

                     </table>

                </HeaderTemplate>

<ItemTemplate>

    <table width="100%">

        <tr>

            <td>

               <asp:ImageButton ID="ImgBtnDeactivate" ToolTip="Deactivate Record" runat="server" CausesValidation="False" CommandName="Deselect" OnClientClick="return confirm('Are you sure you want to Deactivate this record?');" ImageUrl="~/Images/icon/cross.gif" />

            </td>

            <td>

               <asp:ImageButton ID="ImgBtnActivate" ToolTip="Activate Record" runat="server" CausesValidation="False" CommandName="Select" OnClientClick="return confirm('Are you sure you want to Activate this record?');" ImageUrl="~/Images/icon/right.gif" />

                </td>

                <td>

                    <asp:ImageButton ID="ImgBtnDelete" ToolTip="Delete Record" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');"  ImageUrl="~/Images/icon/delete.gif" />

                </td>

                <td>

                   <asp:ImageButton ID="ImgBtnEdit" ToolTip="Edit Record" runat="server" CausesValidation="False" CommandName="Edit" OnClientClick="return confirm('Are you sure you want to Edit this record?');"  ImageUrl="~/Images/icon/edit.gif" />

                </td>

            </tr>

        </table>

    </ItemTemplate>

            </telerik:GridTemplateColumn>

</Columns>

           </MasterTableView>

 

------------------------------------------------

 private void displayConsequenceGrid()
    {
        MiddleWare.BL.Consequence objC = new MiddleWare.BL.Consequence();
        DataTable dt = new DataTable();
        objC._Company_Id = System.Convert.ToInt32(Session[CSession.__Session_Company_Id]);
        objC._Language_Id = System.Convert.ToInt32(Session[CSession.__Session_Lanuage_Id]);
        dt = objC.GetDataAllConsequence();
        RadGrid1.DataSource = dt;
        RadGrid1.DataBind();

     }

 protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        int retResult = -100;
        MiddleWare.BL.Consequence oConsq = new MiddleWare.BL.Consequence();
        oConsq._Updated_By = Convert.ToInt32(Session[CSession.__Session_User_Id]);
        oConsq._Module_Id = oConsq._Module_Id;

  if (e.CommandName == RadGrid.EditCommandName)
        {
            int Consequence_Id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Consequence_Id"].ToString());
        }

 else if (e.CommandName == RadGrid.DeleteCommandName)
        {
            oConsq._Consequence_Id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Consequence_Id"].ToString());
            retResult = oConsq.DeleteConsequence();
            if (retResult == 2)
            {
                displayConsequenceGrid();
            }
           
        }

 else if (e.CommandName == RadGrid.SelectCommandName)
        {
            oConsq._Consequence_Id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Consequence_Id"].ToString());

            retResult = oConsq.ActivateConsequence();
            if (retResult == 1)
            {
                displayConsequenceGrid();
            }
        }


I am attaching the required result page as attached picture. Please help me out.

 

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 30 Jun 2010, 07:27 AM
Hello Debashis,

One Suggestion is using only one ImageButton for 'Activate/Deactivate' . Then from code behind based on the value of 'Status', you can set the properties of ImageButton accordingly . Sample code is given below.

ASPX:
 <ItemTemplate> 
    <table width="100%"
        <tr> 
           <td> 
              <asp:ImageButton ID="ImgBtn" runat="server" CausesValidation="False" /> 
           </td> 
           <td> 
              <asp:ImageButton ID="ImgBtnDelete" ToolTip="Delete Record" runat="server" CausesValidation="False" 
                                    CommandName="Delete" ImageUrl="~/Images/icon/delete.gif" /> 
           </td> 
           <td> 
                <asp:ImageButton ID="ImgBtnEdit" ToolTip="Edit Record" runat="server" CausesValidation="False" 
                                    CommandName="Edit" ImageUrl="~/Images/icon/edit.gif" /> 
           </td> 
         </tr> 
     </table> 
   </ItemTemplate> 

C#:
  
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            ImageButton ImgBtn = (ImageButton)item.FindControl("ImgBtn"); 
            if (item["Status"].Text == "1"
            { 
                ImgBtn.ToolTip = "Deactivate Record"
                ImgBtn.CommandName = "Deselect"
                ImgBtn.ImageUrl = "~/Images/icon/cross.gif"
                ImgBtn.OnClientClick = "return confirm('Are you sure you want to Deactivate this record?');"
            } 
            else 
            { 
                ImgBtn.ToolTip = "Activate Record"
                ImgBtn.CommandName = "Select"
                ImgBtn.ImageUrl = "~/Images/icon/right.gif"
                ImgBtn.OnClientClick = "return confirm('Are you sure you want to Activate this record?');"
            } 
          } 
     } 

Thanks,
Princy.
0
Debashis Pyne
Top achievements
Rank 1
answered on 30 Jun 2010, 11:41 AM

Hi Princy,

Thank you very much, it is the solution I am looking for.

Thanks,

Debasish

Tags
Grid
Asked by
Debashis Pyne
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Debashis Pyne
Top achievements
Rank 1
Share this question
or