在TextBox WPF中传递短日期

问题描述 投票:0回答:1

你好,我有一个dataGrid,双击后会打开一个新的Window,并填充一些TextBoxes我的问题是Textboxes想要显示短日期时显示日期和时间。

我已将SQL Server数据库严格配置为Date,它显示了数据库中的短日期,所以我不确定为什么它在DateTime中传递了Textbox

请帮助!

这是我为主窗口的代码:

        private void dtGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {

        // User double clicks on DataGrid Row
        // Open new Window
        // Populate selected textboxes with selected datarow
        DataGrid gd = (DataGrid)sender;
        DataRowView row_selected = gd.SelectedItem as DataRowView;

        var windowToOpen = new Window1();

        if (gd != null)
        {
            // Textboxes
            windowToOpen.txt_RowRecrd.Text = row_selected["DSP_ID"].ToString();
            windowToOpen.txt_DateResolved.Text = row_selected["DATERSLVD"].ToString();
            windowToOpen.txt_revcls.Text = row_selected["RateType"].ToString();

            windowToOpen.Show();
       }
    }

这里是我的TextBoxes之一的XAML:

                        <TextBox
                        x:Name="txt_DateResolved"
                        Width="110"
                        Height="26"
                        Margin="5,0,0,0"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Bottom"
                        Background="Transparent"
                        BorderBrush="Transparent"
                        BorderThickness="0"
                        Foreground="Black"
                        IsEnabled="True">
                    </TextBox>
c# sql wpf datetime textbox
1个回答
0
投票

由于在C#中没有Date结构,所以只有DateTime struct。您可以使用DateTime方法设置DateTime的格式。您应该有一个DateTime实例,并可以在其上调用以下方法:

ToString()

dateTime.ToShortDateString()
© www.soinside.com 2019 - 2024. All rights reserved.