Solved

Vb.Net Datagridview date column with mask

Posted on 2011-03-21
2
2,528 Views
Last Modified: 2012-05-11
how can set a mask dd/mm/yyyy in a date column in datagridview, like "  -  -    ". When run project in the date column not appear mask: "  -  -    "
0
Comment
Question by:rflorencio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Assisted Solution

by:Maverick_Cool
Maverick_Cool earned 100 total points
ID: 35179603
0
 
LVL 2

Accepted Solution

by:
ramkihardy earned 400 total points
ID: 35179770
You can add a MaskedTextBox into the control collection of the DataGridView, handle the DataGridView's CellBeginEdit event to host the MaskedTextBox in the current cell, the Mask of which is set based on the cell value in the ComboBox column; handle the CellEndEdit event to update the current cell with the data in the MaskedTextBox. And we have to handle the Scroll event in order to adjust the location for the MaskedTextBox while scrolling if it's visible.

public partial class Form7 : Form

    {

        public Form7()

        {

            InitializeComponent();

        }

 

        private void Form7_Load(object sender, EventArgs e)

        {

            DataGridViewComboBoxColumn TpyeCol = new DataGridViewComboBoxColumn();

            TpyeCol.Name = "Type";

            TpyeCol.HeaderText = "Type";

            TpyeCol.Items.AddRange(new string[] { "Home Phone", "Cell", "Work", "Email" });

            this.dataGridView1.Columns.Add(TpyeCol);

 

            this.dataGridView1.Columns.Add("Description", "Description");

            this.dataGridView1.Rows.Add("Home Phone","");

 

            this.maskedTextBox = new MaskedTextBox();

            this.maskedTextBox.Visible = false;

            this.dataGridView1.Controls.Add(this.maskedTextBox);

 

            this.dataGridView1.CellBeginEdit += 

                new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);

 

            this.dataGridView1.CellEndEdit += 

                new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

 

            this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);

        }

 

        void dataGridView1_Scroll(object sender, ScrollEventArgs e)

        {

            if (this.maskedTextBox.Visible)

            {
                //we have to adjust the location for the MaskedTextBox while scrolling

                Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(

                    this.dataGridView1.CurrentCell.ColumnIndex, 

                    this.dataGridView1.CurrentCell.RowIndex, true);

                this.maskedTextBox.Location = rect.Location;

            }

        }

 

        void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)

        {

            if (e.ColumnIndex == this.dataGridView1.Columns["Description"].Index &&

                e.RowIndex < this.dataGridView1.NewRowIndex)

            {

                string type = this.dataGridView1["Type", e.RowIndex].Value.ToString();

                if (type == "Home Phone" || type == "Cell" || type == "Work")

                {

                    this.maskedTextBox.Mask = "(###)###-####";

                    Rectangle rect = 
                       this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,true);

                    this.maskedTextBox.Location = rect.Location;

                    this.maskedTextBox.Size = rect.Size;

                    this.maskedTextBox.Text = "";

                    if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)

                    {

                        this.maskedTextBox.Text = this.dataGridView1[e.ColumnIndex, 
                            e.RowIndex].Value.ToString();

                    }

                    this.maskedTextBox.Visible = true;

                }

                // if type is Email, do no show the MaskedTextBox

            }

        }

 

        void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

        {

            if (this.maskedTextBox.Visible)

            {

                this.dataGridView1.CurrentCell.Value = this.maskedTextBox.Text;

                this.maskedTextBox.Visible = false;

            }

        }

 

        MaskedTextBox maskedTextBox;

    }

Open in new window

Hope it helps...
Ramki
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question