locked
DateTime format in DataTable RRS feed

  • Question

  • Hi people,

     

    I have DateTime column in my DataTable which is the data source for DataGridView. The question is how do I format my DateTime string to e.g. (01 January 2007) rather than 01/01/2007 which I believe is the default format.

     

    Here is my code:

     

    //Create DateTime column

    column = new DataColumn();

    column.DataType = System.Type.GetType("System.DateTime");

    column.ColumnName = "Date";

    column.Caption = "Date";

    column.ReadOnly = true;

    column.Unique = false;

    newDT.Columns.Add(column);

     

     

    <this is where i try to format my DateTime before being bounded to datagridview>

    dateString = dt.Rows[count][5].ToString();//DateTime

    row["Date"] = DateTime.ParseExact(dateString, "dd MMMM yyyy",null);

    newDT.Rows.Add(row);

     

    THank you

    Wednesday, May 23, 2007 1:13 PM

Answers

  • This worked for me:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) {
          DataGridViewCellStyle style = new DataGridViewCellStyle();
          style.Format = "dd MMMM yyyy";
          style.NullValue = "";
          dataGridView1.Columns[0].DefaultCellStyle = style;
          dataGridView1.Rows.Add();
          dataGridView1.Rows[0].Cells[0].Value = DateTime.Now;
        }
      }

    Thursday, May 24, 2007 3:47 PM
    Moderator

All replies

  • Try setting

     

    column.DefaultCellStyle.Format

     

    to one of the standard datatime formats, or to a custom form. If you look up the Format property in the help system it will link you to the availavle formats and to creating custom formats.

     

    Never tried it myself but seems like ti should do the trick.

    Wednesday, May 23, 2007 4:43 PM
  • argh... its still not working.

    btw, this is my code where fill my datagridview along with the idea of using the above method.

    DGview.DataSource = func.filledDataTable();
    DGview.Columns["Date"].DefaultCellStyle.Format = "dd MMMM yyyy";

    Can someone please help me. Thx
    Thursday, May 24, 2007 2:36 PM
  • This worked for me:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) {
          DataGridViewCellStyle style = new DataGridViewCellStyle();
          style.Format = "dd MMMM yyyy";
          style.NullValue = "";
          dataGridView1.Columns[0].DefaultCellStyle = style;
          dataGridView1.Rows.Add();
          dataGridView1.Rows[0].Cells[0].Value = DateTime.Now;
        }
      }

    Thursday, May 24, 2007 3:47 PM
    Moderator