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

Adding FADatePickerColumn to RadGridView

8 Answers 257 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tooraj
Top achievements
Rank 1
Tooraj asked on 20 Oct 2010, 11:40 AM
Hi,
I want to add FADatePicker Component to my RadGridView but I don't know how. Here is the code which makes it possible to add FADatePicker to GridView:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using FarsiLibrary.Utils;
using FarsiLibrary.Win.Enums;

namespace FarsiLibrary.Win.Controls
{
    #region DataGridViewDateTimeCell

    public class DataGridViewFADateTimePickerCell : DataGridViewTextBoxCell
    {
        #region Fields

        private DateTime selectedDateTime;
        private static Type valueType = typeof (DateTime);
        private static Type editType = typeof (DataGridViewFADateTimePickerEditor);
        private StringAlignment verticalAlignment;
        private StringAlignment horizontalAlignment;
        private FormatInfoTypes format;
        
        #endregion

        #region Props
        
        /// <summary>
        /// FormatInfoTypes instance, used to format date to string representation.
        /// </summary>
        [Description("FormatInfoTypes instance, used to format date to string representation.")]
        [DefaultValue(typeof(FormatInfoTypes), "ShortDate")]
        public FormatInfoTypes FormatInfo
        {
            get { return format; }
            set
            {
                if (format == value)
                    return;

                format = value;
            }
        }

        public ThemeTypes Theme
        {
            get
            {
                if(DataGridView == null || EditingFADatePicker == null)
                    return ThemeTypes.Office2000;
                
                return EditingFADatePicker.Theme;
            }
            set
            {
                if(DataGridView != null && EditingFADatePicker != null)
                    EditingFADatePicker.Theme = value;
            }
        }

        public override Type EditType
        {
            get { return editType; }
        }

        public override Type ValueType
        {
            get { return valueType; }
        }

        public DateTime SelectedDateTime
        {
            get { return selectedDateTime; }
            set { selectedDateTime = value; }
        }
        
        /// <summary>
        /// Returns the current DataGridView EditingControl as a DataGridViewNumericUpDownEditingControl control
        /// </summary>
        private DataGridViewFADateTimePickerEditor EditingFADatePicker
        {
            get { return DataGridView.EditingControl as DataGridViewFADateTimePickerEditor; }
        }

        
        
        #endregion

        #region Methods

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
                                      DataGridViewElementStates cellState, object value, object formattedValue,
                                      string errorText, DataGridViewCellStyle cellStyle,
                                      DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                      DataGridViewPaintParts paintParts)
        {
            if(DataGridView == null)
                return;


            // First paint the borders and background of the cell.
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts & ~(DataGridViewPaintParts.ErrorIcon | DataGridViewPaintParts.ContentForeground));

            Point ptCurrentCell = DataGridView.CurrentCellAddress;
            bool cellCurrent = ptCurrentCell.X == ColumnIndex && ptCurrentCell.Y == rowIndex;
            bool cellEdited = cellCurrent && DataGridView.EditingControl != null;

            // If the cell is in editing mode, there is nothing else to paint
            if (!cellEdited && value != null && !string.IsNullOrEmpty(value.ToString()))
            {
                PersianDate pd = null;
                if (value is DateTime)
                {
                    pd = (DateTime)value;
                }
                else if (value is string)
                {
                    pd = PersianDate.Parse(value.ToString());
                }

                if (pd != null)
                {
                    using (SolidBrush brFG = new SolidBrush(cellStyle.ForeColor))
                    using (SolidBrush brSelected = new SolidBrush(cellStyle.SelectionForeColor))
                    using (StringFormat fmt = new StringFormat())
                    {
                        fmt.LineAlignment = HorizontalAlignment;
                        fmt.Alignment = VerticalAlignment;
                        fmt.Trimming = StringTrimming.None;
                        fmt.FormatFlags = StringFormatFlags.LineLimit;

                        graphics.DrawString(pd.ToString(), cellStyle.Font, IsInState(cellState, DataGridViewElementStates.Selected) ? brSelected : brFG, cellBounds, fmt);
                    }
                }
            }
            
