asp.net - Gridview Sort Causing RowDataBound to not function correctly -


i have gridview works fine each of row commands, however, once sorted causes rowdatabound event use incorrect values rather actual values in row after has been sorted. appreciated!

here code behind.

 //verify , update record protected void unverifiedsalesgv_rowcommand(object sender, gridviewcommandeventargs e) {     buttoncommand = e.commandname;     messagelbl.text = "";      if (e.commandname == "verifyrecord")     {          //get record id         string salesid = unverifiedsalesgv.datakeys[convert.toint32(e.commandargument)].value.tostring();          //get productid          salesdata getsalesrecord = new salesdata();         getsalesrecord.getrowvaluessalesid(salesid);          string productid = getsalesrecord.productid;           if (productid != "38")         {             verifyrenewal = false;             try             {                 updatesalesrecordsds.updatecommand = "update sales set verified = @verified id = @id";                 updatesalesrecordsds.updateparameters["id"].defaultvalue = unverifiedsalesgv.datakeys[convert.toint32(e.commandargument)].value.tostring();                 updatesalesrecordsds.updateparameters["verified"].defaultvalue = true.tostring();                 updatesalesrecordsds.update();                  unverifiedsalesgv.databind();                 verifiedsalesgv.databind();             }             catch (exception ex)             {                 messagelbl.forecolor = color.red;                 messagelbl.text = "could not verify sale. message: " + ex.message;             }         }         else         {             //get row index.             unverifiedsalesgv.seteditrow(convert.toint32(e.commandargument));             verifyrenewal = true;         }     }     if (e.commandname == "updateproduct")     {          dropdownlist productvalue = (dropdownlist)unverifiedsalesgv.rows[convert.toint32(e.commandargument)].findcontrol("renewalproductddl");         if (productvalue.selectedindex != 0)         {             try             {                 updatesalesrecordsds.updatecommand = "update sales set productid = @productid, verified = @verified id = @id";                 updatesalesrecordsds.updateparameters["id"].defaultvalue = unverifiedsalesgv.datakeys[convert.toint32(e.commandargument)].value.tostring();                 updatesalesrecordsds.updateparameters["productid"].defaultvalue = productvalue.selectedvalue;                 updatesalesrecordsds.updateparameters["verified"].defaultvalue = true.tostring();                 updatesalesrecordsds.update();                  unverifiedsalesgv.databind();                 unverifiedsalesgv.editindex = -1;                 verifiedsalesgv.databind();             }             catch (exception ex)             {                 messagelbl.forecolor = color.red;                 messagelbl.text = "could not verify sale. message: " + ex.message;             }         }         else         {             messagelbl.forecolor = color.red;             messagelbl.text = "please select renewal type.";         }      }     if (e.commandname == "updaterecord")     {         //get date , user info         datetime getdate = system.datetime.now;         membershipuser user = membership.getuser();         string activeuser = user.username;          try         {             //get dropdown , textbox values             string commisionmonth;             string grosssalesamount;             string netsalesamount;             string notes;              textbox grosssalesvalue = (textbox)unverifiedsalesgv.rows[convert.toint32(e.commandargument)].findcontrol("grosssalestxt");             textbox netsalesvalue = (textbox)unverifiedsalesgv.rows[convert.toint32(e.commandargument)].findcontrol("netsalestxt");             textbox notesvalue = (textbox)unverifiedsalesgv.rows[convert.toint32(e.commandargument)].findcontrol("notestxt");             dropdownlist commissionmonthvalue = (dropdownlist)unverifiedsalesgv.rows[convert.toint32(e.commandargument)].findcontrol("commissionmonthddl");             grosssalesamount = grosssalesvalue.text;             netsalesamount = netsalesvalue.text;             commisionmonth = commissionmonthvalue.selectedvalue;             notes = notesvalue.text;              unverifiedsalessds.updatecommand = "update [sales] set [grosssalesamount] = @grosssalesamount, [netsalesamount] = @netsalesamount, [notes] = @notes, [commissionmonth] = @commissionmonth, [datelastmodified] = @datelastmodified, [userlastmodified] = @userlastmodified [id] = @id";             unverifiedsalessds.updateparameters["grosssalesamount"].defaultvalue = grosssalesamount;             unverifiedsalessds.updateparameters["netsalesamount"].defaultvalue = netsalesamount;             unverifiedsalessds.updateparameters["commissionmonth"].defaultvalue = commisionmonth;             unverifiedsalessds.updateparameters["notes"].defaultvalue = notes;             unverifiedsalessds.updateparameters["datelastmodified"].defaultvalue = getdate.tostring();             unverifiedsalessds.updateparameters["userlastmodified"].defaultvalue = activeuser;             unverifiedsalessds.updateparameters["id"].defaultvalue = unverifiedsalesgv.datakeys[convert.toint32(e.commandargument)].value.tostring();               unverifiedsalessds.update();             unverifiedsalesgv.databind();             unverifiedsalesgv.editindex = -1;           }         catch (exception ex)         {             messagelbl.forecolor = color.red;             messagelbl.text = "could not update record. message: " + ex.message;         }     }  }  //get product protected void unverifiedsalesgv_rowdatabound(object sender, gridviewroweventargs e) {     if (e.row.rowstate == datacontrolrowstate.edit)     {          if (buttoncommand == "verifyrecord")         {             //get record id             string salesid = unverifiedsalesgv.datakeys[convert.toint32(e.row.dataitemindex)].value.tostring();              //get productid              salesdata getsalesrecord = new salesdata();             getsalesrecord.getrowvaluessalesid(salesid);              string productid = getsalesrecord.productid;              if (productid == "38")             {                 //items hide/display                 button updateproductbtn = (button)e.row.findcontrol("updateproductbtn");                 button updatebtn = (button)e.row.findcontrol("updatebtn");                 label productlbl = (label)e.row.findcontrol("currentproductlbl");                 dropdownlist productddl = (dropdownlist)e.row.findcontrol("renewalproductddl");                 label grosssaleslbl = (label)e.row.findcontrol("grosssaleslbl");                 textbox grosssalestxt = (textbox)e.row.findcontrol("grosssalestxt");                 label netsaleslbl = (label)e.row.findcontrol("netsaleslbl");                 textbox netsalestxt = (textbox)e.row.findcontrol("netsalestxt");                 label commissionmonthlbl = (label)e.row.findcontrol("salescommissionlbl");                 dropdownlist commissionmonthddl = (dropdownlist)e.row.findcontrol("commissionmonthddl");                 textbox notestxt = (textbox)e.row.findcontrol("notestxt");                   updateproductbtn.visible = true;                 updatebtn.visible = false;                 productlbl.visible = false;                 productddl.visible = true;                 grosssaleslbl.visible = true;                 grosssalestxt.visible = false;                 netsaleslbl.visible = true;                 netsalestxt.visible = false;                 commissionmonthlbl.visible = true;                 commissionmonthddl.visible = false;                 notestxt.visible = false;             }         }      } } 

