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

GridView with nested DataTemplate Grid, how to add rows and columns programmatically?

3 Answers 243 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 26 Jun 2012, 05:31 PM
I have a GridView in my xaml.  I am planning on showing a nested GridView when the row is clicked on.  I have a DataTemplate for that, located in my XAML Resources.

            <DataTemplate x:Key="KeyTemplate">
                <telerik:RadGridView x:Name="rgvKeyData"
                                     CanUserFreezeColumns="False"
                                     DataContext="{Binding}"
                                     IsReadOnly="True"
                                     RowIndicatorVisibility="Collapsed"
                                     ShowGroupPanel="False" />
            </DataTemplate>

I would like to add columns and rows to this GridView programmatically, in the .cs file, but I cannot access rgvKeyData in my code.  Here is what I am trying to do in the C#... the two bold commented out lines are where I am trying to work with the GridView:

        private void RefreshDataGrid(string pFieldNames, string pFieldValues)
        {
            var _KeyDataTemplate = Resources["KeyTemplate"] as DataTemplate;
            
            var _Rows = new SortableCollectionView();

            pFieldNames = " Columns:|" + pFieldNames;
            pFieldValues = "Values:|" + pFieldValues;
            
            var _FieldNamesList = pFieldNames.Split('|');
            var _FieldValueList = pFieldValues.Split('|');

            try
            {
                var i = 0;

                while (i < _FieldNamesList.Length)
                {
                    var _Col = new DataGridTextColumn
                    {
                        Header = _FieldNamesList[i],
                        Binding = new System.Windows.Data.Binding
                        {
                            Converter = new RowIndexConverter(),
                            ConverterParameter = _FieldNamesList[i]
                        }
                    };
                    
                    //rgvKeyData.Columns.Add(_Col);

                    i++;
                }
                
                i = 0;
                
                var _Row = new Row();

                while (i < _FieldValueList.Length)
                {
                    if (i < _FieldNamesList.Length)
                    {
                        _Row[_FieldNamesList[i]] = _FieldValueList[i];
                    }

                    i++;
                }

                _Rows.Add(_Row);

                //rgvKeyData.ItemsSource = _Rows;
            }
            catch
            {
                //quiet Catch
            }
        }

I either need to be able to access the rgvKeyTemplate GridView in my cs code, or I need another way to do this.  I am pretty new to Telerik controls.  I understand how to do a DataTemplate for a nested GridView, but only when I know the Grid columns before runtime.  I just don't know how to handle the DataTemplate at runtime.

3 Answers, 1 is accepted

Sort by
0
Stephen
Top achievements
Rank 1
answered on 27 Jun 2012, 03:12 PM
I figured out how to do this.  Just in case anyone wants to know, I will lay out the basic pieces.  This adds several columns, depending on what the contents of Column Names (| delimited) and then splits out Column Values into those columns.  There will only ever be one row in this case, but it does show how the process works.

Xaml:
        <Grid.Resources>
            <DataTemplate x:Key="InnerGridTemplate">
                <telerik:RadGridView x:Name="rgvInnerGrid"
                                     AutoGenerateColumns="False"
                                     CanUserFreezeColumns="False"
                                     DataContext="{Binding}"
                                     IsReadOnly="True"
                                     Loaded="InnerGrid_Loaded"
                                     RowIndicatorVisibility="Collapsed"
                                     ShowGroupPanel="False" />
            </DataTemplate>
        </Grid.Resources>

Xaml Grid View:
<telerik:RadGridView.RowDetailsTemplate>
                                    <DataTemplate>
                                        <StackPanel Margin="10,10,10,10"
                                                    Orientation="Horizontal" />
                                    </DataTemplate>
        </telerik:RadGridView.RowDetailsTemplate>

C#
        private void rgvAuditLogData_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            var _Object = e.DataElement as AuditLogByTableDetail;
            var _Row = e.Row as GridViewRow;

            if (_Object == null || _Row == null)
            {
                return;
            }
}

        private void InnerGrid_Loaded(object sender, RoutedEventArgs e)
        {
            var _InnerGrid = (RadGridView) sender;
            var _Data = (AuditLogByTableDetail) rgvAuditLogData.SelectedItem;

            RefreshDataGrid(_InnerGrid, _Data);
        }

private void RefreshDataGrid(RadGridView pGrid, AuditLogByTableDetail pData)
        {
            pData.ColumnNames = "Column|" + pData.ColumnNames;
            pData.ColumnValues = "Value|" + pData.ColumnValues;
            
            var _FieldNamesList = pData.ColumnNames.Split('|');
            var _CurrentList = pData.ColumnValues.Split('|');

            try
            {
                var _Rows = new SortableCollectionView();
                var i = 0;
                
                while (i < _FieldNamesList.Length)
                {
                    var _Col = new GridViewDataColumn();
                    _Col.Header = _FieldNamesList[i];
                    _Col.DataMemberBinding = new System.Windows.Data.Binding();
                    _Col.DataMemberBinding.Converter = new RowIndexConverter();
                    _Col.DataMemberBinding.ConverterParameter = _FieldNamesList[i];

                    pGrid.Columns.Add(_Col);
                    i++;
                }
                
                var _Row = new Row();
                i = 0;

                while (i < _CurrentList.Length)
                {
                    if (i < _FieldNamesList.Length)
                    {
                        _Row[_FieldNamesList[i]] = _CurrentList[i];
                    }

                    i++;
                }

                while (i < _FieldNamesList.Length)
                {
                    _Row[_FieldNamesList[i]] = "";
                    i++;
                }

                _Rows.Add(_Row);
                pGrid.ItemsSource = _Rows;
            }
            catch (Exception)
            {
                ShowErrorPopup("blah");
            }
        }
0
Dimitrina
Telerik team
answered on 27 Jun 2012, 03:13 PM
Hello,

 You could access the hierarchy grid the first time it is loaded. You could subscribe for its Loaded event and add column programmatic when the event is raised.

private void childGrid_Loaded(object sender, RoutedEventArgs e)
       {
           RadGridView grid = (sender as RadGridView);
           // your logic
       }
Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Stephen
Top achievements
Rank 1
answered on 27 Jun 2012, 03:14 PM
Actually, I finally figured that out (or something very similar to it).  I posted my code, just in case someone else is looking for a way to do this.  Thank you for your response.
Tags
GridView
Asked by
Stephen
Top achievements
Rank 1
Answers by
Stephen
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or