Windows Develop Bookmark and Share   
 index > Windows Forms General > How to let user enter only numeric value in particular cell of a gridview????
 

How to let user enter only numeric value in particular cell of a gridview????

hi,
i have a gridview which consist of 10 column.....

now
col1 col2 col3 col4 col5 col6.....................................

i wan the user to enter only numeric value in col1 and col3 an col4 of gridview....

How to handle this????


abhi0410  Thursday, November 13, 2008 5:30 AM

Hi Abhishek_Ranjan

You can restrict user input non-digital char into these cells using this code.

Code Snippet

partial class Form1 : Form

{

private DataTable dtTest = new DataTable();

public Form1()

{

InitializeComponent();

dtTest.Columns.Add("Col1");

dtTest.Columns.Add("Col2");

dtTest.Columns.Add("Col3");

dtTest.Columns.Add("Col4");

dtTest.Columns.Add("Col5");

dtTest.Columns.Add("Col6");

dtTest.Rows.Add(1, "Test", 2, 3, "Test", "Test");

dtTest.Rows.Add(1, "Test", 2, 3, "Test", "Test");

dataGridView1.DataSource = dtTest;

dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

}

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

TextBox textBox = e.Control as TextBox;

if (textBox != null)

{

textBox.KeyPress += new KeyPressEventHandler(textBox_KeyPress);

}

}

void textBox_KeyPress(object sender, KeyPressEventArgs e)

{

if (this.dataGridView1.CurrentCell.ColumnIndex == 0 || this.dataGridView1.CurrentCell.ColumnIndex == 2 || this.dataGridView1.CurrentCell.ColumnIndex == 3)

{

if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '\b')

{

e.Handled = true;

}

else

{

e.Handled = false;

}

}

}

Handle the EditingControlShowing event let you get the currently edited TextBox in a cell, then you can handle the KeyPress event of the TextBox to cancel input if that char is not a digital char.

Wish you can get help from my solution.

Sincerely,

Kira Qian

Kira Qian  Friday, November 14, 2008 5:20 AM
When do you want validation to occur? If there is a submit button or something, you can grab the DataGridViewRow in question and try ( Convert.ToInt32(row.Cells[x].Value.ToString()) > 0) to ensure it is an integer.
omarazam  Thursday, November 13, 2008 5:58 PM

Hi Abhishek_Ranjan

You can restrict user input non-digital char into these cells using this code.

Code Snippet

partial class Form1 : Form

{

private DataTable dtTest = new DataTable();

public Form1()

{

InitializeComponent();

dtTest.Columns.Add("Col1");

dtTest.Columns.Add("Col2");

dtTest.Columns.Add("Col3");

dtTest.Columns.Add("Col4");

dtTest.Columns.Add("Col5");

dtTest.Columns.Add("Col6");

dtTest.Rows.Add(1, "Test", 2, 3, "Test", "Test");

dtTest.Rows.Add(1, "Test", 2, 3, "Test", "Test");

dataGridView1.DataSource = dtTest;

dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

}

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

TextBox textBox = e.Control as TextBox;

if (textBox != null)

{

textBox.KeyPress += new KeyPressEventHandler(textBox_KeyPress);

}

}

void textBox_KeyPress(object sender, KeyPressEventArgs e)

{

if (this.dataGridView1.CurrentCell.ColumnIndex == 0 || this.dataGridView1.CurrentCell.ColumnIndex == 2 || this.dataGridView1.CurrentCell.ColumnIndex == 3)

{

if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '\b')

{

e.Handled = true;

}

else

{

e.Handled = false;

}

}

}

Handle the EditingControlShowing event let you get the currently edited TextBox in a cell, then you can handle the KeyPress event of the TextBox to cancel input if that char is not a digital char.

Wish you can get help from my solution.

Sincerely,

Kira Qian

Kira Qian  Friday, November 14, 2008 5:20 AM

You can use google to search for other answers

Custom Search

More Threads

• Looking for recommendation on a good WinForms demo capturing technology or service?
• This forum has migrated successfully on December 10, 2008
• How to add icon to ToolStripComboBox (like IE has the favicon next to the URL)
• TabIndexChanged
• Communication between usercontrols (strongly typed)
• getting rid of restore/down?
• Motherboard resource accessing
• Check if TextBox contents are Numeric
• problem with Object and SQLCommand
• OCX msninst.dll