This is a migrated thread and some comments may be shown as answers.

FADatePicker Control

1 Answer 131 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tooraj
Top achievements
Rank 1
Tooraj asked on 29 Oct 2010, 03:22 PM
Hi Emanuel,
I tested your code for FADateTimePicker. It embeds the control to the cell.
But still there are some problems:
1. When clicking inside the cell this error occurs: 'Object reference not set to an instance of an object.' this error raises because of this line of your code: dateTimePicker.SelectedDateTime = (DateTime)activeEditor.Value;
2. When clicking the calendar's combobox to open the dropdown list to choose date, the list does not drop down. You must press Enter to open the Date Picker.
3. After selecting the date the contents of the cell must be the same as the host control that is:
 CellContents=FADateTimePicker.SelectedDateTime.
4. User must enter to the cell to make the calendar visible, while I want the calendar to be visible for each cell of the column when the form loads.
Thank you Emanuel, I will not forget your name because of your KIND Attention.
Sincerly, Tooraj Azizi.

1 Answer, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Oct 2010, 09:10 PM
Hello Tooraj,

1. In my example it was not throwing exceptions, it might thrown an exception if you don't have a date for that cell in which case you will need a default NULL DATE.
2. Yes it won't open if the grid is not editing, if the grid is in edit mode the dropdown will open, i will post screenshots :P.
3. it is changing the contents of the cell based on the selecteddatetime
4. I would suggest something else instead of the calendar being always embedded in the cell it is ugly in my point of view and it will create visual glitches (editor controls should not be visible when the grid is not editing). You can try formatting the cell (when not editing) to match the contents of your editor, won't this be enough?

Please take a look at this revised code:
public partial class Form1 : Form
{
    private RadGridView radGridView1;
 
    public Form1()
    {
        InitializeComponent();
        LoadGrid();
    }
 
    private void LoadGrid()
    {
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        radGridView1.CellBeginEdit += new GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);
        radGridView1.CellEndEdit += new GridViewCellEventHandler(radGridView1_CellEndEdit);
        radGridView1.DataSource = new TestsCollection(10);
    }
 
    void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
    {
        if (radGridView1.Columns[e.ColumnIndex].Name == "Date")
        {
            if (this.radGridView1.CurrentCell.Children.Count == 1)
            {
                this.radGridView1.CurrentCell.Children.RemoveAt(0);
            }
        }
    }
 
    void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        if (radGridView1.Columns[e.ColumnIndex].Name == "Date")
        {
            var dateTimePicker = new FADatePicker();
 
            DateTime selectedDate = dateTimePicker.SelectedDateTime;
            if (radGridView1.CurrentCell.Value is DateTime)
            {
                selectedDate = (DateTime)radGridView1.CurrentCell.Value;
            }
 
            dateTimePicker.SelectedDateTime = selectedDate;
            dateTimePicker.SelectedDateTimeChanged += delegate
                {
                    radGridView1.CurrentCell.Value = dateTimePicker.SelectedDateTime;
                };
 
            var item = new RadHostItem(dateTimePicker);
            this.radGridView1.CurrentCell.Children.Add(item);
        }
    }
}
 
#region Helpers
 
internal class Test
{
    public int Id { get; set; }
 
    public string Name { get; set; }
 
    public DateTime Date { get; set; }
 
    public Test(int id, string name, DateTime date)
    {
        this.Id = id;
        this.Name = name;
        this.Date = date;
    }
}
 
internal class TestsCollection : BindingList<Test>
{
    public TestsCollection(int noItems)
    {
        for (int i = 0; i < noItems; i++)
        {
            this.Add(new Test(i, "item" + i, DateTime.Now.AddDays(i)));
        }
    }
}
 
#endregion Helpers


Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Tags
GridView
Asked by
Tooraj
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Share this question
or