Solved

Vb.Net Datagridview date column with mask

Posted on 2011-03-21
2
2,415 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

822 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