Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Related to Windows Forms
 

Related to Windows Forms

Hi Everybody,

In my current applicationi am developing a windows form in that datagrid i am embeding a text box and i have to validate that textbox.

the problem here i am facing is on Keypress event it is validatingthat only numbers should be acceptedthe first column of the grid rather the second column where the text box is present.

Every time itwill take thealphenumeric characters and after tab button is pressed it will fire the validation message where the focus will on first column. What i want is that the validation should be done while enteringinto the textbox and itshould not take any characters and that the textbox is in second column of the grid, when the focus is in first column of grid if i try to enter other characters it will fire but same is not happening in textbox which is in second column..

Hope you understood the problem..

Hope i can get a solution here.....

Chetan VD  Monday, August 20, 2007 9:43 AM

Hi Chetan,

I didn’t quite the problem you are facing. Based on my understanding about your post, you want a datagrid with two columns. The first column only accepts numbers, and the second column accepts all characters. Right? If so, I would like the following way to do this:

Code Snippet

DataGridP

public partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

DataTable dt = new DataTable();

private void Form3_Load(object sender, EventArgs e)

{

dt.Columns.Add("numbers");

dt.Columns.Add("any");

DataGridTableStyle ts = new DataGridTableStyle();

ts.GridColumnStyles.Clear();

DataGridTextBoxColumn tb1 = new DataGridTextBoxColumn();

tb1.HeaderText = "Numbers";

tb1.MappingName = "numbers";

tb1.Width = 120;

tb1.NullText = "";

tb1.TextBox.KeyPress += new KeyPressEventHandler(TextBox_KeyPress);

//you can also add validation event for first column here

ts.GridColumnStyles.Add(tb1);

DataGridTextBoxColumn tb2 = new DataGridTextBoxColumn();

tb2.HeaderText = "Any";

tb2.MappingName = "any";

tb2.Width = 120;

tb2.NullText = "";

ts.GridColumnStyles.Add(tb2);

this.dataGrid1.TableStyles.Add(ts);

this.dataGrid1.DataSource = dt;

}

void TextBox_KeyPress(object sender, KeyPressEventArgs e)

{

if (!Char.IsNumber(e.KeyChar))

e.Handled = true;

}

}

I assume you use .Net Framework 1.1, because you told us you use DataGrid. If you use DataGridView in .Net Framework 2.0, these is an EditingControlShowing event for this control, and you can handle this event to get the TextBox which is hosted in this control.

Hope this helps.

Best regards.

Rong-Chun Zhang  Thursday, August 23, 2007 6:46 AM

Hi Chetan,

I didn’t quite the problem you are facing. Based on my understanding about your post, you want a datagrid with two columns. The first column only accepts numbers, and the second column accepts all characters. Right? If so, I would like the following way to do this:

Code Snippet

DataGridP

public partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

DataTable dt = new DataTable();

private void Form3_Load(object sender, EventArgs e)

{

dt.Columns.Add("numbers");

dt.Columns.Add("any");

DataGridTableStyle ts = new DataGridTableStyle();

ts.GridColumnStyles.Clear();

DataGridTextBoxColumn tb1 = new DataGridTextBoxColumn();

tb1.HeaderText = "Numbers";

tb1.MappingName = "numbers";

tb1.Width = 120;

tb1.NullText = "";

tb1.TextBox.KeyPress += new KeyPressEventHandler(TextBox_KeyPress);

//you can also add validation event for first column here

ts.GridColumnStyles.Add(tb1);

DataGridTextBoxColumn tb2 = new DataGridTextBoxColumn();

tb2.HeaderText = "Any";

tb2.MappingName = "any";

tb2.Width = 120;

tb2.NullText = "";

ts.GridColumnStyles.Add(tb2);

this.dataGrid1.TableStyles.Add(ts);

this.dataGrid1.DataSource = dt;

}

void TextBox_KeyPress(object sender, KeyPressEventArgs e)

{

if (!Char.IsNumber(e.KeyChar))

e.Handled = true;

}

}

I assume you use .Net Framework 1.1, because you told us you use DataGrid. If you use DataGridView in .Net Framework 2.0, these is an EditingControlShowing event for this control, and you can handle this event to get the TextBox which is hosted in this control.

Hope this helps.

Best regards.

Rong-Chun Zhang  Thursday, August 23, 2007 6:46 AM

Hi Rong-Chun Zhang,

thanks for the valuable reply that helped my problem......

sorry for the delay in the reply.......

Chetan VD  Friday, February 15, 2008 5:06 AM

You can use google to search for other answers

Custom Search

More Threads

• Networking layer
• server side config
• Unable to Install TV Server on Windows Server 2003 Std.
• Trying to find WebBrowserSamples
• Bind/load .abr files into photoshop
• three tier archetecture
• NO BussinesRules Separation!
• Where's the documentation?
• TaskVision and Replication
• primary key