I need Currency TextBox in DataGridView , I search Internet and Find this solution [^] But this is useful when dataGridView Cell Leave event,I need comma separator in textchange , However I write this source for this purpose :
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txt_edit = e.Control as TextBox;
if (txt_edit != null)
{
txt_edit.TextChanged += new EventHandler(txt_edit_TextChanged);
}
}
private void txt_edit_TextChanged(object sender, EventArgs e)
{
TextBox txt = (TextBox) sender;
string str = txt.Text;
str = str.Replace(",", "");
int len = str.Length;
if (len > 3)
{
str = str.Insert(len - 3, ",");
len = len - 3;
while (len > 3)
{
str = str.Insert(len - 3, ",");
len = len - 3;
}
}
dataGridView1.EndEdit();
dataGridView1.CurrentRow.Cells[0].Value = str;
dataGridView1.BeginEdit(false);
}
when i run my program and input number this source work correctly for 3 first digit , until type fourth number cording this error:
why this error cording? Is there a better way to solve the problem? tnx