Here is a small routine I had to develop in order to solve a problem about editing an integer value in a grid.
How to trap the error is the user put a letter instead of an integer and click update?
NameSpace
First of all I’m using collections, so we must import the relevant namespace
1 | using System.Collections; |
Grid Sample code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <asp:gridview id= "grdCart" runat= "server" allowsorting= "True" autogeneratecolumns= "False" showfooter= "True" datakeynames= "ID_ARTICLES_COMMANDE" datasourceid= "srcCartView" gridlines= "None" width= "100%" style= "height: 50px; border-radius: 5px" borderwidth= "0px" cssclass= "table" onselectedindexchanged= "grdCart_SelectedIndexChanged" onrowdatabound= "grdCart_RowDataBound" emptydatatext= "Le contenu de votre panier est vide..." onrowupdating= "grdCart_RowUpdating" > <columns> <asp:boundfield datafield= "ID_ARTICLES_COMMANDE" headertext= "ID_ARTICLES_COMMANDE" insertvisible= "False" readonly = "True" sortexpression= "ID_ARTICLES_COMMANDE" visible= "False" > <asp:boundfield datafield= "ID_ARTICLES" headertext= "ID_ARTICLES" sortexpression= "ID_ARTICLES" visible= "False" > <asp:boundfield datafield= "ID_CLIENTS" headertext= "ID_CLIENTS" sortexpression= "ID_CLIENTS" visible= "False" > <asp:boundfield datafield= "DESIGNATION_ARTICLES" headertext= "Designation/Description" sortexpression= "DESIGNATION_ARTICLES" readonly = "true" itemstyle-horizontalalign= "left" footertext= "TOTAL" > <headerstyle horizontalalign= "right" > <itemstyle horizontalalign= "Left" ></itemstyle> <headerstyle horizontalalign= "Left" > </headerstyle></headerstyle></asp:boundfield> <asp:boundfield datafield= "QUANTITE_COMMANDE" headertext= "Qte/Qty" headerstyle-horizontalalign= "Right" sortexpression= "QUANTITE_COMMANDE" itemstyle-horizontalalign= "right" footertext= "TOTAL QTY" > <footerstyle horizontalalign= "Left" > <headerstyle horizontalalign= "right" > <itemstyle horizontalalign= "Left" ></itemstyle> </headerstyle></footerstyle></asp:boundfield> <asp:boundfield datafield= "PRIX_VENTE_HT" headertext= "Prix/Price" headerstyle-horizontalalign= "Right" sortexpression= "PRIX_VENTE_HT" itemstyle-horizontalalign= "Right" readonly = "true" dataformatstring= "{0:0.00}" > <headerstyle horizontalalign= "Right" > <itemstyle horizontalalign= "Left" ></itemstyle> </headerstyle></asp:boundfield> <asp:boundfield datafield= "MONTANT_HT" headertext= "TOTAL" headerstyle-horizontalalign= "Right" sortexpression= "MONTANT_HT" itemstyle-horizontalalign= "Right" readonly = "true" footertext= "TOTAL" dataformatstring= "{0:0.00}" > <headerstyle horizontalalign= "Right" > <itemstyle horizontalalign= "Left" ></itemstyle> <headerstyle horizontalalign= "Right" > </headerstyle></headerstyle></asp:boundfield> <asp:boundfield datafield= "timestamp" headertext= "timestamp" sortexpression= "timestamp" visible= "False" > <asp:commandfield showdeletebutton= "True" showeditbutton= "True" buttontype= "Image" cancelimageurl= "~/images/glyphicons-154-unchecked.png" deleteimageurl= "~/images/glyphicons_016_bin.png" editimageurl= "~/images/glyphicons_150_edit.png" updateimageurl= "~/images/glyphicons-153-check.png" > </asp:commandfield></asp:boundfield></asp:boundfield></asp:boundfield></asp:boundfield></columns> <headerstyle horizontalalign= "Center" ></headerstyle> </asp:gridview> |
C# Code on RowUpdating grid event / Column QUANTITE_COMMANDE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | protected void grdCart_RowUpdating( object sender, GridViewUpdateEventArgs e) { string strQty = "" ; DictionaryEntry ColEntry; //Browse the new values and save them in a collection (key / value) foreach (DictionaryEntry entry in e.NewValues) { if (entry.Key.ToString() == "QUANTITE_COMMANDE" ) //Corresponding to Datafield in your Grid { strQty = entry.Value.ToString(); ColEntry.Key = entry.Key; ColEntry.Value = entry.Value; } } //Isolate the new value int intQty; // Try to convert the new value to integer try { intQty = int .Parse(strQty); } // If conversion fails then go back to the old value catch { intQty = int .Parse(e.OldValues[ColEntry.Key].ToString()); } // Set the Quantity (old or new) to the new value in order to update it e.NewValues[ColEntry.Key] = intQty; } |