            if (PartPainted(paintParts, DataGridViewPaintParts.ErrorIcon))
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.ErrorIcon);
        }

        [DefaultValue("Center")]
        public StringAlignment VerticalAlignment
        {
            get { return verticalAlignment; }
            set { verticalAlignment = value; }
        }

        [DefaultValue("Near")]
        public StringAlignment HorizontalAlignment
        {
            get { return horizontalAlignment; }
            set { horizontalAlignment = value; }
        }

        private static bool IsInState(DataGridViewElementStates currentState, DataGridViewElementStates checkState)
        {
            return (currentState & checkState) != 0;
        }

        /// <summary>
        /// Little utility function called by the Paint function to see if a particular part needs to be painted. 
        /// </summary>
        private static bool PartPainted(DataGridViewPaintParts paintParts, DataGridViewPaintParts paintPart)
        {
            return (paintParts & paintPart) != 0;
        }

        /// <summary>
        /// Determines whether this cell, at the given row index, shows the grid's editing control or not.
        /// The row index needs to be provided as a parameter because this cell may be shared among multiple rows.
        /// </summary>
        private bool OwnsEditor(int rowIndex)
        {
            if (rowIndex == -1 || DataGridView == null)
                return false;

            DataGridViewFADateTimePickerEditor editor = DataGridView.EditingControl as DataGridViewFADateTimePickerEditor;
            return editor != null && rowIndex == editor.EditingControlRowIndex;
        }

        internal void SetValue(int rowIndex, DateTime value)
        {
            SelectedDateTime = value;
            if (OwnsEditor(rowIndex))
                EditingFADatePicker.SelectedDateTime = value;
        }
        
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            DataGridViewFADateTimePickerEditor editor = DataGridView.EditingControl as DataGridViewFADateTimePickerEditor;

            if (editor != null)
            {
                editor.RightToLeft = DataGridView.RightToLeft;
                editor.Theme = Theme;
                string formattedValue = initialFormattedValue.ToString();

                if (string.IsNullOrEmpty(formattedValue))
                {
                    editor.SelectedDateTime = DateTime.Now;
                    editor.mv.MonthViewControl.SetNoneDay();
                }
                else
                {
                    editor.SelectedDateTime = DateTime.Parse(formattedValue);
                }
            }
        }

        #endregion
    }

    #endregion

    #region DataGridViewFADateTimePickerEditor

    public class DataGridViewFADateTimePickerEditor : FADatePicker, IDataGridViewEditingControl
    {
        private int editingControlRowIndex;
        private DataGridView editingControlDataGridView;
        private bool valueChanged;

        public DataGridViewFADateTimePickerEditor()
        {
            SelectedDateTimeChanged += new EventHandler(OnInternalSelectedDateTimeChanged);
        }

        private void OnInternalSelectedDateTimeChanged(object sender, EventArgs e)
        {
            EditingControlValueChanged = true;
            NotifyDataGridViewOfValueChange();
        }

        ///<summary>
        ///Changes the control's user interface (UI) to be consistent with the specified cell style.
        ///</summary>
        ///
        ///<param name="dataGridViewCellStyle">The <see cref="T:System.Windows.Forms.DataGridViewCellStyle"></see> to use as the model for the UI.</param>
        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
        }

        ///<summary>
        ///Determines whether the specified key is a regular input key that the editing control should process or a special key that the <see cref="T:System.Windows.Forms.DataGridView"></see> should process.
        ///</summary>
        ///
        ///<returns>
        ///true if the specified key is a regular input key that should be handled by the editing control; otherwise, false.
        ///</returns>
        ///
        ///<param name="keyData">A <see cref="T:System.Windows.Forms.Keys"></see> that represents the key that was pressed.</param>
        ///<param name="dataGridViewWantsInputKey">true when the <see cref="T:System.Windows.Forms.DataGridView"></see> wants to process the <see cref="T:System.Windows.Forms.Keys"></see> in keyData; otherwise, false.</param>
        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            return true;
        }

        ///<summary>
        ///Retrieves the formatted value of the cell.
        ///</summary>
        ///
        ///<returns>
        ///An <see cref="T:System.Object"></see> that represents the formatted version of the cell contents.
        ///</returns>
        ///
        ///<param name="context">A bitwise combination of <see cref="T:System.Windows.Forms.DataGridViewDataErrorContexts"></see> values that specifies the context in which the data is needed.</param>
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            if (mv.MonthViewControl.IsNull)
            {
                return string.Empty;
            }

            return SelectedDateTime.ToString("G");
        }

        ///<summary>
        ///Prepares the currently selected cell for editing.
        ///</summary>
        ///
        ///<param name="selectAll">true to select all of the cell's content; otherwise, false.</param>
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            if (selectAll)
            {
                TextBox.SelectAll();
            }
            else
            {
                TextBox.SelectionStart = TextBox.Text.Length;
            }
        }

        ///<summary>
        ///Gets or sets the <see cref="T:System.Windows.Forms.DataGridView"></see> that contains the cell.
        ///</summary>
        ///
        ///<returns>
        ///The <see cref="T:System.Windows.Forms.DataGridView"></see> that contains the <see cref="T:System.Windows.Forms.DataGridViewCell"></see> that is being edited; null if there is no associated <see cref="T:System.Windows.Forms.DataGridView"></see>.
        ///</returns>
        public DataGridView EditingControlDataGridView
        {
            get { return editingControlDataGridView; }
            set { editingControlDataGridView = value; }
        }

        ///<summary>
        ///Gets or sets the formatted value of the cell being modified by the editor.
        ///</summary>
        ///
        ///<returns>
        ///An <see cref="T:System.Object"></see> that represents the formatted value of the cell.
        ///</returns>
        ///
        public object EditingControlFormattedValue
        {
            get { return SelectedDateTime; }
            set { SelectedDateTime = (DateTime) value; }
        }

        ///<summary>
        ///Gets or sets the index of the hosting cell's parent row.
        ///</summary>
        ///
        ///<returns>
        ///The index of the row that contains the cell, or �1 if there is no parent row.
        ///</returns>
        ///
        public int EditingControlRowIndex
        {
            get { return editingControlRowIndex; }
            set { editingControlRowIndex = value; }
        }

        ///<summary>
        ///Gets the cursor used when the mouse pointer is over the <see cref="P:System.Windows.Forms.DataGridView.EditingPanel"></see> but not over the editing control.
        ///</summary>
        ///
        ///<returns>
        ///A <see cref="T:System.Windows.Forms.Cursor"></see> that represents the mouse pointer used for the editing panel. 
        ///</returns>
        ///
        public Cursor EditingPanelCursor
        {
            get { return Cursors.Default; }
        }

        ///<summary>
        ///Gets or sets a value indicating whether the cell contents need to be repositioned whenever the value changes.
        ///</summary>
        ///
        ///<returns>
        ///true if the contents need to be repositioned; otherwise, false.
        ///</returns>
        ///
        public bool RepositionEditingControlOnValueChange
        {
            get { return true; }
        }

        /// <summary>
        /// Small utility function that updates the local dirty state and 
        /// notifies the grid of the value change.
        /// </summary>
        private void NotifyDataGridViewOfValueChange()
        {
            if (EditingControlValueChanged)
                EditingControlDataGridView.NotifyCurrentCellDirty(true);
        }

        /// <summary>
        /// Property which indicates whether the value of the editing control has changed or not
        /// </summary>
        public virtual bool EditingControlValueChanged
        {
            get { return valueChanged; }
            set { valueChanged = value; }
        }
    }

    #endregion

    #region DataGridViewFADateTimePickerColumn

    [ToolboxBitmap(typeof (DataGridViewFADateTimePickerColumn), "DataGridViewFADateTimePickerColumn.bmp")]
    public class DataGridViewFADateTimePickerColumn : DataGridViewColumn
    {
        #region Ctor

        public DataGridViewFADateTimePickerColumn() : base(new DataGridViewFADateTimePickerCell())
        {
        }

        #endregion

        #region Props

        public ThemeTypes Theme
        {
            get { return FADatePickerCellTemplate.Theme; }
            set { FADatePickerCellTemplate.Theme = value; }
        }

        [DefaultValue("Center")]
        public StringAlignment VerticalAlignment
        {
            get { return FADatePickerCellTemplate.VerticalAlignment; }
            set { FADatePickerCellTemplate.VerticalAlignment = value; }
        }

        [DefaultValue("Near")]
        public StringAlignment HorizontalAlignment
        {
            get { return FADatePickerCellTemplate.HorizontalAlignment; }
            set { FADatePickerCellTemplate.HorizontalAlignment = value; }
        }

        /// <summary>
        /// Represents the implicit cell that gets cloned when adding rows to the grid.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override DataGridViewCell CellTemplate
        {
            get { return base.CellTemplate; }
            set
            {
                DataGridViewFADateTimePickerCell dataGridViewFADateTimePickerCell = value as DataGridViewFADateTimePickerCell;
                if (value != null && dataGridViewFADateTimePickerCell == null)
                    throw new InvalidCastException("Value provided for CellTemplate must be of type DataGridViewRadioButtonElements.DataGridViewRadioButtonCell or derive from it.");

                base.CellTemplate = value;
            }
        }

        /// <summary>
        /// Small utility function that returns the template cell as a DataGridViewRadioButtonCell.
        /// </summary>
        private DataGridViewFADateTimePickerCell FADatePickerCellTemplate
        {
            get { return (DataGridViewFADateTimePickerCell) CellTemplate; }
        }

        /// <summary>
        /// Replicates the MaxDisplayedItems property of the DataGridViewRadioButtonCell cell type.
        /// </summary>
        [Category("Behavior")]
        [Description("The maximum number of radio buttons to display in the cells of the column.")]
        public DateTime SelectedDateTime
        {
            get
            {
                if (FADatePickerCellTemplate == null)
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");

                return FADatePickerCellTemplate.SelectedDateTime;
            }
            set
            {
                if (SelectedDateTime != value)
                {
                    FADatePickerCellTemplate.SelectedDateTime = value;
                    if (DataGridView != null)
                    {
                        DataGridViewRowCollection dataGridViewRows = DataGridView.Rows;
                        int rowCount = dataGridViewRows.Count;

                        for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
                        {
                            DataGridViewRow dataGridViewRow = dataGridViewRows.SharedRow(rowIndex);
                            DataGridViewFADateTimePickerCell dataGridViewCell = dataGridViewRow.Cells[Index] as DataGridViewFADateTimePickerCell;

                            if (dataGridViewCell != null)
                                dataGridViewCell.SelectedDateTime = value;
                        }

                        DataGridView.InvalidateColumn(Index);
                        // TODO: Add code to autosize the column and rows, the column headers,
                        // the row headers, depending on the autosize settings of the grid.
                    }
                }
            }
        }
        
        

        #endregion

        #region Overrides

        /// <summary>
        /// Returns a standard compact string representation of the column.
        /// </summary>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder(64);
            sb.Append("DataGridViewFADateTimePickerCell { Name=");
            sb.Append(Name);
            sb.Append(", Index=");
            sb.Append(Index.ToString(CultureInfo.CurrentCulture));
            sb.Append(" }");
            return sb.ToString();
        }

        #endregion
    }

    #endregion
}

