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

How to conditionally show/hide EditFormSettings Edit Column insert/Update/Cancel Image Button, hide Child Table Command Item Template

3 Answers 535 Views
Grid
This is a migrated thread and some comments may be shown as answers.
gc_0620
Top achievements
Rank 1
gc_0620 asked on 25 May 2012, 01:26 AM

Folks,

Using VS 2010 with RadControls for ASP.NET AJAX Q1 2012. I am using below link as Prototype.

http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/validation/defaultcs.aspx

My question's are:
1) I would like to hide the EditColumn Insert/Update/Cancel Button image based on certain condition of a Grid Column Data.

Example:

- If RadGrid DataItem Units in Stock is > 25, then show EditColumn Insert/Update/Cancel Button image.
- If RadGrid DataItem Units in Stock is < 25, then Just show EditColumn Cancel Button image and change CancelText ="Exit"

Attached is my screen printout.

2) Is it possible to hide Child Table Command item Template based on a Grid Dataitem column value of Parent Item? Ex: using Northwind Customer Table, if Customer Id = 'ALFKI', hide Command Item Template of its Child Table, if Customer Id is <> 'ALFKI', show Command Item Template of its Child Table.

Thanks a lot for help

GC_0620

3 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 25 May 2012, 07:26 AM
Hello,

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
           OnItemDataBound="RadGrid1_ItemDataBound
           ondetailtabledatabind="RadGrid1_DetailTableDataBind">
           <MasterTableView DataKeyNames="CustomerId,Stock" CommandItemDisplay="Top" Name="parent">
               <Columns>
                   <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="CustomerId" HeaderText="CustomerId" UniqueName="CustomerId">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="Stock" HeaderText="Stock" UniqueName="Stock">
                   </telerik:GridBoundColumn>
                   <telerik:GridEditCommandColumn>
                   </telerik:GridEditCommandColumn>
               </Columns>
               <DetailTables>
                   <telerik:GridTableView CommandItemDisplay="Top" Name="child">
                       <Columns>
                           <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                           </telerik:GridBoundColumn>
                       </Columns>
                   </telerik:GridTableView>
               </DetailTables>
           </MasterTableView>
       </telerik:RadGrid>
protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
    {
        GridDataItem item = e.DetailTableView.ParentItem as GridDataItem;
        string strCustomerId = item.GetDataKeyValue("CustomerId").ToString();
        if (strCustomerId == "ALFKI")
        {
            e.DetailTableView.CommandItemDisplay = GridCommandItemDisplay.None;
        }
 
        
        dynamic data = new[] {
                new { ID = 1, Name ="Name_1"},
                new { ID = 2, Name = "Name2"},
                new { ID = 3, Name = "Name3"},
                new { ID = 4, Name = "Name4"},
                new { ID = 5, Name = "Name5"}
            };
 
        e.DetailTableView.DataSource = data;
         
 
    }
 
 protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DateTime dt = DateTime.Now;
 
        dynamic data = new[] {
                new { ID = 1, Name ="Name_1", customdate=dt,NO = 11.00,CustomerId="ALFKI",Stock=20},
                new { ID = 2, Name = "Name2", customdate=dt,NO = 11.00,CustomerId="ALFKI2",Stock=30},
                new { ID = 3, Name = "Name3", customdate=dt,NO = 11.00,CustomerId="ALFKI1",Stock=20},
                new { ID = 4, Name = "Name4", customdate=dt,NO = 11.44,CustomerId="ALFKI",Stock=40},
                new { ID = 55555, Name = "Name5", customdate=dt,NO = 10.4,CustomerId="ALFKI",Stock=20}
            };
 
        RadGrid1.DataSource = data;
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        
        if (e.Item.IsInEditMode)
        {
            if (e.Item is GridDataInsertItem)
            {  
            }
            else
            {
                GridEditableItem editItem = (GridEditableItem)e.Item;
                int stock = 0;
 
                int.TryParse(editItem.GetDataKeyValue("Stock").ToString(),out stock);
 
                if (stock < 25)
                {
                    LinkButton updateButton = (LinkButton)editItem.FindControl("UpdateButton");
                    updateButton.Visible = false;
                    LinkButton CancelUpdateButton = (LinkButton)editItem.FindControl("CancelButton");
                    CancelUpdateButton.Text = "Exit";
                }
                // If you used ButtonType = "ImageButton"
                //ImageButton updateButton = (ImageButton)editItem.FindControl("UpdateButton");
                //ImageButton CancelUpdateButton = (ImageButton)editItem.FindControl("CancelButton");
                 
            }
        }
 
 
    }


Thanks,
Jayesh Goyani
0
gc_0620
Top achievements
Rank 1
answered on 25 May 2012, 06:16 PM
Thank you Jayesh, your solution works.

One quick question, Is it possible to hide certain command template items from Child Table based on Grid Dataitem column value of Parent Item? Assuming Child Table command Item Template has following items (Add New, Edit, Delete etc.). 

So if Customer Id = 'ALFKI' in parent table, just hide Delete command template item of Child Table. If Customer Id is <> 'ALFKI', show all available Command Template items of its Child Table.

Thanks again for your help.

gc_0620
0
Accepted
Shinu
Top achievements
Rank 2
answered on 28 May 2012, 08:47 AM
Hello,

Try the following code to achieve your scenario.
C#:
string value;
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
  if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "detail")
  {
    GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
    value = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["CustomerID"].ToString();
    if (value == "ALFKI")
    {
       GridTableView childtable = (GridTableView)e.Item.OwnerTableView;
       GridCommandItem item = (GridCommandItem)childtable.GetItems(GridItemType.CommandItem)[0];
       Button btn = (Button)item.FindControl("DeleteBtn");
       btn.Visible = false;
    }
  }
}

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