Home > database >  Datetime without time textbox C# (Winform)
Datetime without time textbox C# (Winform)

Time:10-23

I'm learning code so I have a problem that I want to show date from datagridview to textbox but I don't know how to show it without time. Thank you guys. enter image description here

public partial class StackoverFlowForm : Form
{
    private readonly BindingSource _bindingSource = new();
    private readonly string _dateFormat = "dd/MM/yyyy";
    public StackoverFlowForm()
    {
        InitializeComponent();

        _bindingSource.DataSource = Mocked.Table();
        dataGrid.DataSource = _bindingSource;
        dataGrid.Columns["DateItem"]!.DefaultCellStyle.Format = _dateFormat;
    }

    private void GetStartDateButton_Click(object sender, EventArgs e)
    {
        if (dataGrid.CurrentRow!.Cells["DateItem"].Value.IsNull())
        {
            txtDate.Text = "";
        }
        else
        {
            var dateTime = (((DataRowView)_bindingSource.Current)!)
                .Row.Field<DateTime>("DateItem");

            txtDate.Text = dateTime.ToString(_dateFormat);
        }
    }
}

#region Place these classes in separate files
public static class LanguageExtensions
{
    public static bool IsNull(this object sender)
        => sender == null || sender == DBNull.Value || Convert.IsDBNull(sender);
}
public class Mocked
{
    public static DataTable Table()
    {
        var dt = new DataTable();


        dt.Columns.Add(new DataColumn()
        {
            ColumnName = "Id",
            DataType = typeof(int),
            AutoIncrement = true,
            AutoIncrementSeed = 1,
            ColumnMapping = MappingType.Hidden
        });

        dt.Columns.Add(new DataColumn() { ColumnName = "FirstName", DataType = typeof(string) });
        dt.Columns.Add(new DataColumn() { ColumnName = "LastName", DataType = typeof(string) });
        dt.Columns.Add(new DataColumn() { ColumnName = "DateItem", DataType = typeof(DateTime) });

        dt.Rows.Add(null, "Jeanine", "Abbott",
            new DateTime(2018, 2, 12, 8, 0, 0));
        dt.Rows.Add(null, "Lester", "Lam",
            new DateTime(2018, 9, 8, 9, 0, 0));
        dt.Rows.Add(null, "Claire", "Cowan",
            new DateTime(2018, 1, 1, 10, 0, 0));
        dt.Rows.Add(null, "Karen", "Payne",
            new DateTime(2022, 1, 1, 10, 0, 0));

        return dt;
    }
} 
#endregion

There was a mention of using a DateOnly, but so no gain.

private void GetStartDateButton_Click(object sender, EventArgs e)
{
    if (dataGrid.CurrentRow!.Cells["DateItem"].Value.IsNull())
    {
        txtDate.Text = "";
    }
    else
    {
        var dateTime = (((DataRowView)_bindingSource.Current)!)
            .Row.Field<DateTime>("DateItem");

        txtDate.Text = DateOnly.FromDateTime(dateTime).ToString(_dateFormat);
    }
}

CodePudding user response:

You are trying to apply an overload of the ToString method that does not exist on a string.

You must first convert the initial string to DateTime like this:

if(!string.IsNullOrWhiteSpace(dataGrid.Cells[2].Value.ToString()) && DateTime.TryParse(dataGrid.Cells[2].Value.ToString(), out DateTime date))
{
         txtDate.Text = date.ToString("dd/MM/yyyy");
}

An overload of the ToString is available by taking as a parameter the format

CodePudding user response:

You can try using DateOnly.

https://learn.microsoft.com/en-us/dotnet/api/system.dateonly?view=net-6.0

Or convert to string in the desired format output, by using:

ToString(dd/MM/yyyy)

  • Related