Home » VB.NetRSS

How to Format Data in the Windows Forms DataGridView Control

hi all

i want to format columns of my datagridview.for example 20100630 ------>2010/06/30

this code doesnot work for me:

dataGridView1.Columns[2].DefaultCellStyle.Format = "YYYY/MM/dd";
thsnk s

 

 

20 Answers Found

 

Answer 1

Hello Babak_bsn,

 

    Just change YYYY into yyyy.

    dgtest.Columns["DOB"].DefaultCellStyle.Format = "yyyy/MM/dd";

  kindly, let me know if you need further help.

Thanks,

Paras Sanghani

Mark As answer if it helped you.

 

Answer 2

You could always replace one string with the other...

string mystring=20100630;

mystring.Insert(4,"/");

......

You get the drift

Regards

 

Answer 3

Thank you friends

here is my code,i changed my code but it doesn't work

SqlCommand cmd = new SqlCommand("SELECT * FROM City", new Setting().Getconn());
      SqlDataReader dr = cmd.ExecuteReader();
      DataTable dt = new DataTable();
      dt.Load(dr);
      dataGridView2.DataSource = dt;
      dataGridView2.Columns[2].DefaultCellStyle.Format = "yyyy/MM/dd";
      
Thnaks

 

Answer 4

Replace the string as I said or tell the database how do you want the date or change the way the database store the date.

Regards

PS: I wrote this but it did not show :|

 

Answer 5

This way as you are trying to do is impossible. 

You will have to do it row by row when you are populating DGW (from dataTable - this is the best and simpliest way of holding data  in the code):