i able solve problem hardcoding in following:

 //hide , show fields protected void unverifiedsalesgv_rowdatabound(object sender, gridviewroweventargs e) {     if (e.row.rowindex == rowindex)     {         string state = e.row.rowstate.tostring();         if (state == "alternate, edit" || state == "edit")         {              //get record id             string salesid = unverifiedsalesgv.datakeys[convert.toint32(e.row.dataitemindex)].value.tostring();              //get productid              salesdata getsalesrecord = new salesdata();             getsalesrecord.getrowvaluessalesid(salesid);              string productid = getsalesrecord.productid;              if (productid == "38")             {                 //items hide/display                 button updateproductbtn = (button)e.row.findcontrol("updateproductbtn");                 button updatebtn = (button)e.row.findcontrol("updatebtn");                 label productlbl = (label)e.row.findcontrol("currentproductlbl");                 label accountmanagerlbl = (label)e.row.findcontrol("accountmanagerlbl");                 dropdownlist productddl = (dropdownlist)e.row.findcontrol("renewalproductddl");                 dropdownlist accountmanagerddl = (dropdownlist)e.row.findcontrol("accountmanagerddl");                 label grosssaleslbl = (label)e.row.findcontrol("grosssaleslbl");                 textbox grosssalestxt = (textbox)e.row.findcontrol("grosssalestxt");                 label netsaleslbl = (label)e.row.findcontrol("netsaleslbl");                 textbox netsalestxt = (textbox)e.row.findcontrol("netsalestxt");                 label commissionmonthlbl = (label)e.row.findcontrol("salescommissionlbl");                 dropdownlist commissionmonthddl = (dropdownlist)e.row.findcontrol("commissionmonthddl");                 textbox notestxt = (textbox)e.row.findcontrol("notestxt");                 label noteslbl = (label)e.row.findcontrol("noteslbl");                   updateproductbtn.visible = true;                 updatebtn.visible = false;                 productlbl.visible = false;                 productddl.visible = true;                 accountmanagerlbl.visible = true;                 accountmanagerddl.visible = false;                 grosssaleslbl.visible = true;                 grosssalestxt.visible = false;                 netsaleslbl.visible = true;                 netsalestxt.visible = false;                 commissionmonthlbl.visible = true;                 commissionmonthddl.visible = false;                 notestxt.visible = false;                 noteslbl.visible = true;             }         }     } } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -