开发者

Apply the sorting of a datagrid to another datagrid

开发者 https://www.devze.com 2023-02-03 20:51 出处:网络
My client has this requirement : i.e. a grid with collapsible columns.The easiest way I found to do that is to have 3 separate datagrids and 2 buttons, showing or collapsing the 开发者_如何学运维grid

My client has this requirement :

Apply the sorting of a datagrid to another datagrid

i.e. a grid with collapsible columns. The easiest way I found to do that is to have 3 separate datagrids and 2 buttons, showing or collapsing the 开发者_如何学运维grids.

Here is the associated XAML:

<StackPanel Orientation="Horizontal" Grid.Column="2" Grid.Row="0" VerticalAlignment="Stretch">

            <toolkit:DataGridDragDropTarget VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">

                <sdk:DataGrid Name="Grid1" SelectionChanged="Grid_SelectionChanged" AutoGenerateColumns="False">
                    <sdk:DataGrid.Columns>
                        <sdk:DataGridTextColumn Binding="{Binding somefield}" Header="someheader" />
                        <sdk:DataGridTextColumn Binding="{Binding somefield}" Header="someheader" />
                        <sdk:DataGridTextColumn Binding="{Binding somefield}" Header="someheader"/>
                        <sdk:DataGridTextColumn Binding="{Binding somefield}" Header="someheader" />
                    </sdk:DataGrid.Columns>
                </sdk:DataGrid>

            </toolkit:DataGridDragDropTarget>

            <Button Content=">" Click="Button_Click" Name="btn1" />
            <sdk:DataGrid Name="Grid2" SelectionChanged="Grid_SelectionChanged" AutoGenerateColumns="False" ItemsSource="{Binding ItemsSource, ElementName=Grid1}">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn Header="someheader" Binding="{Binding somefield}" />
                    <sdk:DataGridCheckBoxColumn Header="someheader" Binding="{Binding somefield}" />
                    <sdk:DataGridCheckBoxColumn Header="someheader" Binding="{Binding somefield}" />
                    <sdk:DataGridTextColumn Header="someheader" Binding="{Binding somefield}" />
                    <sdk:DataGridTextColumn Header="someheader" Binding="{Binding somefield}" />
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>

            <Button Content=">" Name="btn2" Click="Button_Click"/>
            <sdk:DataGrid Name="Grid3" SelectionChanged="Grid_SelectionChanged" AutoGenerateColumns="False"  ItemsSource="{Binding ItemsSource, ElementName=Grid1}">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn Header="someheader" Binding="{Binding somefield}" />
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>

        </StackPanel>

This is working correctly. My problem occurs when I want to sort one of the grid. As the sorting is internal to the datagrid, the changes are not propagated to the other (even tough they are bound to the same source !).

Is there a way to "propagate" the sorting to the other grids ? I tried to find a way to intercept a sorting event, but it doesn't seem to exist...

Thanks in advance !


Why use 3 database? It's better to use just 1 database and a Options page where the person can hide his / her columns. you can hide to columns by using DataGrid.Columns[0].Visibility = Visibility.Collapsed.

You can do this with a button above the column.. or a checkbox somewhere... or a options page. Lots of oppertunaties. The only downside is that this can't be implemented in MVVM since DataGridColumn dont support Visibility Binding.

As a answer to your question:

Perhaps this information helps you, just apply sorting to both datagrids

private void Sort_DataGrid(object source,  System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
    // Never use Queries like this always use Stored procedures

    SqlCommand myCommand = new SqlCommand("SELECT * FROM Categories", myConnection);                                                            
    myCommand.CommandType = CommandType.Text;
    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
    DataSet ds = new DataSet();
    myAdapter.Fill(ds,"Categories");
    DataView dv = new DataView(ds.Tables["Categories"]);
    if( (numberDiv%2) == 0 )
        dv.Sort = e.SortExpression + " " + "ASC";
    else
        dv.Sort = e.SortExpression + " " + "DESC";
    numberDiv++;
    myDataGrid.DataSource = dv;
    myDataGrid.DataBind();
}

Source: http://www.codeproject.com/KB/webforms/SortingDataGridColumns.aspx


  1. If you stick to the three DataGrid solution I think you'd better wrap them in a control and add some sort buttons to this control. The buttons should then trigger the sorting of ALL datagrids.

  2. Another way is to use 1 DataGrid and change the visibilty of the column(s) you would want to hide.

0

精彩评论

暂无评论...
验证码 换一张
取 消