I want to make it possible to add FADatePicker to RadGridView but I don't know how.

Please guide me.
thank you.

8 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 20 Oct 2010, 12:06 PM
hello, 

Are you looking to add a DateTime column with the ability to pick a date time from a calendar as a column to your RadGridView? 
If so, this link will help you to do that. 

If that's not the case, could you explain how you want a DateTime to appear in the RadGridView? 
Regards, 
Richard
0
Tooraj
Top achievements
Rank 1
answered on 20 Oct 2010, 01:08 PM
Hi,
In fact, the code which I sent in previous Post tries to add FADatePicker to DataGrid Component of VS 2010. I do don't know how to this for RadGridView object.  there is a FADAtePickerDataGridViewColumn class in code which is derived from DateGridColumn to make it possible to add it to DatGridView Object. You assume that I have a component which I want add to RadGridView object. Please JUST type the code which works for me.
this code fragment:
   public class DataGridViewFADateTimePickerColumn : DataGridViewColumn//Makes it possible to add to DataGrid Object
    {
        #region Ctor

        public DataGridViewFADateTimePickerColumn() : base(new DataGridViewFADateTimePickerCell())//this creates the FADatePicker as a cell
        {
        }

 Completing my project highly depends on adding this type of coulmn to my RadGridView object.

I'm ready to provide you any information you need to answer.
Thank you.
 
0
Emanuel Varga
Top achievements
Rank 1
answered on 20 Oct 2010, 01:12 PM
Hello Tooraj,

Can you please create a simple windows forms project that uses that column and has that component embedded, and please put it on a sharing site and post the link here,

I will do my best to help, but i think you know that this could be a very long task to achieve.

Best Regards,
Emanuel Varga
0
Tooraj
Top achievements
Rank 1
answered on 20 Oct 2010, 01:16 PM
Hi Emanuel,
the FADatePicker link which you provided to me in a previous thread, is everything I'm talking about. What which this code fragmet does is all I need to replace:
    public class DataGridViewFADateTimePickerColumn : DataGridViewColumn
    {
        #region Ctor

        public DataGridViewFADateTimePickerColumn() : base(new DataGridViewFADateTimePickerCell())
        {
        }
I've just used FarsiLibrary namespace in my project. these namespace has a control named FADatePicker. I want to add this to my project. they have provided the code which makes it possible to add it to a DataGrid object I must change it to add it to  RadGridView object.
I DON'T know how to replace!
thank you.
0
Emanuel Varga
Top achievements
Rank 1
answered on 20 Oct 2010, 01:41 PM
Hello Tooraj,

But can you please do what i've asked? provide a windows forms application with a data grid view that uses those controls?

I have to start from there, and i will try to make some time to see what i can do, but sadly i cannot promise anything because i have a loot to do lately.

Best Regards,
Emanuel Varga
0
Tooraj
Top achievements
Rank 1
answered on 21 Oct 2010, 01:38 PM
Hello Emanuel,
this is the link which lets you have the FADatePicker Component:
http://www.codeproject.com/KB/selection/FarsiLibrary.aspx
You can simply add it to a Windows Forms Project by right clicking on the ToolBox and then choosing Choose Toolbox Items...
Drag the FADatePicker into the form. Drag a GridView object to the form. You will see that you can add the Calendar to the DataGrid.
But to add to RadGridView you must change namespace FarsiLibrary.Win.Controls in a way that makes it possible. Everything is available in the link.
there is no need to send a project to you because everything is available in the link.
the Author of the component says that to add FADatePicker to a DataGrid which is provided by Microsoft and is in ToolBox, we must create a class which makes it possible to add this control to the DataGrid. to do this we must write this code:
public class DataGridViewFADateTimePickerColumn : DataGridViewColumn
{
}

as you can see deriving from DataGridViewColumn make this possible. to do this for RadGridView we must write:
public class DataGridViewFADateTimePickerColumn : GridViewDataColumn
{
}
Microsoft says that you must Derive from this class:DataGridViewColumn
and Telerik offers this class:GridViewDataColumn

namespace FarsiLibrary.Win.Controls is where I'm talking about.
I tried to change the FarsiLibrary.Win.Controls in order to implement RadGridViewFADateTimePickerColumn but many errors occured.
Is anything Clear?
I wish to say one more time that you must download the FarsiLibrary from the link I provide at first and try to add it to RadGridViewObject.

Thank you Emanuel, I hope that everything is provided for you to reply to my question.

0
Tooraj
Top achievements
Rank 1
answered on 22 Oct 2010, 12:54 PM
What happend?
Is there anybody to solve my problem?
Emanuel, if you tell me your email address I can Send an instance of project to you.
0
Stefan
Telerik team
answered on 25 Oct 2010, 04:47 PM
Hi Tooraj,

The current discussion is continued in this forum thread.

I would kindly ask you to avoid splitting one topic in several forum threads. This will allow us our users to follow our forum more easily and will avoid any possible confustion. Thank you for your understanding.
 
Sincerely yours,
Stefan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Tooraj
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Tooraj
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or