I'm trying to count the Date cells in my datagridview, then display the total of cells but only if the date is today.
So in brief, if my datagrid contains 2 dates that are today my label will read "2" really I would like it to say "You have 2 appointments today" in one label.
Below is some code I'm using but it's not counting correctly?
if (row.IsNewRow) break;
object obj = row.Cells[2].Value;
DateTime dt;
if (!DateTime.TryParse(obj.ToString(), out dt)&& dt.Date == DateTime.Today) count++;
{
label2.Text = count.ToString();
}
Loading

VulpesRank #55 · 29.2K reputationPosted Aug 31, 2011, 10:37 AM EDT
It should, of course, be:
DataGridViewRow row = dataGridView1.Rows[i];
Prabhu RajaRank #861 · 2.6K reputationPosted Aug 31, 2011, 4:01 AM EDT
if (this.datagridview1 .Rows.Count > 0) //datagridview1 is your datagridview name
{
int count = 0;
for (int i = 0; i < this.datagridview1.Rows.Count - 1; i++)
{
if (this.datagridview1.Rows[i].Cells[2].Value.Equals(DateTime.Today))
{
++count;
}
}
label2.Text = count.ToString();
}
VulpesRank #55 · 29.2K reputationPosted Aug 31, 2011, 5:02 AM EDT
if (obj == null) continue;
mike DelvottiRank #7,298 · 244 reputationPosted Aug 31, 2011, 7:35 AM EDT
Vulpes yes you are correct the date is in string format like so: dd/MM/yyyy HH:mm:ss I've tried the below but it's still not counting even though I've put 2 test dates for today in cells[2] my label still displays "0"
foreach (DataGridViewRow row in DataGridView1.Rows)
{
int count = 0;
for (int i = 0; i < DataGridView1.Rows.Count; i++)
{
if (row.IsNewRow) break;
object obj = row.Cells[2].Value;
if (obj == null) continue;
DateTime dt;
if (DateTime.TryParse(obj.ToString(), out dt) && dt.Date == DateTime.Today) count++;
}
label2.Text = count.ToString();
}
Mahesh ChandRank #1 · 393.3K reputationPosted Aug 31, 2011, 7:41 AM EDT
Something like this:
DataTable.Select('ColumnName='+ TodaysDate)
Mahesh ChandRank #1 · 393.3K reputationPosted Aug 31, 2011, 7:43 AM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 31, 2011, 7:45 AM EDT
mike DelvottiRank #7,298 · 244 reputationPosted Aug 31, 2011, 9:40 AM EDT
Vulpes, my label still displays "0" it's a stange one that it doesn't event count the rows?
This is most weird as when I run the code below on some text it seems to work so can't understand why in won't count through the date columns and count todays date?
I run the below just to see if it finds the word "Test" and it does find it
foreach (DataGridViewRow row in DataGridView1.Rows)
{
if (row.IsNewRow) break;
object obj = row.Cells[4].Value;
{
string content = obj.ToString();
if (content == "Test")
{
label1.Text = "Test";
}
}
}
Thanks for your help.
VulpesRank #55 · 29.2K reputationPosted Aug 31, 2011, 9:50 AM EDT
mike DelvottiRank #7,298 · 244 reputationPosted Aug 31, 2011, 10:11 AM EDT
If i use that directly in my function it won't debug as "row" throws an error?
VulpesRank #55 · 29.2K reputationPosted Aug 31, 2011, 10:16 AM EDT
mike DelvottiRank #7,298 · 244 reputationPosted Aug 31, 2011, 10:24 AM EDT
Cannot implicitly convert type 'System.Windows.Forms.DataGridViewRow' to 'System.Data.DataRow'
mike DelvottiRank #7,298 · 244 reputationPosted Aug 31, 2011, 10:44 AM EDT
On that note I'm going to turn the PC off and rest my eyes but remain very grateful of your assistance.
Thanks to everybody else too.