Select to view content in your preferred language

Can I change the layout of the datagrid in code-behind?

1293
10
04-26-2011 08:39 AM
DanDong
New Contributor
Hi all,

My case is that the spatial query task in my application may perform on multiple layers and I bind the results to a datagrid. But each spatial query task's result is different from the others. For example, for county layer, the result contains name, area, pop1997, pop2000 and fips, while for town layer, the result contains name, county fips, town fips, area.

My first thought is to generate many datagrid and design them according to each layer's result. Then in the code behind I can use if...else if...or switch to identify which layer the spatial query is working on and make its paired datagrid visible. But....I am wondering if there is any more convenient way to do that, like I only add one single datagrid in xaml and change the layout of it in the codes according to the layer that spatial query is working on, or some better methods to do that? Any suggestions? Thank you very much!

Here is my datagrid in xaml:
 <Border x:Name="CountyResultsDisplay" Background="#77919191" BorderThickness="1" CornerRadius="5"
                HorizontalAlignment="Center"  VerticalAlignment="Top" Visibility="Collapsed"
                Margin="5" Padding="10" BorderBrush="Black">
                <Border.Effect>
                    <DropShadowEffect/>
                </Border.Effect>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="15" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <TextBlock x:Name="DataDisplayTitle" Text="Search Results" Foreground="Black" FontSize="9" Grid.Row="0" FontWeight="Bold" />
                    <slData:DataGrid x:Name="QueryDetailsDataGrid" Grid.Row="1" Width="Auto" Height="170" AutoGenerateColumns="False" HeadersVisibility="Column" Background="White" 
                             IsReadOnly="True" HorizontalScrollBarVisibility="Hidden"
                             RowStyle="{StaticResource MyCustomRow}" CanUserSortColumns="True"
                             SelectionChanged="QueryDetailsDataGrid_SelectionChanged"
                             LoadingRow="QueryDetailsDataGrid_LoadingRow">
                        <slData:DataGrid.Columns>
                        <slData:DataGridTextColumn CanUserSort="True" SortMemberPath="NAME_CNTY" Binding="{Binding Attributes[OBJECTID]}" Header="Rec"/>
                        <slData:DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[NAME_CNTY]}" Header="County Name"/>
                        <slData:DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[AREA_SQMI]}" Header="Area"/>
                        <slData:DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[POP_1990]}" Header="Population 1997"/>
                        <slData:DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[POP_2000]}" Header="Population 2000"/>
                        <slData:DataGridTextColumn CanUserSort="True"  SortMemberPath="POP2007" Binding="{Binding Attributes[FIPS_CNTY]}" Header="FIPS"/>
                        </slData:DataGrid.Columns>
                    </slData:DataGrid>
                </Grid>
            </Border>
0 Kudos
10 Replies
DanDong
New Contributor
Hey Dominique, that really works! In the meanwhile I learned a new method from this thread! 😉 I am able to generate the datagrid just like what I want now. Really appreciate!

                int rec = 0;
                foreach (var feature in featureSet.Features)
                    feature.Attributes["Rec"] = ++rec;

                DataGrid dg = rectangleselection.QueryDetailsDataGrid;
                List<string> fields = new List<string>();
                dg.Columns.Clear();

                //generate the first column for record index
                DataGridTextColumn dataGridTextColumnRec = new DataGridTextColumn();
                Binding Recbinder = new Binding();
                Recbinder.Path = new PropertyPath("Attributes[Rec]");
                dataGridTextColumnRec.Header = "Rec";
                Recbinder.Mode = BindingMode.OneWay;
                dataGridTextColumnRec.Binding = Recbinder;
                dg.Columns.Add(dataGridTextColumnRec);

                //generate the other columns
                foreach (var item in featureSet.FieldAliases)
                {
                    DataGridTextColumn dataGridTextColumn = new DataGridTextColumn();
                    Binding binder = new Binding();
                    fields.Add(item.Value);   //FieldAliases is a Dictionary, the field name being the key and the field alias being the value.
                    binder.Path = new PropertyPath("Attributes[" + item.Key + "]");
                    dataGridTextColumn.Header = item.Value;
                    binder.Mode = BindingMode.OneWay;
                    dataGridTextColumn.Binding = binder;
                    dg.Columns.Add(dataGridTextColumn);
                }

[ATTACH]6509[/ATTACH]
0 Kudos