Click here to Skip to main content
15,909,953 members
Home / Discussions / C#
   

C#

 
Questionquery wizard Pin
mohammad alnoed24-Jul-09 5:55
mohammad alnoed24-Jul-09 5:55 
AnswerRe: query wizard Pin
Abhijit Jana24-Jul-09 6:26
professionalAbhijit Jana24-Jul-09 6:26 
GeneralRe: query wizard Pin
mohammad alnoed24-Jul-09 8:13
mohammad alnoed24-Jul-09 8:13 
GeneralRe: query wizard Pin
Abhijit Jana24-Jul-09 8:25
professionalAbhijit Jana24-Jul-09 8:25 
QuestionDoes anyone know any open source C# implementation of XAML Object Mapping Specification? Pin
Michael Sync24-Jul-09 5:35
Michael Sync24-Jul-09 5:35 
AnswerRe: Does anyone know any open source C# implementation of XAML Object Mapping Specification? Pin
BillWoodruff24-Jul-09 22:25
professionalBillWoodruff24-Jul-09 22:25 
GeneralRe: Does anyone know any open source C# implementation of XAML Object Mapping Specification? Pin
Michael Sync24-Jul-09 22:34
Michael Sync24-Jul-09 22:34 
QuestionProblem in datagridview datetimepicker column. Pin
priyamtheone24-Jul-09 5:24
priyamtheone24-Jul-09 5:24 
I'm trying to make a column of a datagridview to act like a datetimepicker column (C#.Net 2005). These are the behaviours that the dgv should have:
1) Initially all the cells of the dtp column should be blank unless they are filled by the user.
2) As soon as the user enters a cell, the dtp control should appear as the editing control of that cell. If there's a value in the cell beforehand, that value is set as the value of the dtp editing control and it is checked, else the dtp editing control remains unchecked.
3) If the user selects a date from the dtp editing control and moves to another cell the value of the dtp should be set to the previous cell in string format (In that way we can define custom format for the cell e.g. dd MMM yyyy).
4) If the user unchecks the dtp editing control while it's on a cell the value of that cell should be set to null irrespective of whether the cell had a value beforehand.

I managed everything till point 3. But can't work out with point 4. Cannot set the value of a cell to null when its dtp editing control is unchecked. There's a way to do it by handling the Datagridview_CellValidating event. But in that case if the datagridview is data bound, u need to uncheck the editing control once, goto another cell, come back to the previous one and uncheck it once again to set its cell value to null. I want to know if there's any way to do it through the derived classes or their class template that I'm using so that I wont have to use that event and everything can be done through the class template only. Please help. My code follows:

Code for the template (I'm using a code file) containing all the derived classes:
using System;
using System.Windows.Forms;

namespace DGV_DateTimePicker
{
    public class DateTimePickerColumn : DataGridViewColumn
    {
        public DateTimePickerColumn()
            : base(new DateTimePickerCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a DateTimePickerCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(DateTimePickerCell)))
                {
                    throw new InvalidCastException("Must be a DateTimePickerCell");
                }
                base.CellTemplate = value;
            }
        }
    }

    public class DateTimePickerCell : DataGridViewTextBoxCell
    {

        public DateTimePickerCell()
            : base()
        {
            // Use the custom defined date format.
            this.Style.Format = "dd MMM yyyy";
        }

        public override void InitializeEditingControl(int rowIndex, object
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
                dataGridViewCellStyle);

            DateTimePickerEditingControl ctl = (DateTimePickerEditingControl)DataGridView.EditingControl;
            DateTime d;
            ctl.Value = DateTime.TryParse((Value ?? "").ToString(), out d) ? d : DateTime.Now;

            /*Check whether the datagridview is databound/unbound. In both cases if the value of the cell
		 isn't null then check the DateTimePickerEditing Control else uncheck it.*/
            if (ctl.EditingControlDataGridView.CurrentCell.OwningColumn.IsDataBound)
                ctl.Checked = Value.ToString() == "" ? false : Value == null ? false : true;
            else
                ctl.Checked = Value == null ? false : true;
        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that DateTimePickerCell uses.
                return typeof(DateTimePickerEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that DateTimePickerCell contains.
                return typeof(DateTime);
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                //Return null as the default value for new row.
                return null;
            }
        }
    }

    class DateTimePickerEditingControl : DateTimePicker, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;

        public DateTimePickerEditingControl()
        {
            this.Format = DateTimePickerFormat.Custom;
            this.CustomFormat = "dd MMM yyyy";
            this.ShowCheckBox = true;
            this.Checked = false;
        }

        // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
        // property.
        public object EditingControlFormattedValue
        {
            get
            {
                return this.Value.ToShortDateString();
            }
            set
            {
                if (value is String)
                {
                    this.Value = DateTime.Parse((String)value);
                }
            }
        }

        // Implements the 
        // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
        public object GetEditingControlFormattedValue(
            DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        // Implements the 
        // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
        public void ApplyCellStyleToEditingControl(
            DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
            this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
        }

        // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
        // property.
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
        // method.
        public bool EditingControlWantsInputKey(
            Keys key, bool dataGridViewWantsInputKey)
        {
            // Let the DateTimePicker handle the keys listed.
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:
                    return true;
                default:
                    return !dataGridViewWantsInputKey;
            }
        }

        // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
        // method.
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparation needs to be done.
        }

        // Implements the IDataGridViewEditingControl
        // .RepositionEditingControlOnValueChange property.
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlDataGridView property.
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlValueChanged property.
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingPanelCursor property.
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        protected override void OnValueChanged(EventArgs eventargs)
        {
            // Notify the DataGridView that the contents of the cell
            // have changed.
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnValueChanged(eventargs);
        }
    }
}


