EditMask в DataGrid
От: Аноним  
Дата: 16.08.05 12:39
Оценка:
Можно ли сделать в одном из столбцов DataGrid маску ввода для ввода даты(DateTime)

Пример: дд.мм.гг
EditMask в DataGrid
От: Аноним  
Дата: 20.08.05 17:13
Оценка:
Сделайте свой стиль столбца на основе System.Windows.Forms.DataGridColumnStyle
и Как в .NET можно использовать Microsoft Masked Edit ActiveX Control?


данное сообщение получено с www.gotdotnet.ru
ссылка на оригинальное сообщение
Re: EditMask в DataGrid
От: cyberok  
Дата: 22.08.06 10:02
Оценка:
Здравствуйте, Аноним, Вы писали:

А>Можно ли сделать в одном из столбцов DataGrid маску ввода для ввода даты(DateTime)


А>Пример: дд.мм.гг


В описании стиля грида:

Dim col As New DataGridTextBoxColumn
With col
.MappingName = "TransitDateTime"
.HeaderText = "Дата"
.Width = 130
.TextBox.MaxLength = 16
.Format = "dd.MM.yyyy HH:mm"
.NullText = ""
End With
AddHandler col.TextBox.KeyDown, AddressOf ut_ValidDateTimeInput


[AddHandler col.TextBox.KeyDown, AddressOf ut_ValidDateTimeInput] — ссылка на обработчик ввода

Процедура обработки ввода:

