0

In my sql i'm using date(Data type). Then in my Form_load:

SqlDataAdapter sda = new SqlDataAdapter("select * from incomes", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item[0].ToString();
dataGridView1.Rows[n].Cells[1].Value = item[1].ToString();
dataGridView1.Rows[n].Cells[2].Value = item[2].ToString();
dataGridView1.Rows[n].Cells[3].Value = item[3].ToString();
dataGridView1.Rows[n].Cells[4].Value = item[4].ToString();

But in the datagridview column doesnt show only date it shows date with time. How can i change it, only to date?

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Dim
  • 122
  • 11

2 Answers2

1

You could explicitly parse your Date using the DateTime.Parse() or Convert.ToDateTime() methods and then use the ToString() method to format it:

dataGridView1.Rows[n].Cells[0].Value = Convert.ToDateTime(item[0]).ToString("MM/dd/yyyy");

You can use any of the available standard or custom formatting strings to output as you need.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
0

Firstly, declare it like this:

 dataGridView1.Format = "dd/MM/yyyy";
 this.date.DefaultCellStyle = dataGridView1;

or, set it from the designer Designer

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32