Code for the form containing the datagridview:
private void Form1_Load(object sender, EventArgs e)
{
    DateTimePickerColumn col = new DateTimePickerColumn();
    dataGridView1.Columns.Add(col);
    dataGridView1.RowCount = 5;
}


Reference from MSDN 2005- How to: Host Controls in Windows Forms DataGridView Cells
AnswerRe: Problem in datagridview datetimepicker column. Pin
Muhammad Mazhar24-Jul-09 5:47
Muhammad Mazhar24-Jul-09 5:47 
GeneralRe: Problem in datagridview datetimepicker column. Pin
priyamtheone24-Jul-09 6:55
priyamtheone24-Jul-09 6:55 
QuestionCreating a completely unclickable WebBrowser Pin
SimpleData24-Jul-09 3:29
SimpleData24-Jul-09 3:29 
AnswerRe: Creating a completely unclickable WebBrowser Pin
musefan24-Jul-09 3:33
musefan24-Jul-09 3:33 
AnswerRe: Creating a completely unclickable WebBrowser Pin
Mike Ellison24-Jul-09 3:42
Mike Ellison24-Jul-09 3:42 
GeneralRe: Creating a completely unclickable WebBrowser Pin
SimpleData24-Jul-09 4:08
SimpleData24-Jul-09 4:08 
GeneralRe: Creating a completely unclickable WebBrowser Pin
Mike Ellison24-Jul-09 4:23
Mike Ellison24-Jul-09 4:23 
GeneralRe: Creating a completely unclickable WebBrowser Pin
SimpleData24-Jul-09 4:28
SimpleData24-Jul-09 4:28 
GeneralRe: Creating a completely unclickable WebBrowser Pin
Mike Ellison24-Jul-09 4:33
Mike Ellison24-Jul-09 4:33 
GeneralRe: Creating a completely unclickable WebBrowser Pin
musefan24-Jul-09 4:46
musefan24-Jul-09 4:46 
GeneralRe: Creating a completely unclickable WebBrowser Pin
SimpleData24-Jul-09 6:01
SimpleData24-Jul-09 6:01 
AnswerRe: Creating a completely unclickable WebBrowser Pin
Abhijit Jana24-Jul-09 3:46
professionalAbhijit Jana24-Jul-09 3:46 
GeneralRe: Creating a completely unclickable WebBrowser Pin
0x3c024-Jul-09 4:06
0x3c024-Jul-09 4:06 
AnswerRe: Creating a completely unclickable WebBrowser Pin
Luc Pattyn24-Jul-09 4:09
sitebuilderLuc Pattyn24-Jul-09 4:09 
GeneralRe: Creating a completely unclickable WebBrowser Pin
SimpleData24-Jul-09 4:13
SimpleData24-Jul-09 4:13 
AnswerRe: Creating a completely unclickable WebBrowser Pin
Eddy Vluggen24-Jul-09 4:09
professionalEddy Vluggen24-Jul-09 4:09 
GeneralRe: Creating a completely unclickable WebBrowser Pin
SimpleData24-Jul-09 4:11
SimpleData24-Jul-09 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.