Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datagridview cell specific custom mask
 

Datagridview cell specific custom mask

I have a table named ContactInformation which holds customer phone numbers (cell, home, work, etc.) and email(s). It has three columns, "Type" (1 = home phone, 2 = cell, 3 = work, 4 = email, etc.) and "Description" and "Notes".

I have the "Type" column in the datagridiew as a combobox. What I want to do is have the "Description"cell display a mask textbox depending on the value in the "Type" cell for that row. So if the "Type" is 1, 2,or 3 then the "Description" cell displays a mask for a phone number (###) ###-####. If the "Type" is a 4 then the "Description" cell does not have a mask.

I know how to add a column with a mask text box, but I want the mask to be cell specific based on the value in a different column on the same row.

Suggestions?

chadbengen  Thursday, October 04, 2007 7:24 PM
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..


Code Block
publicpartial 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;

}

There is a sample masked edit column in the datagridview faq samples

http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20Samples.zip

Ken Tucker  Friday, October 05, 2007 1:00 AM
Thank you, I will look at the samples. However, do any of the samples allow for different masks in the same column, depending on the type of data it is displaying?

chadbengen  Wednesday, October 10, 2007 9:06 PM
Unfortunately I did not see any example that illustrates how to have more than one mask in the same column. I am familiar with how to set a mask textbox in a datagridview column, my desire is to have multiple mask's in the same column, depending on the type of data displayed.

chadbengen  Thursday, October 11, 2007 1:34 AM
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..


Code Block
publicpartial 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;

}

Awesome! Thanks for the reply. I'm going to have to work with this code and translate it to vb, but I appreciate it immensly!

chadbengen  Friday, October 12, 2007 5:39 PM
Thank you, it works perfectly fine for me!
Zaarat  Wednesday, September 16, 2009 9:10 AM

You can use google to search for other answers

Custom Search

More Threads

• auto height DataGridView rows
• Maping controls to Datacolumns in SQL server
• datagridview with checkbox
• TableAdapter.Insert(this.cccDataSet.cccTable)
• Adding a property to underlying bound object does not add corresponding column to bound datagridview
• Data Source Configuration Wizard - Multi column primary key
• Changing the font in dataGridView for specific cells.
• adding a row to a datatable and then to a dataset
• how to bring textbox in gridview load
• Create a combobox in code