简体   繁体   中英

show the most recent date of a datagridview

i have a datagridview with a column filled with dates, i want a textbox to show only the most recent one, what i did wasis to put a very early date in the textbox(01/01/0000) and then run the whole datagrid in a for loop but it gives me this error

String was not recognized as a valid DateTime

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

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            DateTime date = Convert.ToDateTime(dataGridView1.Rows[i].Cells[14].Value.ToString());
            DateTime date1 = DateTime.ParseExact(dataRecente.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);
            if (date > date1)
            {
                dataRecente.Text = Convert.ToString(date);
            }
        }

Why not fetch the date straight from the source for the datagridview , presumably a datatable or dataset ? If the underlying data changes later for example due to user input, the datatable can trigger an event , so you can refresh the textbox.

The loop is totally unnecessary, so is the parsing and conversion. If you have valid datetime values they can easily be sorted.

Also, you are assuming that the date will always be in a certain column but the user could drag columns and change the display order, and your code will crash miserably.

So instead of:

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

you can have:

dataGridView1.Columns[datagridview1.Columns["colDate"].Index].DefaultCellStyle.Format = "dd/MM/yyyy";

(not tested but should be very close if not correct)

Each column in the datagrid is an control , and hopefully you've given them meaningful column names (in this example: colDate ).

You could fetch the max date from your datatable using the DataTable.Compute function like:

table.Compute("MAX([DateColumn 1])", "");

(Do handle the case the datatable is empty, or expect the return value to be null)

Other alternatives: use the DataTable.Select function or LINQ .

i found another...incredibly brute and unelagent solution, since i do not care how the interface looks and as a noob i know very little of dataset etc i have created three datagrid with the one column for days one for months and one for years. the script then sort by year, the ones on top with the same year go into the second datagridview and are sorted by month, and the script repeat for the day, in the end the first row of the last datagris contain the most recent date. As said this is incredibly brutish and could seems a neanderthalian solution...but it worked.

 for (int i2 = 0; i2 < date.Rows.Count; i2++)
        {
            int inizio = Int32.Parse(date.Rows[i2].Cells[1].Value.ToString());
            int fine = Int32.Parse(date.Rows[i2].Cells[2].Value.ToString());
            for (int i = inizio; i < fine; i++)
            {
                dataSortingGrid.Rows.Add();
                int startIndex = 0;
                int length = 2;
                string dataIntera = dataGridView1.Rows[i].Cells[14].Value.ToString();
                int i3 = dataSortingGrid.Rows.Count - 1;
                String giorni = dataIntera.Substring(startIndex, length);
                dataSortingGrid.Rows[i3].Cells[0].Value = giorni;
                startIndex = 3;
                length = 2;
                String mesi = dataIntera.Substring(startIndex, length);
                dataSortingGrid.Rows[i3].Cells[1].Value = mesi;
                startIndex = 6;
                length = 4;
                String anni = dataIntera.Substring(startIndex, length);
                dataSortingGrid.Rows[i3].Cells[2].Value = anni;
                dataSortingGrid.Rows[i3].Cells[3].Value = dataGridView1.Rows[i].Cells[14].Value.ToString();
            }
            dataSortingGrid.Sort(dataSortingGrid.Columns[2], System.ComponentModel.ListSortDirection.Descending);
            //mesi

            int nRowMesi = Int32.Parse(dataSortingGrid.Rows.Count.ToString());
            for (int i = 0; i < nRowMesi; i++)
            {
                if (i < 1)
                {
                    dataSortingGridMesi.Rows.Add();
                    dataSortingGridMesi.Rows[i].Cells[0].Value = dataSortingGrid.Rows[i].Cells[0].Value;
                    dataSortingGridMesi.Rows[i].Cells[1].Value = dataSortingGrid.Rows[i].Cells[1].Value;
                    dataSortingGridMesi.Rows[i].Cells[2].Value = dataSortingGrid.Rows[i].Cells[2].Value;
                    dataSortingGridMesi.Rows[i].Cells[3].Value = dataSortingGrid.Rows[i].Cells[3].Value;

                }
                else
                {
                    int valoreAnniSopra = Convert.ToInt32(dataSortingGrid.Rows[i - 1].Cells[2].Value);
                    int valoreAnniSotto = Convert.ToInt32(dataSortingGrid.Rows[i].Cells[2].Value);
                    if (valoreAnniSotto < valoreAnniSopra)
                    {
                        break;
                    }
                    dataSortingGridMesi.Rows.Add();

                    dataSortingGridMesi.Rows[i].Cells[0].Value = dataSortingGrid.Rows[i].Cells[0].Value;
                    dataSortingGridMesi.Rows[i].Cells[1].Value = dataSortingGrid.Rows[i].Cells[1].Value;
                    dataSortingGridMesi.Rows[i].Cells[2].Value = dataSortingGrid.Rows[i].Cells[2].Value;
                    dataSortingGridMesi.Rows[i].Cells[3].Value = dataSortingGrid.Rows[i].Cells[3].Value;

                }
            }
            dataSortingGridMesi.Sort(dataSortingGridMesi.Columns[1], System.ComponentModel.ListSortDirection.Ascending);

            //giorni

            int nRowGiorni = Int32.Parse(dataSortingGridMesi.Rows.Count.ToString());
            for (int i = 0; i < nRowGiorni; i++)
            {
                if (i < 1)
                {
                    dataSortingGridGiorni.Rows.Add();
                    dataSortingGridGiorni.Rows[i].Cells[0].Value = dataSortingGridMesi.Rows[i].Cells[0].Value;
                    dataSortingGridGiorni.Rows[i].Cells[1].Value = dataSortingGridMesi.Rows[i].Cells[1].Value;
                    dataSortingGridGiorni.Rows[i].Cells[2].Value = dataSortingGridMesi.Rows[i].Cells[2].Value;
                    dataSortingGridGiorni.Rows[i].Cells[3].Value = dataSortingGridMesi.Rows[i].Cells[3].Value;

                }
                else
                {
                    int valoreMesiSopra = Convert.ToInt32(dataSortingGridMesi.Rows[i - 1].Cells[1].Value);
                    int valoreMesiSotto = Convert.ToInt32(dataSortingGridMesi.Rows[i].Cells[1].Value);
                    if (valoreMesiSotto < valoreMesiSopra)
                    {
                        break;
                    }
                    dataSortingGridGiorni.Rows.Add();

                    dataSortingGridGiorni.Rows[i].Cells[0].Value = dataSortingGridMesi.Rows[i].Cells[0].Value;
                    dataSortingGridGiorni.Rows[i].Cells[1].Value = dataSortingGridMesi.Rows[i].Cells[1].Value;
                    dataSortingGridGiorni.Rows[i].Cells[2].Value = dataSortingGridMesi.Rows[i].Cells[2].Value;
                    dataSortingGridGiorni.Rows[i].Cells[3].Value = dataSortingGridMesi.Rows[i].Cells[3].Value;

                }
            }
            dataSortingGridGiorni.Sort(dataSortingGridGiorni.Columns[0], System.ComponentModel.ListSortDirection.Descending);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM