Datagrid中的列重新排序

时间:2015-07-13 06:51:31

标签: c# wpf silverlight datagrid

我使用Datagrid显示Silverlight中的记录。我使用 Itemsource绑定DataGrid中的Data 。它工作正常。

我形成Grid Like,

Grid x:Name="LayoutRoot" Background="White" Margin="-157,-45,-318,0">

       <sdk:DataGrid HorizontalAlignment="Left" Height="242.195"                                        
                     Margin="93.282,26.764,0,0" VerticalAlignment="Top" 
                     Width="496.08" RenderTransformOrigin="0.5,0.5"
                     UseLayoutRounding="False" d:LayoutRounding="Auto" Name="showrecordgrid" >

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

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

            <sdk:DataGrid.RenderTransform>
                <CompositeTransform Rotation="-0.566"/>
            </sdk:DataGrid.RenderTransform>


        </sdk:DataGrid>

按钮将显示在Column的开头。我需要重新排序列末尾的按钮吗?

参考ScreenShot, enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用AutoGeneratedColumns事件对列重新排序。首先为您的列设置一些标题名称,如下所示:

   <sdk:DataGrid ItemsSource="{Binding YourItems}" AutoGeneratedColumns="DataGrid_AutoGeneratedColumns" Name="showrecordgrid" ...>

       <sdk:DataGrid.Columns>
            <sdk:DataGridTemplateColumn Header="Edit">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button >Edit</Button>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>

            <sdk:DataGridTemplateColumn Header="Delete">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button >Delete</Button>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>
        </sdk:DataGrid.Columns>

然后按标题查找这些列,并将它们重新排序为最后一个:

    private void DataGrid_AutoGeneratedColumns(object sender, EventArgs e)
    {
        var grid = (DataGrid)sender;
        foreach (var item in grid.Columns)
        {
            if (item.Header.ToString() == "Edit")
            {
                item.DisplayIndex = grid.Columns.Count - 1;
            }
            else if (item.Header.ToString() == "Delete")
            {
                item.DisplayIndex = grid.Columns.Count - 2;
            }
        }
    }