2

请看下面的代码。当我单击 Revert to org order 按钮时,应将列重新排序为原始数据网格顺序。有没有可能?请帮帮我...... //以下是我的代码

    <Window x:Class="GridTextBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized" Loaded="MainWindow_Loaded" Background="Gray">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".25*"/>

            <ColumnDefinition Width=".25*"/>
            <ColumnDefinition Width=".25*"/>
            <ColumnDefinition Width=".25*"/>
        </Grid.ColumnDefinitions>
        <DataGrid ColumnReordered="datagrid1_ColumnReordered"  GotFocus="datagrid1_GotFocus" LoadingRowDetails="datagrid1_LoadingRowDetails"  Grid.ColumnSpan="3" Background="Gray" Grid.Row="1" Height="auto" Name="datagrid1" AutoGenerateColumns="False" Width="440" VerticalAlignment="Center" SelectionChanged="datagrid1_SelectionChanged">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Empid">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Name="txtEmpid"   Text="{Binding Empid}"></TextBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Empname">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Empname}"></TextBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Empname}"></TextBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Empaddress">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Empaddress}"></TextBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="EmpCity">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding EmpCity}"></TextBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Empstate">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding EmpState}"></TextBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="EmpCountry">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding EmpCountry}"></TextBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>           
            </DataGrid.Columns>

        </DataGrid>
        <Button Grid.Row="2" Grid.Column="2" Height="30" Content="Insert Employees" Click="Button_Click"></Button>
        <Button Grid.Row="2" Grid.Column="1" Height="30" Content="Revert to org order" Name="btn" Click="btn_Click"></Button>
    </Grid>
</Window>
4

2 回答 2

1

您可以在内存中保留一个小字典,并在单击按钮时使用每列的DisplayIndex 属性来重置原始视图。


编辑:添加一个例子

XAML

<Grid>
   <Grid.RowDefinitions>
      <RowDefinition Height="281*" />
      <RowDefinition Height="30*" />
   </Grid.RowDefinitions>
   <DataGrid Name="datagrid1" ItemsSource="12345567" Loaded="datagrid1_Loaded">
      <DataGrid.Columns>
         <DataGridTextColumn Header="Col1" Binding="{Binding}" />
         <DataGridTextColumn Header="Col2" Binding="{Binding}" />
         <DataGridTextColumn Header="Col3" Binding="{Binding}" />
         <DataGridTextColumn Header="Col4" Binding="{Binding}" />
         <DataGridTextColumn Header="Col5" Binding="{Binding}" />
      </DataGrid.Columns>
   </DataGrid>
   <Button Grid.Row="1" Content="Reorder" Click="Button_Click"/>
</Grid>

在你的代码后面:

private Dictionary<int, string> originalOrder;
private void datagrid1_Loaded(object sender, RoutedEventArgs e)
{
   if (originalOrder == null) originalOrder = new Dictionary<int, string>();
   //I will save the header content as a string, 
   //but you can save anything you want to identify each column
   datagrid1.Columns.ToList().ForEach(c => originalOrder.Add(c.DisplayIndex, c.Header.ToString()));
}

private void Button_Click(object sender, RoutedEventArgs e)
{
   originalOrder.ToList().ForEach(k =>
   {
    datagrid1.Columns.FirstOrDefault(c => c.Header.ToString() == k.Value).DisplayIndex = k.Key;
   });
}
于 2013-04-23T07:55:19.980 回答
0
 //This is my code behind file for the above xaml
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace GridTextBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 

    //private Dictionary<int, string> orde=new Dictionary<int, string>() ;
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();


        }

        protected void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Employee emp = new Employee();
                DataTable dt = emp.GetEmployees();
                datagrid1.ItemsSource = dt.DefaultView;

         }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            InsertEmployees ie=new InsertEmployees();
            ie.Show();
        }

        private void datagrid1_GotFocus(object sender, RoutedEventArgs e)
        {


        }

        private void datagrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }



         private void datagrid1_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
        {

            var txtbox = GetTemplateChild("txtEmpid") as TextBox;
            txtbox.Text = "hiiiii";

        }

         private void datagrid1_ColumnReordered(object sender, DataGridColumnEventArgs e)
         {

         }

         //declare here your dictionary
         private Dictionary<int, string> originalOrder;

         private void datagrid1_Loaded(object sender, RoutedEventArgs e)
         {
           if (originalOrder == null) originalOrder = new Dictionary<int, string>();

           datagrid1.Columns.ToList().ForEach(c => originalOrder.Add(c.DisplayIndex, c.Header.ToString()));
         }

         private void btn_Click(object sender, RoutedEventArgs e)
         {

          originalOrder.ToList().ForEach(k => 
          {            
          datagrid1.Columns.FirstOrDefault(c => c.Header.ToString() == k.Value).DisplayIndex = k.Key;
          });
         }


    }
}
于 2013-04-24T10:30:49.460 回答