0

I have a DataGridView called DGVpresale, it has all the data from a DataTable called presale

Here's how I'm filling it:

private void fillDGVpresale()
{
    try
    {
        DBconnection.Open();
    }
    catch 
    { }

    string  sqlQuery = "SELECT * FROM presale";
    using (MySqlDataAdapter da = new MySqlDataAdapter(sqlQuery, DBconnection))
    {
        using (DataTable dt = new DataTable())
        {
            da.Fill(dt);
            DGVpresale.DataSource = dt;
   
            DGVpresale.Columns["SELLER"].HeaderText = "Seller";
            DGVpresale.Columns["CLIENT"].HeaderText = "Client";
            DGVpresale.Columns["REGDATE"].HeaderText = "Registration Date";

            DGVpresale.Columns["SELLER"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            DGVpresale.Columns["CLIENT"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            DGVpresale.Columns["REGDATE"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader;
        }
    }
}

I've translated table & column names and deleted some useless ones

There's a Date Column called REGDATE, which saves all dates in this format: yyyy-MM-dd

I want to change the format inside my DataGridView to dd/MM/yyyy

I tried this:

DGVpresale.Columns["REGDATE"].DefaultCellStyle.Format = "dd/MM/yyyy";

But nothing changes and I can't find any other way to do this.

I don't know if changes anything, but since I had an error when trying to fill the datagridview I had to add this to my DBconnection: convert zero datetime=True

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
LucOliSil
  • 33
  • 6
  • If the `REGDATE` column in the `DataTable` returned from the SQL query is truly a `DateTime` type column… then the code that formats the grid column… “should” work… have you checked to see if the column type is actually a `DateTime` type in addition to checking if there are actual dates in the data? – JohnG Sep 30 '21 at 12:08
  • @JohnG Sorry, yeah I'm tired and changed the wrong table (it was varchar before), so thats why it wasn't working lol, fixed it and it's working just fine, thanks – LucOliSil Sep 30 '21 at 12:21

0 Answers0