only Number in datagrid cell
-
hi , How can i use in a DataGrid Cell Only Number , Iuse yhis function for this :
public bool OnlyIsNumeric(string val)
{
try{
int result = 0;
return int.TryParse(val, System.Globalization.NumberStyles.Integer, System.Globalization.NumberFormatInfo.CurrentInfo, out result);
}
catch{ return false;}
}but Ican not use this in datagrid in DataGrid1_CurrentCellChanged Event :
DataGrid1[DataGrid1.CurrentRowIndex, 2] = "";
:confused: thanxs_mostafa_h
-
hi , How can i use in a DataGrid Cell Only Number , Iuse yhis function for this :
public bool OnlyIsNumeric(string val)
{
try{
int result = 0;
return int.TryParse(val, System.Globalization.NumberStyles.Integer, System.Globalization.NumberFormatInfo.CurrentInfo, out result);
}
catch{ return false;}
}but Ican not use this in datagrid in DataGrid1_CurrentCellChanged Event :
DataGrid1[DataGrid1.CurrentRowIndex, 2] = "";
:confused: thanxs_mostafa_h
Hi Mostafa, Please use this code: private void dgview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { dgview.EditingControl.KeyPress += new KeyPressEventHandler(dgviewKeyPress); } private void dgviewKeyPress(object sender, KeyPressEventArgs args) { if (!char.IsDigit(args.KeyChar)) args.Handled = true; } Please let me know if this helps. Thanks,
Gopal.S
-
Hi Mostafa, Please use this code: private void dgview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { dgview.EditingControl.KeyPress += new KeyPressEventHandler(dgviewKeyPress); } private void dgviewKeyPress(object sender, KeyPressEventArgs args) { if (!char.IsDigit(args.KeyChar)) args.Handled = true; } Please let me know if this helps. Thanks,
Gopal.S
thanks a lot for ur reply , but If I want to this (only number ) Only in Column 2 ( one of my field is numeric for e.g. SerialNumber ), in this case How can i do this . By the way I have DataGrid , Not DataGridView .then this object has not the properties that u wrote . :-O thanks again -- modified at 6:58 Monday 7th May, 2007
s_mostafa_h
-
thanks a lot for ur reply , but If I want to this (only number ) Only in Column 2 ( one of my field is numeric for e.g. SerialNumber ), in this case How can i do this . By the way I have DataGrid , Not DataGridView .then this object has not the properties that u wrote . :-O thanks again -- modified at 6:58 Monday 7th May, 2007
s_mostafa_h
-
hi , How can i use in a DataGrid Cell Only Number , Iuse yhis function for this :
public bool OnlyIsNumeric(string val)
{
try{
int result = 0;
return int.TryParse(val, System.Globalization.NumberStyles.Integer, System.Globalization.NumberFormatInfo.CurrentInfo, out result);
}
catch{ return false;}
}but Ican not use this in datagrid in DataGrid1_CurrentCellChanged Event :
DataGrid1[DataGrid1.CurrentRowIndex, 2] = "";
:confused: thanxs_mostafa_h
Hi Mustafa, Here is the another solution: namespace Sample { public partial class Form1 : Form { DataGrid dGird; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dGird = new DataGrid(); dGird.CurrentCellChanged += new EventHandler(dGridCellChanged); DataTable dt = new DataTable(); dt.Columns.Add("SNo"); dt.Columns.Add("Name"); dt.Columns.Add("Amount"); dt.Columns.Add("Address"); DataRow drow = dt.NewRow(); drow[0] = "01"; drow[1] = "Gopal"; drow[2] = "100"; drow[3] = "TTL"; dt.Rows.Add(drow); drow = dt.NewRow(); drow[0] = "02"; drow[1] = "Raja"; drow[2] = "150"; drow[3] = "SSS"; dt.Rows.Add(drow); dGird.DataSource = dt; dGird.Size = new Size(400, 400); this.Controls.Add(dGird); System.Windows.Forms.Control.ControlCollection ctlCollection = dGird.Controls; if (dGird.CurrentCell.ColumnNumber == 0) ctlCollection[2].KeyPress += new KeyPressEventHandler(dgKeyPress); } private void dgKeyPress(object sender, KeyPressEventArgs args) { if (!char.IsDigit(args.KeyChar) && args.KeyChar!=(char)Keys.Back) args.Handled = true; } private void dGridCellChanged(object sender, EventArgs args) { // Control index - 0,1 assigned for scrollbars(default). System.Windows.Forms.Control.ControlCollection ctlCollection = dGird.Controls; if (dGird.CurrentCell.ColumnNumber == 0) ctlCollection[2].KeyPress += new KeyPressEventHandler(dgKeyPress); else if (dGird.CurrentCell.ColumnNumber == 2) ctlCollection[4].KeyPress += new KeyPressEventHandler(dgKeyPress); } } } please let me know if this helps. Thanks,
Gopal.S
-
Hi Mustafa, Here is the another solution: namespace Sample { public partial class Form1 : Form { DataGrid dGird; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dGird = new DataGrid(); dGird.CurrentCellChanged += new EventHandler(dGridCellChanged); DataTable dt = new DataTable(); dt.Columns.Add("SNo"); dt.Columns.Add("Name"); dt.Columns.Add("Amount"); dt.Columns.Add("Address"); DataRow drow = dt.NewRow(); drow[0] = "01"; drow[1] = "Gopal"; drow[2] = "100"; drow[3] = "TTL"; dt.Rows.Add(drow); drow = dt.NewRow(); drow[0] = "02"; drow[1] = "Raja"; drow[2] = "150"; drow[3] = "SSS"; dt.Rows.Add(drow); dGird.DataSource = dt; dGird.Size = new Size(400, 400); this.Controls.Add(dGird); System.Windows.Forms.Control.ControlCollection ctlCollection = dGird.Controls; if (dGird.CurrentCell.ColumnNumber == 0) ctlCollection[2].KeyPress += new KeyPressEventHandler(dgKeyPress); } private void dgKeyPress(object sender, KeyPressEventArgs args) { if (!char.IsDigit(args.KeyChar) && args.KeyChar!=(char)Keys.Back) args.Handled = true; } private void dGridCellChanged(object sender, EventArgs args) { // Control index - 0,1 assigned for scrollbars(default). System.Windows.Forms.Control.ControlCollection ctlCollection = dGird.Controls; if (dGird.CurrentCell.ColumnNumber == 0) ctlCollection[2].KeyPress += new KeyPressEventHandler(dgKeyPress); else if (dGird.CurrentCell.ColumnNumber == 2) ctlCollection[4].KeyPress += new KeyPressEventHandler(dgKeyPress); } } } please let me know if this helps. Thanks,
Gopal.S