当前位置:编程学习 > C#/ASP.NET >>

如何动态的为“DataGrid”添加“列”???

如何动态的为“DataGrid”添加“列”???   也就是通过后台代码添加列。。。

“DataGrid”定义如下:

<sdk:DataGrid Grid.Row="1" Name="dg_EnergyCustom" AutoGenerateColumns="False" >
                                            <sdk:DataGrid.Columns>
                                                <sdk:DataGridTemplateColumn     DisplayIndex="-1"   Header="年份" IsReadOnly="True" Width="30*" >
                                                    <sdk:DataGridTemplateColumn.CellTemplate >
                                                        <DataTemplate>
                                                            <TextBlock FontSize="13" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding EnergyYear}"></TextBlock>
                                                        </DataTemplate>
                                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                                </sdk:DataGridTemplateColumn>
                                                <sdk:DataGridTemplateColumn Header="项目" Width="30*">
                                                    <sdk:DataGridTemplateColumn.CellTemplate >
                                                        <DataTemplate>
                                                            <Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
                                                                <Grid.RowDefinitions>
                                                                    <RowDefinition Height="20" />
                                                                    <RowDefinition Height="20"/>
                                                                    <RowDefinition Height="20"/>
                                                                </Grid.RowDefinitions>
                                                                <TextBlock Text="能耗量合计" Grid.Row="0"   ></TextBlock>
                                                                <TextBlock Text="单位面积能耗" Grid.Row="1"></TextBlock>
                                                                <TextBlock Text="生均能耗" Grid.Row="2"></TextBlock>
                                                            </Grid>

                                                        </DataTemplate>
                                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                                </sdk:DataGridTemplateColumn>
                                                <sdk:DataGridTemplateColumn Header="全校" Width="40*">
                                                    <sdk:DataGridTemplateColumn.CellTemplate >
                                                        <DataTemplate>
                                                            <Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                                                <Grid.RowDefinitions>
                                                                    <RowDefinition Height="20" />
                                                                    <RowDefinition Height="20"/>
                                                                    <RowDefinition Height="20"/>
                                                                </Grid.RowDefinitions>
                                                                <TextBlock  Grid.Row="0"  Text="{Binding EnergySum}"  ></TextBlock>
                                                                <TextBlock  Grid.Row="1" Text="{Binding EnergyUnitArea}" ></TextBlock>
                                                                <TextBlock  Grid.Row="2" Text="{Binding EnergyEveryOne}"></TextBlock>
                                                            </Grid>

                                                        </DataTemplate>
                                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                                </sdk:DataGridTemplateColumn>
                                            </sdk:DataGrid.Columns>

                                        </sdk:DataGrid>

问题:

       如何通过后台代码为“DataGrid”添加一列???列的静态XAML定义如下:

       

      <sdk:DataGridTemplateColumn Header="电信学院" Width="40*">
                                                    <sdk:DataGridTemplateColumn.CellTemplate >
                                                        <DataTemplate>
                                                            <Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                                                <Grid.RowDefinitions>
                                                                    <RowDefinition Height="20" />
                                                                    <RowDefinition Height="20"/>
                                                                    <RowDefinition Height="20"/>
                                                                </Grid.RowDefinitions>
                                                                <TextBlock  Grid.Row="0"  Text="{Binding EnergySum1}"  ></TextBlock>
                                                                <TextBlock  Grid.Row="1" Text="{Binding EnergyUnitArea1}" ></TextBlock>
                                                                <TextBlock  Grid.Row="2" Text="{Binding EnergyEveryOne1}"></TextBlock>
                                                            </Grid>

                                                        </DataTemplate>
                                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                                </sdk:DataGridTemplateColumn>
                                            </sdk:DataGrid.Columns>

个人思路:

             实例化“DataGridTemplateColumn”对象“DataGridTemplateColumn dgtc = new DataGridTemplateColumn();”,然后将“dgtc”添加到“DataGrid”中。

             但是自己不知道,“CellTemplate”、“DataTemplate”这些元素怎样实例化,然后添加进入“ DataGridTemplateColumn”对象中。这样的功能应该怎么写??? --------------------编程问答-------------------- 不知C#代码改怎么写??? --------------------编程问答-------------------- dg.Columns.Clear();
BoundColumn colList = new BoundColumn();

colList = new BoundColumn();
colList.DataField="ID"; 
colList.SortExpression="ID";
colList.HeaderText="序号↓";
dg.Columns.Add(colList);
--------------------编程问答--------------------
引用 2 楼  的回复:
dg.Columns.Clear();
BoundColumn colList = new BoundColumn();

colList = new BoundColumn();
colList.DataField="ID"; 
colList.SortExpression="ID";
colList.HeaderText="序号↓";
dg.Columns.Add(colLis……


易做图,我这是基于Silverlight的啊。 --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,