lets say when you populate the dateTimes in DGW`s column2:

 

dataGridView1.Rows.Clear();
  int row = -1;
  int column = 0;
  foreach (DataRow dr in table.Rows)
  {
  row++;
  column = 0;
  dataGridView1.Rows.Add();
  dataGridView1.Rows[row].Cells[column].Value = dr[0].ToString(); //the 1stcolumn of dataTable you want to populate (i dont know which it is)
  column++;
  DateTime myDate = Convert.ToDateTime(dr[1]);
  string dateString = myDate.ToString('d'); //FORMAT OF: dd//MM/yyyy
  dataGridView1.Rows[row].Cells[column].Value = dateString;
  }

or try this, that it will suit your needs:

 

string dateString = myDate.ToString("yyyy/MM/dd"); //FORMAT OF: dd//MM/yyyy

 

... but there might be one problem. Where from you you get this number? Is a dateTime value or is an integer?

 

Answer 6

Thank you Mitja

that's integer value that save in Table in DB

 

Answer 7

Thank you Serguey

i change the code as you say but it doesn't work too

DataGridViewCellStyle style=new DataGridViewCellStyle();
      style.Format.Insert(4,"/");
      dataGridView2.Columns[2].DefaultCellStyle = style; 

 

Answer 8

The problem is that the value 20100630 is not a valid DateTime, I mean the C# program wont recognize it as a valid date time.

So if you have any other option is ok, but if not, I can suggest you how to salve this problem:

 

int intDate = 20100630;
int a = 2010;
int b = 6;
int c = 30;
DateTime newDate = new DateTime (a,b,c);
string dateString= newDate.ToString("yyyy/MM/dd");

 

So now you have your time of a time :) and you just do:

dataGridView1.Rows[row].Cells[column].Value = dateSting;

 

btw, one friendly suggestion, please change the type of the "date"  in your database. Please use datetime". Would be way easier for you to use it.

thats all :)

enjoy

Mitja

 

Answer 9

Ouch, you are storing as an int in the database?

It would be easier to use datetime, then you could just tell the database to store in the yyyy/MM/dd format  and you wouldn't need a conversion.

borrowing from Mitja code

int intDate = 20100630;

string mystring=intDate.ToString();

mystring.Insert(4,"/");

mystring.Insert(8,"/");

Regards

PS: I'm not sure of the insert index, check them before hand

 

 

Answer 10

Do you know how to seperate the int 20100630 into 3 peaces (for year, month and day)?

And, if you always have 8 characters for "date"? I mean if you aways write integer with 8 characters, for example  if the day is 1st of July 2010 - do you write it as 20100701 or 201071? There is a lot of differences between these two integers. Specially if you want to seperate them into 3 peaces. 

If you know what I mean.

btw, I did get your previous post, and what about myString.Insert() method, whats this got to do here?

 

Answer 11

Are you asking me or him?  Sorry I'm a bit confused.

Regards

 

Answer 12

Ups, sorry, didnt realize you posted that post.

ok, so im asking you, if you are the owner of it. Just curious.

 

Answer 13

Hi experts thank you for your useful answers

i change type of my field into navarchar(50) (20100630 is now string)

then write this code:

DataGridViewCellStyle style=new DataGridViewCellStyle();
      dataGridView2.DataSource = dt;
      style.Format.Insert(3, "/");
      style.Format.Insert(5, "/");
      style.Format.Insert(7, "/");
      dataGridView2.Columns[2].DefaultCellStyle.Format = style;
but doesn't work. i want to show 2010/06/30 to user

 

Answer 14

1st of all you should change to DATETIME!

then when you change datime value to string you have to seperate value to 3 pieces (year, month, day). and only then you can change it to you type of string.

Let me try to do the code. wait

 

Answer 15

Ok, this is it. It works fine for me... this is the FULL CODE you can use:

dataGridView1.Rows.Clear();
 int row = -1;
 int column = 0;
 foreach (DataRow dr in table.Rows)
 {
 row++;
 column = 0;
 dataGridView1.Rows.Add();
 dataGridView1.Rows[row].Cells[column].Value = dr[0].ToString(); //the 1stcolumn of dataTable you want to populate (i dont know which it is)
 column++;
 int intDate = Convert.ToInt32(dr[1]);
  char[] charABC = Convert.ToString(intABC).ToCharArray();
  string year = charABC[0].ToString() + charABC[1].ToString() + charABC[2].ToString() + charABC[3].ToString();
  string month = charABC[4].ToString() + charABC[5].ToString();
  string day = charABC[6].ToString() + charABC[7].ToString();

  string strDate = year + "/" + month + "/" + day;
  dataGridView1.Rows[row].Cells[column].Value = strDate;
 string dateString = myDate.ToString('d'); //FORMAT OF: dd//MM/yyyy
 dataGridView1.Rows[row].Cells[column].Value = dateString;
 }

I just hope you insert 8 characters all the time. I mean if the month or the day has one number, you insert for January 01, and not 1. Be careful with that!

Hope I helped you.

Mitja

 

Answer 16

One more question, can you mark this thread as Helpful on my name? No one so far hasn`t done this for me :) That green arrow just bellow my name.

thx

If you have any other question, dont hasitate.

Best regards

Mitja

 

Answer 17

There is no need to use that roundabout way of converting a string to a char[] to a string

Granted it would be better to simply change the type in the database and tell it how to store the date.

using insert on the string work

using System;
  public class SampleClass
  {
    public staticvoid Main ( )
    {
      int max=20100615;
      string mystring=max.ToString();
      mystring=mystring.Insert(4,"/");
      mystring=mystring.Insert(7,"/");
      System.Console.WriteLine(mystring);
      System.Console.Read();
    }
  }

 

Answer 18

 

      int max=20100615;
      string mystring=max.ToString();
      mystring=mystring.Insert(4,"/");
      mystring=mystring.Insert(7,"/");
      System.Console.WriteLine(mystring);

wow, until now I didnt know for this methos Insert. Great, I`ll remember that.

cheers Serguey123 :)

 

Answer 19

friends

Why this doesn't work for me:

foreach(DataRow dtr in dt.Rows)
      {
        int date=Convert.ToInt32( dtr[2]);
        string mystring = date.ToString();

        mystring.Insert(4, "/");
        mystring.Insert(7, "/");
        MessageBox.Show(mystring);

      }

 

Answer 20

foreach(DataRow dtr in dt.Rows)
   {
    string mystring = (dtr[2]).ToString();

    mystring =mystring.Insert(4, "/");
    mystring =mystring.Insert(7, "/");
    MessageBox.Show(mystring);

   }
Regards
 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter