c# - asp.net gridview set null to datasource -
asp.net html:
<asp:gridview id="grddynamic" runat="server" allowpaging="true" gridlines="none" pagesize="20" cellspacing="0" alternatingitemstyle-cssclass="rowb renklisatir" cssclass="tumislerimtable" allowsorting="true" autogeneratecolumns="false"> <headerstyle cssclass="rowa" /> </asp:gridview> <asp:imagebutton id="search" runat="server" alternatetext="search" imageurl="../images/search.gif" onclick="search_click"> </asp:imagebutton>
if click search button below method works
search_click method
protected void search_click(object sender, imageclickeventargs e) { datatable nulldatatable = new datatable(); nulldatatable = null; grddynamic.datasource = nulldatatable; // i'm trying clear gridview datasource not working me grddynamic.databind(); //after bind datasource below programatically datatable dt = new datatable(); datarow drow = dt.newrow(); foreach (var project in multicombobox.checkeditems.tolist()) { dataset ds = procedure.getprojectbyid(type.selectedvalue, startdate, enddate); if (ds.tables[0].rows.count > 0) { datacolumn dcol = new datacolumn(ds.tables[0].rows[0 ["flow_name"].tostring(), typeof(system.int32)); dt.columns.add(dcol); drow[ds.tables[0].rows[0]["flow_name"].tostring()] = ds.tables[0].rows[0]["sayi"].tostring(); } } dt.rows.add(drow); foreach (datacolumn col in dt.columns) { boundfield bfield = new boundfield(); bfield.datafield = col.columnname; bfield.headertext = col.columnname; grddynamic.columns.add(bfield); } grddynamic.datasource = dt; grddynamic.databind(); }
if click search button first time result below
flow_name 1
if click search button second time result below
flow name flow_name 1 1
if click search button third time result below
flow name flow_name flow_name 1 1 1
question:
when click button column , row copies 1 more set datasource null onclick search button not working.how can solve problem how can prevent copy same column , row values ?
note data binding trick clearing rows, not columns. , rows not matter because 1 more data bind in end of click handler, fills gridview data anew.
as columns, run every button click
foreach (datacolumn col in dt.columns)
effectively adding new columns every time. should run once, , on subsequent clicks make sure not add added columns. or better yet try define list of columns declaratively in markup , rid of column-adding code @ all.
Comments
Post a Comment