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
VulpesPosted Aug 31, 2011, 2:37 PM
It should, of course, be:
DataGridViewRow row = dataGridView1.Rows[i];
mike DelvottiPosted Aug 31, 2011, 2:44 PM
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.
mike DelvottiPosted Aug 31, 2011, 2:24 PM
Cannot implicitly convert type 'System.Windows.Forms.DataGridViewRow' to 'System.Data.DataRow'
VulpesPosted Aug 31, 2011, 2:16 PM
mike DelvottiPosted Aug 31, 2011, 2:11 PM
If i use that directly in my function it won't debug as "row" throws an error?
VulpesPosted Aug 31, 2011, 1:50 PM
mike DelvottiPosted Aug 31, 2011, 1:40 PM
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.
VulpesPosted Aug 31, 2011, 11:45 AM
Mahesh ChandPosted Aug 31, 2011, 11:43 AM
Mahesh ChandPosted Aug 31, 2011, 11:41 AM
Something like this:
DataTable.Select('ColumnName='+ TodaysDate)
mike DelvottiPosted Aug 31, 2011, 11:35 AM
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();
}
VulpesPosted Aug 31, 2011, 9:02 AM
if (obj == null) continue;
Prabhu RajaPosted Aug 31, 2011, 8:01 AM
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();
}