How can i make ColumnHeaderName to HH:mm format?

string columnHeaderName = dgvSchedule.Columns[i].HeaderText.ToString();

       DateTime timeColumn = Convert.ToDateTime(columnHeaderName);

Recommended Answers

All 14 Replies

You want that the ColumnHeader of one column in the DGV shows the time? and the time in thr real time?

You want that the ColumnHeader of one column in the DGV shows the time? and the time in thr real time?

yes

Simple :)
You will have to use a Timer object to update the time in the DGV`s headerText:

System.Windows.Forms.Timer timer1;
        public Form1()
        {
            InitializeComponent();
            dataGridView1.Columns.Add("col1", "");
            dataGridView1.Columns.Add("col2", "Column 2");
            dataGridView1.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            ShowTime();

            timer1 = new Timer();
            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
        }        

        private void timer1_Tick(object sender, EventArgs e)
        {
            ShowTime();
        }

        private void ShowTime()
        {
            string time = String.Format("{0:HH:mm:ss}", DateTime.Now);
            dataGridView1.Columns[0].HeaderText = time;
        }
static void Main(string[] args)
        {

            DateTime dt = DateTime.Now;

            string Dstr = String.Format("{0:t}", dt);
            Console.WriteLine(Dstr);
            // This is the same:
            Console.WriteLine("{0:t}", dt);
 
            Console.ReadKey();
}
static void Main(string[] args)
        {

            DateTime dt = DateTime.Now;

            string Dstr = String.Format("{0:t}", dt);
            Console.WriteLine(Dstr);
            // This is the same:
            Console.WriteLine("{0:t}", dt);
 
            Console.ReadKey();
}

its not that i want to convert from datetime to string.. I want to convert my string to datetime.. and the datetime has a format of HH:mm

Your first post states :

How can i make ColumnHeaderName to HH:mm format?

Unless you cannot come up with some sort of year,month and day, I believe you gonna have a hard time : http://msdn.microsoft.com/en-us/library/system.datetime(v=VS.100).aspx

thats bad :( as i dont have 2 much time

since you are only displaying the time, add a fake date (1/1/2000 for example).

have you checked my solution?

have you checked my solution?

yes i did i did not try it as i already entered the values in the datagridview frm the design

You better try it, you will see it works.

btw, can I see your code?

You better try it, you will see it works.

btw, can I see your code?

yes oFcourse.. ill try it l8er so i will do more things

public void FillSechedule()
        {
            dgvSchedule.TopLeftHeaderCell.Value = "Time\nTable";

            ManagerTables mgrTables = new ManagerTables();
            List<Table> listOfAllTables = mgrTables.GetAllTables();

            ManagerTableReservation mgrTableReservation = new ManagerTableReservation();
            ManagerReservations mgrReservation = new ManagerReservations();

            foreach(Table t in listOfAllTables) 
            {
                int n = dgvSchedule.Rows.Add();
                dgvSchedule.Rows[n].HeaderCell.Value = t.TableID.ToString(); //"Table " + t.TableID.ToString();
            }

            for (int i = 0; i < listOfAllTables.Count; i++) //used to loop all tables
            {
                string rowHeaderName = dgvSchedule.Rows[i].HeaderCell.Value.ToString();
                int tableNo = Convert.ToInt32(rowHeaderName);

                List<TableReservation> listOfTableReservation = mgrTableReservation.GetReservationsByTableID(tableNo);

                foreach (TableReservation tr in listOfTableReservation)//used to llop through all table reservations
                {
                    Reservation res = mgrReservation.GetReservationsByReservationID(tr.ReservationID);

                    DateTime dateSelected = Convert.ToDateTime(dtpDateTimeSchedule.Value.ToString("dd/MM/yyyy"));
                    DateTime resDate = Convert.ToDateTime(res.DateTimeOfEvent.ToShortDateString());

                    if (dateSelected==resDate)//(res.DateTimeOfEvent.ToString("MM/dd/yyyy") == dateSelected.ToString())
                    {
                        for (int a = 0; a < dgvSchedule.Rows.Count; a++)//Loop thorough rows
                        {

                            Table t = listOfAllTables[i];

                            if (t.TableID == tableNo)
                            {
                                for (int b = 0; b < dgvSchedule.Columns.Count; b++)//Loop thorough columns
                                {
                                    string columnHeaderName = dgvSchedule.Columns[i].HeaderText.ToString();
                                    

                                    DateTime timeColumn = Convert.ToDateTime(String.Format("{0:t}", columnHeaderName));
                                    DateTime dateTimeOfReservation = Convert.ToDateTime(res.DateTimeOfEvent.ToShortTimeString());
                                    DateTime dateTimeOfReservationPlus = Convert.ToDateTime(res.DateTimeOfEvent.AddMinutes(30).ToShortTimeString());

                                    
                                    if (dateTimeOfReservation >= timeColumn && dateTimeOfReservationPlus <= timeColumn && res.Cancelled.Equals(false))
                                    {
                                        dgvSchedule.Rows[a].Cells[b].Style.BackColor = Color.Chartreuse;//.color
                                        dgvSchedule.Rows[a].Cells[b].Value = res.CustomerID.ToString();
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                    //Foreach ohra bix igib column column tad dgv
                    //loop all reservations and check if time is == torow time, If yes highlight cell.. else continue
                }

            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.