Solved

Vb.Net Datagridview date column with mask

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

937 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now