Raize Ahamed
Raize Ahamed

Reputation: 385

how to set custom date format for specific column in datagridview? - c#

I am having many fields in a table.

Each field contains different data types i.e text, number, date/time.

I am displaying the table in datagridview.

Default date format is M/dd/yyyy.

But i want the date to display in a custom format i.e dd/M/yyyy in a datagridview.

I tried changing the format in default cell style as dd/M/yyyy. Now the date column works fine but the number columns are displaying as dd/M/yyyy.

string connetionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=database1.mdb;";
// select * from ~tablename~; 
string sql = "select * from table1";
OleDbConnection connection = new OleDbConnection(connetionString);
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, vehicleno);
dataGridView1.DataSource = ds.Tables[0];
connection.Close();

How do I set custom date format for specific column in datagridview?

Upvotes: 3

Views: 2756

Answers (1)

OhBeWise
OhBeWise

Reputation: 5454

Try this line after binding your data source:

this.dataGridView1.Columns["YourDateCol"].DefaultCellStyle.Format = "dd/MM/yyyy";

Where "YourDateCol" is the name of your date column in the database.

Upvotes: 2

Related Questions