Solved

Vb.Net Datagridview date column with mask

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

16 Experts available now in Live!

Get 1:1 Help Now