c# - How to bind a list item to gridview -
i retrieving bunch of strings after doing comma separation. next want bind gridview. but, throws error:
a field or property name _barcodes not found on selected data source.
below code:
private void retrievescannedbarcodes() ////code retrieve barcodes foreach (var item in scannedbarcodes) { _barcodes.addrange(item.split(',')); foreach (var b in _barcodes) { gvscannedbarcodes.datasource = b; gvscannedbarcodes.databind(); } }
and gridview code:
<asp:gridview id="gvscannedbarcodes" runat="server" allowsorting="true" autogeneratecolumns="false" pagersettings-mode="numericfirstlast" pagesize="25" width="741px"> <columns> <asp:boundfield datafield="_barcodes" headertext="barcodes" itemstyle-horizontalalign="center" headerstyle-width="50" /> </columns> </asp:gridview>
any kind of appreciated. in advance.
a field or property name _barcodes not found on selected data source.
you binding string datasource gridview instead of collection datatable
of list
has column or property named _barcodes
.
you have list scannedbarcodes
contains comma separated bar codes strings , want barcodes list binded gridview. have bar codes scannedbarcodes
in collection have _barcodes
column.
you can use linq query bar codes using selectmany
, select
, put result anonymous object list having column name _barcodes
this.
gvscannedbarcodes.datasource = scannedbarcodes.selectmany(s=>s.split(',') .select(b=>new {_barcodes = b})); gvscannedbarcodes.databind();
to make simple understand see following example.
list<string> list = new list<string>(new string []{"1,2,3","4,5,6"}); gvscannedbarcodes.datasource = list.selectmany(l=>l.split(',') .select(b=>new {_barcodes = b})); gvscannedbarcodes.databind();
Comments
Post a Comment