views:

78

answers:

1

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?

+1  A: 

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();
}
TheVillageIdiot
Didn't work. I already have a DataSet as the DataGrid's DataSource. Is there an easier way?
Mohit Deshpande
I will also be using the `MonthCalendar.UpdateBoldedDates()` method when a new row is added. There is no need for a button.
Mohit Deshpande
Then you ignore the creation of `DataTable` and binding stuff. When new row is added you can handle that event and get data from the corresponding column.
TheVillageIdiot
Or better still post some code so that I have an Idea what and how your stuff is now.
TheVillageIdiot