asp.net - Add string values into array list after looping through gridview rows c# -
i using gridview bulk update. after make changes column , hit update button in page, foreach loop executed update. how can add these string values arraylist after looping
protected void btnupdate_click(object sender, eventargs e) { try { foreach (gridviewrow row in gvdetails.rows) { string strid = ((label)row.findcontrol("lblid")).text; string strgroup = ((label)row.findcontrol("lblgrp")).text; string strvalue = ((textbox)row.findcontrol("txtvalue")).text; } arraylist allist = new arraylist(); allist.add(strid); allist.add(strgroup); allist.add(strvalue); } catch (exception ex) { } }
but values of string after looping not getting inserted arraylist. van me through this.
it looks code should be
var allist = new list<string>(); foreach (gridviewrow row in gvdetails.rows) { string strid = ((label)row.findcontrol("lblid")).text; string strgroup = ((label)row.findcontrol("lblgrp")).text; string strvalue = ((textbox)row.findcontrol("txtvalue")).text; allist.add(strid); allist.add(strgroup); allist.add(strvalue); }
otherwise you're trying add list variable out of scope (since strid
, others has been declared inside loop inner scope)
note: don't use arraylist
obsolete. in case it's better use list<string>
instead.
or, if missed point , need add list variables last iteration (as can supposed code) - declare strid
, other variables outside loop, this:
string strid = null, strgroup = null, strvalue = null; var allist = new list<string>(); foreach (gridviewrow row in gvdetails.rows) { strid = ((label)row.findcontrol("lblid")).text; strgroup = ((label)row.findcontrol("lblgrp")).text; strvalue = ((textbox)row.findcontrol("txtvalue")).text; } allist.add(strid); allist.add(strgroup); allist.add(strvalue);
update
since we've clarified in comments goal , need not list<string>
rather datatable
, code like:
var dt = new datatable(); dt.columns.add("id", typeof(string)); dt.columns.add("group", typeof(string)); dt.columns.add("value", typeof(string)); foreach (gridviewrow row in gvdetails.rows) { var dro = dt.newrow(); dro["id"] = ((label)row.findcontrol("lblid")).text; dro["group"] = ((label)row.findcontrol("lblgrp")).text; dro["value"] = ((textbox)row.findcontrol("txtvalue")).text; dt.rows.add(dro); }
also note - data types of datatable columns can any, not strings - depends on actual data want store.
Comments
Post a Comment