c# - Add decimal point & set cursor position to DataGridView cell text on KeyPress event -


the following code used adding decimal point after 5 digits in datagridview cell , set cursor positions last, code work first datagridview cell.
in advance.

result image

grid editingcontrolshowing event code:

private void grdcharges_editingcontrolshowing(object sender, datagridvieweditingcontrolshowingeventargs e) {     datagridview grd = sender datagridview;     e.control.keypress -= new keypresseventhandler(grdcharges_row0_column2_keypress);     e.control.keypress -= new keypresseventhandler(grdcharges_row1_column2_keypress);     e.control.keypress -= new keypresseventhandler(grdcharges_column3_keypress);     if (grdcharges.currentrow.index == 0)     {         if (grdcharges.currentcell.columnindex == 2) //desired column         {             textbox r0c2 = e.control textbox;             if (r0c2 != null)             {                 r0c2.keypress += new keypresseventhandler(grdcharges_row0_column2_keypress);             }         }     }     if (grdcharges.currentrow.index == 1)     {         if (grdcharges.currentcell.columnindex == 2) //desired column         {             textbox r1c2 = e.control textbox;             if (r1c2 != null)             {                 r1c2.keypress += new keypresseventhandler(grdcharges_row1_column2_keypress);             }         }     }     if (grdcharges.currentcell.columnindex == 3) //desired column     {         textbox tb3 = e.control textbox;         if (tb3 != null)         {             tb3.keypress += new keypresseventhandler(grdcharges_column3_keypress);         }     } } 

add decimal point gridview data value:

private void grdcharges_row0_column2_keypress(object sender, keypresseventargs e) {      vaildationgrdcharges(sender, e, grdcharges.currentrow.index, grdcharges.currentcell.columnindex);     textbox textbox = (textbox)sender;     if (textbox.text.length.tostring() == "5")     {         grdcharges.rows[grdcharges.currentrow.index].cells[grdcharges.currentcell.columnindex].value = string.concat(textbox.text, ".");         if (grdcharges.currentcell.edittype == typeof(datagridviewtextboxeditingcontrol))         {             ((textbox)this.grdcharges.editingcontrol).selectionstart = textbox.text.length + 2; // add logic if length 0            // ((textbox)this.grdcharges.editingcontrol).selectionlength = 0;         }     } }  private void grdcharges_row1_column2_keypress(object sender, keypresseventargs e) {     vaildationgrdcharges(sender, e, grdcharges.currentrow.index, grdcharges.currentcell.columnindex);                     textbox textbox = (textbox)sender;     if (textbox.text.length.tostring() == "5")     {         grdcharges.rows[grdcharges.currentrow.index].cells[grdcharges.currentcell.columnindex].value = string.concat(textbox.text, ".");         if (grdcharges.currentcell.edittype == typeof(datagridviewtextboxeditingcontrol))         {             ((textbox)this.grdcharges.editingcontrol).selectionstart = textbox.text.length + 2; // add logic if length 0             //((textbox)this.grdcharges.editingcontrol).selectionlength = 0;         }     } } 

this code works first grid-view cell?

i need clue or answer or suggesstion.

its because remove all event handlers:

e.control.keypress -= new keypresseventhandler(grdcharges_row0_column2_keypress); e.control.keypress -= new keypresseventhandler(grdcharges_row1_column2_keypress); e.control.keypress -= new keypresseventhandler(grdcharges_column3_keypress); 

but add one depending on if statement conditions checking currentcell row , columnm indexes, eg:

r0c2.keypress += new keypresseventhandler(grdcharges_row0_column2_keypress); 

you can see events lost using debug.writeline, off top of head example:

system.diagnostics.debug.writeline("someevent has " + someevent.getinvocationlist().length + " event handlers");

so that's problem fix take hans advice , use cellvalidating event set cursor.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -