LINQ to SQL类,如何更改列格式? (数据网格)

时间:2016-04-22 14:48:43

标签: c# wpf linq datagrid sql-server-2014

如何更改DataGrid控件中LINQ to SQL类的列中的数据格式?

我有一个带有DateTime数据的SQL服务器表列,它显示为您所期望的10/19/2015 8:30:00 AM 如何更改格式以仅显示日期10/19/2015

我在属性面板中看不到任何可以执行此操作的内容。这会在XMAL中处理吗?

到目前为止我的代码。

public partial class MainWindow : Window
    {
        HomeLINQSqlDataContext dc = new HomeLINQSqlDataContext(Properties.Settings.Default.Staff_ManagerConnectionString2);
        //TimeRecorderLINQSqlDataContext dc = new TimeRecorderLINQSqlDataContext(Properties.Settings.Default.Staff_ManagerConnectionString1);       
        public MainWindow()
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

            PopulateCollection();
            if (dc.DatabaseExists()) TimeRcorderDataGrid.ItemsSource = dc.Staff_Time_TBLs;
            //if (dc.DatabaseExists()) TimeRcorderDataGrid.ItemsSource = dc.Staff_Data_TBLs;
            InitializeComponent();


        }
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            dc.SubmitChanges();
        }
        private void PopulateCollection()
        {
            ObservableCollection<FooClass> fooColl = new ObservableCollection<FooClass>();
            for (int i = 0; i <= 10; i++)
            {
                fooColl.Add(new FooClass() { Description = i.ToString(), SomeDate = DateTime.Now });
            }
            TimeRcorderDataGrid.ItemsSource = fooColl;
        }
    }
    public class FooClass
    {
        public string Description { get; set; }
        public DateTime SomeDate { get; set; }

    }  

}

XAML,

 <TabControl>
        <TabItem Header="   社員データ   " FontSize="20">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="16*"/>
                    <ColumnDefinition Width="113*"/>
                    <ColumnDefinition Width="1057*"/>
                </Grid.ColumnDefinitions>
                <DataGrid>
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding StartDate, StringFormat=\{0:dd.MM.yy \}}" />
                    </DataGrid.Columns>
                </DataGrid>
                <DataGrid Name="TimeRcorderDataGrid" Margin="0,0,0,79" Grid.ColumnSpan="3">
                    <DataGrid.Columns>
                        <DataGridTextColumn Width="Auto" />
                    </DataGrid.Columns>
                </DataGrid>                
                <Button x:Name="SaveButton" Content="Save" Click="SaveButton_Click" Grid.Column="1" HorizontalAlignment="Left" Margin="10,583,0,0" VerticalAlignment="Top" Width="391" Grid.ColumnSpan="2" Height="57"/>
            </Grid>
        </TabItem>
        <TabItem Header="   入力ページ   " FontSize="20">
            <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Margin="10" Name="dataGrid1">
                <DataGrid.Columns>
                    <DataGridComboBoxColumn Header="Combo" Width="300"
                     SelectedItemBinding="{Binding Values}" 
                     />
                </DataGrid.Columns>
            </DataGrid>
        </TabItem>


    </TabControl>

由于

2 个答案:

答案 0 :(得分:1)

如果您明确定义列(而不是依赖于autoGenerateColumns),您应该能够说明日期的格式;

<DataGrid  AutoGenerateColumns="False" ... >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Raised" Binding="{Binding INSERT_PROPERTY_HERE, StringFormat=\{0:dd/MM/yy\}}"/>
    </DataGrid.Columns>
 </DataGrid>

答案 1 :(得分:0)

您可以在绑定中使用DateTime格式化StringFormat。让我举个例子:

XAML:

<DataGrid Name="dataGrid" VerticalGridLinesBrush="Yellow"  
   HorizontalGridLinesBrush="Yellow" AutoGenerateColumns="False">
   <DataGrid.Columns>
       <DataGridTextColumn Binding="{Binding Description}" />
       <DataGridTextColumn Binding="{Binding SomeDate, StringFormat=\{0:dd.MM.yy \}}" />                
   </DataGrid.Columns>
</DataGrid>

型号:

public class FooClass
{

    public string Description { get; set; }
    public DateTime SomeDate { get; set; }
}

代码隐藏:

public MainWindow()
{
   Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
   InitializeComponent();
   PopulateCollection();
}

private void PopulateCollection()
{
    ObservableCollection<FooClass> fooColl = new ObservableCollection<FooClass>();
    for (int i = 0; i <= 10; i++)
    {
        fooColl.Add(new FooClass() { Description=i.ToString(), SomeDate=DateTime.Now});
    }
    dataGrid.ItemsSource = fooColl;
}

<强>更新

DataGrid中有12列从SQL Server导入,其中只有两列是DateTime,No.7和No.8列。如何指定这些列的格式?

您应明确声明DataGrid的列,并为StringFormatNo. 7列撰写No. 8。例如:

<DataGrid Name="TimeRcorderDataGrid" Margin="0,0,0,79" Grid.ColumnSpan="3">
  <DataGrid.Columns>
     <DataGridTextColumn Binding="{Binding ColumnOne}" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnTwo}" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnThree}" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnFour}" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnFive}" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnSix}" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnSeven, StringFormat=\{0:dd.MM.yy \}}"}" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnEight, StringFormat=\{0:dd.MM.yy \}}"}" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnNine}" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnTen" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnEleven}" Width="Auto" />
     <DataGridTextColumn Binding="{Binding ColumnTwelve}" Width="Auto" />
  </DataGrid.Columns>
</DataGrid>