Public Sub ut_ValidDateTimeInput(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Dim mVal As String
Dim PosInVAL As Integer
Dim PosInTXT As Integer


mVal = sender.Text
PosInTXT = sender.SelectionStart


mVal = mVal.Replace(".", "")
mVal = mVal.Replace(" ", "")
mVal = mVal.Replace(":", "")
mVal = mVal.Replace("_", "")

If PosInTXT = 0 Then PosInVAL = 0
If PosInTXT = 1 Then PosInVAL = 1
If PosInTXT = 2 Then PosInVAL = 2
If PosInTXT = 3 Then PosInVAL = 2
If PosInTXT = 4 Then PosInVAL = 3
If PosInTXT = 5 Then PosInVAL = 4
If PosInTXT = 6 Then PosInVAL = 4
If PosInTXT = 7 Then PosInVAL = 5
If PosInTXT = 8 Then PosInVAL = 6
If PosInTXT = 9 Then PosInVAL = 7
If PosInTXT = 10 Then PosInVAL = 8
If PosInTXT = 11 Then PosInVAL = 8
If PosInTXT = 12 Then PosInVAL = 9
If PosInTXT = 13 Then PosInVAL = 10
If PosInTXT = 14 Then PosInVAL = 10
If PosInTXT = 15 Then PosInVAL = 11
If PosInTXT = 16 Then PosInVAL = 12

If Not IsNumeric(mVal) And Len(mVal) > 0 Then
mVal = mVal.Remove(mVal.Length — 1, 1)
PosInVAL = PosInVAL — 1
End If

If e.KeyCode = Keys.Back Then
If mVal.Length > 0 And PosInVAL > 0 And mVal.Length = PosInVAL Then
If PosInTXT < 15 Then
mVal = mVal.Remove(PosInVAL — 1, 1)
PosInVAL = PosInVAL — 1
End If
End If

Else
'============= подстановка месяца
If mVal.Length = 3 Then
If mVal.Substring(2, 1) > 1 Then
mVal = mVal.Substring(0, 2) & "0" & mVal.Substring(2, 1)
PosInVAL = PosInVAL + 1
End If
End If
'============= подстановка года
If mVal.Length = 5 Then
If mVal.Substring(4, 1) > 2 Then
mVal = mVal.Substring(0, 4) & "200" & mVal.Substring(4, 1)
PosInVAL = PosInVAL + 3
End If
End If
If mVal.Length > PosInVAL And PosInVAL > 0 Then
mVal = mVal.Insert(PosInVAL — 1, e.KeyData)
'PosInVAL = PosInVAL + 1

End If
End If


If PosInVAL = 0 Then PosInTXT = 0
If PosInVAL = 1 Then PosInTXT = 1
If PosInVAL = 2 Then PosInTXT = 3
If PosInVAL = 3 Then PosInTXT = 4
If PosInVAL = 4 Then PosInTXT = 6
If PosInVAL = 5 Then PosInTXT = 7
If PosInVAL = 6 Then PosInTXT = 8
If PosInVAL = 7 Then PosInTXT = 9
If PosInVAL = 8 Then PosInTXT = 11
If PosInVAL = 9 Then PosInTXT = 12
If PosInVAL = 10 Then PosInTXT = 14
If PosInVAL = 11 Then PosInTXT = 15



'================= ЗАПИСЬ В ТЕКСТВОХ
If mVal.Length = 0 And e.KeyCode = Keys.Back Then sender.Text = mVal.Insert(0, "")
If mVal.Length = 1 Then sender.Text = mVal.Insert(1, "_.__.____ __:__") : sender.SelectionStart = 1 : sender.SelectionLength = 1

If mVal.Length = 2 Then sender.Text = mVal.Insert(2, ".__.____ __:__") : sender.SelectionStart = 3 : sender.SelectionLength = 2
If mVal.Length = 3 Then sender.Text = mVal.Substring(0, 2) & "." & mVal.Substring(2, 1) & "_.____ __:__" : sender.SelectionStart = 4 : sender.SelectionLength = 1

If mVal.Length = 4 Then sender.Text = mVal.Substring(0, 2) & "." & mVal.Substring(2, 2) & ".____ __:__" : sender.SelectionStart = 6 : sender.SelectionLength = 4
If mVal.Length = 5 Then sender.Text = mVal.Substring(0, 2) & "." & mVal.Substring(2, 2) & "." & mVal.Substring(4, 1) & "___ __:__" : sender.SelectionStart = 7 : sender.SelectionLength = 3
If mVal.Length = 6 Then sender.Text = mVal.Substring(0, 2) & "." & mVal.Substring(2, 2) & "." & mVal.Substring(4, 2) & "__ __:__" : sender.SelectionStart = 8 : sender.SelectionLength = 2
If mVal.Length = 7 Then sender.Text = mVal.Substring(0, 2) & "." & mVal.Substring(2, 2) & "." & mVal.Substring(4, 3) & "_ __:__" : sender.SelectionStart = 9 : sender.SelectionLength = 1

If mVal.Length = 8 Then sender.Text = mVal.Substring(0, 2) & "." & mVal.Substring(2, 2) & "." & mVal.Substring(4, 4) & " __:__" : sender.SelectionStart = 11 : sender.SelectionLength = 2
If mVal.Length = 9 Then sender.Text = mVal.Substring(0, 2) & "." & mVal.Substring(2, 2) & "." & mVal.Substring(4, 4) & " " & mVal.Substring(8, 1) & "_:__" : sender.SelectionStart = 12 : sender.SelectionLength = 1

If mVal.Length = 10 Then sender.Text = mVal.Substring(0, 2) & "." & mVal.Substring(2, 2) & "." & mVal.Substring(4, 4) & " " & mVal.Substring(8, 2) & ":__" : sender.SelectionStart = 14 : sender.SelectionLength = 2

If e.KeyData <> Keys.Left And e.KeyData <> Keys.Right Then sender.SelectionStart = PosInTXT
End Sub


Процедура несовершенна, создавалась в попыхах, но работает удовлетворительно.
Re[2]: EditMask в DataGrid
От: cyberok  
Дата: 22.08.06 10:39
Оценка:
В этом алгоритме проблемы с редактирование введённых данных.
Может кто-нибудь доработает алгоритм или поделится своим. Сейчас работаю над новым проектом, опять понадобится, а глючный ставить не охота.
Re[2]: EditMask в DataGrid
От: Аноним  
Дата: 23.08.06 10:01
Оценка:
использую такой код:
public class DataGridViewMaskedTextBoxEditingControl : MaskedTextBox, IDataGridViewEditingControl
    {

        public DataGridViewMaskedTextBoxEditingControl () :base()
        {}
#region IDataGridViewEditingControl Members

        public Cursor EditingPanelCursor
        {
            get { return this.Cursor; }
        }

        private DataGridView dataGrid;
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGrid;
            }

            set
            {
                dataGrid=value;
            }
        }

        public object EditingControlFormattedValue
        {
            get { return this.Text; }
            set { this.Text = value.ToString(); }
        }


        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            switch ((keyData & Keys.KeyCode))
            {
                case Keys.Prior:
                case Keys.Next:
                    if (this.bValueChanged)
                    {
                        return true;
                    }
                    break;

                case Keys.End:
                case Keys.Home:
                    if (this.SelectionLength != this.Text.Length)
                    {
                        return true;
                    }
                    break;

                case Keys.Left:
                    if (((this.RightToLeft == RightToLeft.No) && ((this.SelectionLength != 0) || (base.SelectionStart != 0))) || ((this.RightToLeft == RightToLeft.Yes) && ((this.SelectionLength != 0) || (base.SelectionStart != this.Text.Length))))
                    {
                        return true;
                    }
                    break;

                case Keys.Up:
                    if ((this.Text.IndexOf("\r\n") >= 0) && ((base.SelectionStart + this.SelectionLength) >= this.Text.IndexOf("\r\n")))
                    {
                        return true;
                    }
                    break;

                case Keys.Right:
                    if (((this.RightToLeft == RightToLeft.No) && ((this.SelectionLength != 0) || (base.SelectionStart != this.Text.Length))) || ((this.RightToLeft == RightToLeft.Yes) && ((this.SelectionLength != 0) || (base.SelectionStart != 0))))
                    {
                        return true;
                    }
                    break;

                case Keys.Down:
                    {
                        int num1 = base.SelectionStart + this.SelectionLength;
                        if (this.Text.IndexOf("\r\n", num1) != -1)
                        {
                            return true;
                        }
                        break;
                    }
                case Keys.Delete:
                    if ((this.SelectionLength > 0) || (base.SelectionStart < this.Text.Length))
                    {
                        return true;
                    }
                    break;

                case Keys.Return:
                    if ((keyData & (Keys.Alt | Keys.Control | Keys.Shift)) == Keys.Shift)
                    {
                        return true;
                    }
                    break;
            }
            return !dataGridViewWantsInputKey;

        }

        public void PrepareEditingControlForEdit(bool selectAll)
        {
            if(selectAll)
                this.SelectAll();
        }
        public bool RepositionEditingControlOnValueChange
        {
            get { return false; }
        }

        private int m_nRowIndex;
        public int EditingControlRowIndex
        {
            get
            {
                return m_nRowIndex;
            }

            set
            {
                m_nRowIndex = value;
            }
        }


        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            //this.TextAlign = dataGridViewCellStyle.Alignment;
            this.Font = dataGridViewCellStyle.Font;
            this.ForeColor = dataGridViewCellStyle.ForeColor;
            this.BackColor = dataGridViewCellStyle.BackColor;
        }
        bool bValueChanged;
        public bool EditingControlValueChanged
        {
            get
            {
                return bValueChanged;
            }

            set
            {
                bValueChanged=value;
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            bValueChanged=true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnTabIndexChanged(e);
        }


        #endregion
    }
    public class DataGridViewMaskedTextBoxCell : DataGridViewTextBoxCell
    {
        public DataGridViewMaskedTextBoxCell(): base()
        { }
        public override Type EditType
        {
            get { return typeof(DataGridViewMaskedTextBoxEditingControl); }
        }


        public override object Clone()
        {
            DataGridViewMaskedTextBoxCell ret = base.Clone() as DataGridViewMaskedTextBoxCell;
            if (ret != null)
            {
                ret.m_strMask = m_strMask;
                ret.m_strPromt = m_strPromt;
                ret.m_eTextAlign = m_eTextAlign;
            }
            return ret;
        }

        string m_strMask;
        public string MaskText
        {
            get { return m_strMask; }
            set { m_strMask = value; }
        }
        char m_strPromt;
        public char Promt
        {
            get { return m_strPromt;}
            set { m_strPromt = value; }
        }
        HorizontalAlignment m_eTextAlign;
        public HorizontalAlignment TextAlign
        {
            get { return m_eTextAlign;}
            set { m_eTextAlign = value; }
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            DataGridViewMaskedTextBoxEditingControl ctrl = this.DataGridView.EditingControl as DataGridViewMaskedTextBoxEditingControl;
            if (ctrl != null)
            {
                ctrl.Mask = m_strMask;
                ctrl.PromptChar = m_strPromt;
                ctrl.TextAlign = m_eTextAlign;
                ctrl.Text = initialFormattedValue.ToString();
                //ctrl.BorderStyle = BorderStyle.None;
            }
        }
    }
    public class DataGridViewMaskedTextBoxColumn : DataGridViewColumn
    {
        public DataGridViewMaskedTextBoxColumn():base()
        {
            CellTemplate = new DataGridViewMaskedTextBoxCell();
        }
        

        [System.ComponentModel.Editor("System.Windows.Forms.Design.MaskPropertyEditor, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.Drawing.Design.UITypeEditor)), System.ComponentModel.DefaultValue("")]
        public string Mask
        {
            get
            {
                DataGridViewMaskedTextBoxCell Cell = CellTemplate as DataGridViewMaskedTextBoxCell;
                if (Cell != null) return Cell.MaskText;
                return "";
            }
            set
            {
                DataGridViewMaskedTextBoxCell Cell = CellTemplate as DataGridViewMaskedTextBoxCell;
                if (Cell != null)
                    Cell.MaskText = value;
            }
        }
        [DefaultValue('\0')]
        public char Promt
        {
            get
            {
                DataGridViewMaskedTextBoxCell Cell = CellTemplate as DataGridViewMaskedTextBoxCell;
                if (Cell != null) return Cell.Promt;
                return '\0';
            }
            set
            {
                DataGridViewMaskedTextBoxCell Cell = CellTemplate as DataGridViewMaskedTextBoxCell;
                if (Cell != null)
                    Cell.Promt = value;
            }
        }
        [DefaultValue(0)]
        public HorizontalAlignment TextAlign
        {
            get{
                DataGridViewMaskedTextBoxCell Cell = CellTemplate as DataGridViewMaskedTextBoxCell;
                if (Cell != null) return Cell.TextAlign;
                return HorizontalAlignment.Left;}
            set
            {
                DataGridViewMaskedTextBoxCell Cell = CellTemplate as DataGridViewMaskedTextBoxCell;
                if (Cell != null)
                    Cell.TextAlign = value;
            }
        }
    }





данное сообщение получено с www.gotdotnet.ru
ссылка на оригинальное сообщение
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.