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

bind headers to database field value

1 Answer 60 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Anthony asked on 18 Aug 2014, 09:33 PM
I'm trying to make a gridview object dynamic so I don't have to update and distribute a binary every time I want to change the grid construction.

1) database table with information about reports called GeotechReports
2) database table with information about each field in GeotechReports called GeotechSetup
3) gridview object called GeotechGrid that has AutoGenerateColumns="True"
4) listbox object called GeotechHideColListbox that is bound to GeotechGrid to set the content and state of each listbox member

Information I need from a table, but can't seem to get into the grid or listbox:
1) The GeotechSetup table has two fields of interest: header and visibility. I want to somehow grab the header from this table for the grid to use so the database field name isn't the column header. Since the data is in GeotechReports and the header name is in GeotechSetup (different tables), I don't know how to make the grid understand to look at another datacontext/database table.
2) I want to change the listbox state in the same manner as above. By that I mean I want to set the state based on the visibility field in GeotechSetup.

Here's the relevant xaml:

<telerik:RadGridView Grid.Row="1" Grid.Column="0"
            HorizontalAlignment="Stretch" Margin="0,0,0,0"
            CanUserDeleteRows="False"     CanUserInsertRows="False"
            IsReadOnly="True"             IsFilteringAllowed="True"
            ColumnWidth="*"               SelectionMode="Multiple"
            SelectionUnit="FullRow"       AutoGenerateColumns="True"
            GroupRenderMode="Flat"        AutoExpandGroups="True"
            IsSynchronizedWithCurrentItem="True" x:Name="GeotechGrid"
            ItemsSource="{Binding Path=ReportsToDisplay, Mode=OneWay}"
            SelectedItem="{Binding Path=SelectedReport, Mode=TwoWay}">

<ListBox ItemsSource="{Binding Columns, ElementName=GeotechGrid}"
            <CheckBox Content="{Binding Header}"
                  IsChecked="{Binding IsVisible, Mode=TwoWay}" />

Essentially what I'm asking is how do I programmatically get access to GeotechGrid and then update each column with the header information stored in GeotechSetup? Here's what I've got so far. I know it accesses the GeotechSetups context as expected from the rlinq object because I displayed x.Fieldname and x.Fieldheader to a message box.

private void RetrieveSetupParameters()
  foreach(GeotechSetup x in this.context.GeotechSetups.ToList())
    // 1) x.Fieldname is the name of the field in the GeotechReport table
    // 2) x.FieldHeader is what I want to be the column header text
    // 3) x.FieldVisibility is what I want to use to set the listbox items
    //    and hide the columns I specify in the database

I don't need the communication to be in twoway mode. I'm only interested in doing this to set column default states on first load.

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 21 Aug 2014, 04:16 PM

The columns populated in RadGridView are presented in RadGridView.Columns collection.

As to Binding columns dynamically, you can check this github example on Binding columns from view model which allows dynamically adding and removing columns. 
Although GitHub is a very well-known platform we saw a better and easier approach for reviewing our examples developing our brand new SDK Samples Browser. You can also use it to review the examples.

Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question