Question

I have a dataGridView that has a column called Date Due which is of type System.DataTime. I want to put these dates in a monthCalendar. Do I have to iterate throught the column? Do I have to cast it?

Was it helpful?

Solution

to get date from each column YES you have to iterate through all rows in column. As you are displaying date as text you will need to parse using DateTime.Parse or DateTime.TryParse methods.

EDIT:- supposing this is a forms application. I added a DataGridView, a MonthCalendar and a Button. I was able to get dates from gridview and bold the selected dates in month cal.

public Form2()
{
    InitializeComponent();

    CreateData();
}

private void CreateData()
{
    var dtb = new DataTable();
    dtb.Columns.Add("Column1");
    dtb.Columns.Add("Column2");
    dtb.Columns.Add("Column3");

    var dt = DateTime.Now;
    var rand = new Random();

    for (var i = 0; i < 10; i++)
    {
        var r = dtb.NewRow();

        r.ItemArray = new object[] {dt.ToString("ddd"), dt.ToString("MMM"), dt};
        dt = dt.AddDays(rand.Next(30));

        dtb.Rows.Add(r);
    }

    dataGridView1.DataSource = dtb;
}

private void button1_Click(object sender, EventArgs e)
{
    var bds = new List<DateTime>();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        DateTime dt;
        var b = DateTime.TryParse(row.Cells[row.Cells.Count - 1].Value+"", out dt);
        if(!b) continue;

        bds.Add(dt);
    }

    cal.BoldedDates = bds.ToArray();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top