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

Possible Radgrid to have different column template

1 Answer 28 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Phumlani Maxwell
Top achievements
Rank 1
Phumlani Maxwell asked on 09 Oct 2013, 08:21 AM
Hi
I would like to use the hierarchy radgrid tool and I have used it before. I have search on the net and found no solution for the issues I have.
1st - Is it possible to have different templates in one gridcolumn e.g. Combobox, Checkbox, textbox and raddatepicker. It should always display one control depending on the other field e.g. control type code.
2nd - Is it possible to disable add button on the detail grid on some of the rows and not all and it shall depend on the e.g. [hasrow] field on the parent row.

The columns for detail table will always be equal but each detail table can have different control depending on the parent row.
Please also bear in mind that columns will be auto generated as this will be determine by the database which columns to display.

Regards,
Max

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 11 Oct 2013, 10:23 AM
Hi ,

Please try the sample code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="true"
    OnPreRender="RadGrid1_PreRender" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top">
        <DetailTables>
            <telerik:GridTableView DataSourceID="SqlDataSource2" DataKeyNames="OrderID" CommandItemDisplay="Top"
                Name="Child">
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="OrderID" MasterKeyField="OrderID" />
                </ParentTableRelation>
                <Columns>
                    <telerik:GridTemplateColumn HeaderText="GridTemplateColumn">
                        <ItemTemplate>
                            <telerik:RadComboBox ID="RadComboBox1" runat="server" Visible="false" DataSourceID="SqlDataSource1"
                                DataTextField="ShipCity" DataValueField="ShipCity">
                            </telerik:RadComboBox>
                            <asp:CheckBox ID="CheckBox1" runat="server" Visible="false" />
                            <asp:TextBox ID="TextBox1" runat="server" Visible="false"></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
      //Hide AddNewRecordButton if the rows are empty
       foreach (GridDataItem item in RadGrid1.Items)
        {
            if (item.Expanded)
            {
                GridTableView detailTable = (GridTableView)item.ChildItem.NestedTableViews[0];
                if (detailTable.Items.Count == 0)
                {
                    GridCommandItem itm = (GridCommandItem)detailTable.GetItems(GridItemType.CommandItem)[0];
                    itm.FindControl("AddNewRecordButton").Parent.Visible = false;
                }
            }
        }
    }  
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
     //Different controls in the same column of the detail table
        if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "Child")
        {
            GridDataItem item = (GridDataItem)e.Item;
            string value = item.GetDataKeyValue("OrderID").ToString();
            if (Some Condition)
            {
                RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
                combo.Visible = true;
            }
            if (Some Condition)
            {
                CheckBox check = (CheckBox)item.FindControl("CheckBox1");
                check.Visible = true;
            }
            if (Some Condition)
            {
                TextBox txt = (TextBox)item.FindControl("TextBox1");
                txt.Visible = true;
            }
        }
    }

Thanks,
Princy
Tags
Grid
Asked by
Phumlani Maxwell
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Share this question
or