Solved

Vb.Net Datagridview date column with mask

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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