c# - how to work with multiple datakeys in gridview? -
i created gridview multiple (two) datakeys , tried extract datakey values in code included below. when load gridview, row["booktaxid"] gives correct id being used. while i'm extracting value gridview datakeys code same column returns "1". can me? code-->
gridview:- <asp:gridview runat="server" id="taxgv" datakeynames="id, booktaxid" autogeneratecolumns="false" ondatabound="taxgv_databound" onrowcommand="taxgv_rowcommand" cssclass="table table-bordered table-hover table-responsive" showfooter="true"> loadinggridview taxgv:- public void loadtaxgridview_table() { double value = 0; int bkid = convert.toint32(bookid); string str = configurationmanager.connectionstrings["constr"].connectionstring; sqlconnection con = new sqlconnection(str); con.open(); sqlcommand cmd = new sqlcommand("select b.booktaxid booktaxid, b.taxid id, t.name name, b.taxvalue value bookingtax b inner join taxes t on t.id = b.taxid b.bookingid=@bookingid , b.is_delete=0", con); cmd.parameters.addwithvalue("@bookingid", bkid); datatable dt_booktax = new datatable(); sqldatareader reader = cmd.executereader(); dt_booktax.load(reader); con.close(); foreach(datarow row in dt_booktax.rows) { int id = convert.toint32(row["booktaxid"]);//this gives correct value of booktaxid value = convert.todouble(row["value"]); total_tax_amount += (temp_amount * value / 100); } getparameters_bookingtax(value); datarow dr = null; dr = dt_booktax.newrow(); dr["booktaxid"] = 0; dr["id"] = 0; dr["name"] = string.empty; dr["value"] = 0; dt_booktax.rows.add(dr); viewstate["booktax_table"] = dt_booktax; taxgv.datasource = dt_booktax; taxgv.databind(); taxgv.rows[taxgv.rows.count - 1].visible = false; }
updaing table through gridview taxgv:-
public void edit_booking_tax() { int bkid = convert.toint32(bookid); string str = configurationmanager.connectionstrings["constr"].connectionstring; sqlconnection con = new sqlconnection(str); con.open(); foreach(gridviewrow row in taxgv.rows) { int rowindex = row.rowindex; int booktaxid = convert.toint32(taxgv.datakeys[rowindex].values["booktaxid"]); //this booktaxid gives value=1; label lb_taxvalue = (label)row.cells[1].findcontrol("item_value"); hiddenfield hf_taxid = (hiddenfield)row.cells[2].findcontrol("hf_taxid"); if (lb_taxvalue.text != "0") { sqlcommand cmd = new sqlcommand("update bookingtax set bookingid=@bookingid, taxid=@taxid, taxvalue=@taxvalue, modifiedby=@modifiedby, modifieddate=@modifieddate booktaxid=@booktaxid", con); cmd.parameters.addwithvalue("@bookingid", bkid); cmd.parameters.addwithvalue("@taxid", convert.toint32(hf_taxid.value)); cmd.parameters.addwithvalue("@taxvalue", convert.todouble(lb_taxvalue.text)); cmd.parameters.addwithvalue("@modifiedby", session["id"].tostring()); cmd.parameters.addwithvalue("@modifieddate", datetime.now); cmd.parameters.addwithvalue("@bookingid", bkid); cmd.parameters.addwithvalue("booktaxid", booktaxid); cmd.executenonquery(); } } con.close(); }
try changing line
int booktaxid = convert.toint32(taxgv.datakeys[rowindex].values["booktaxid"]);
to
int booktaxid = convert.toint32(taxgv.datakeys[rowindex].values[1]);
as far remember should able access values of datakeys based on position similar accessing array particular row.
Comments
Post a Comment