c# - How can I use a WHERE clause in GridView control? -


i want create clause can values database gridview particular id have stored in viewstate on page load. clause marked in stars in code

<asp:gridview id="gvview" runat="server" autogeneratecolumns="false" datakeynames="id"                 datasourceid="sqldatasource" allowpaging="true" pagesize="50" width="100%"                 emptydatatext="--- no records yet. ---" pagerstyle-horizontalalign="center" pagersettings-pagebuttoncount="5"                 emptydatarowstyle-forecolor="#888581" emptydatarowstyle-font-size="14px" emptydatarowstyle-height="30px"                 emptydatarowstyle-font-italic="true" alternatingrowstyle-backcolor="#e2e2e2"                 pagerstyle-cssclass="pager">                 <columns>                     <asp:templatefield headertext="select">                         <itemtemplate>                             <asp:checkbox id="rowselector" runat="server" />                         </itemtemplate>                     </asp:templatefield>                     <asp:boundfield datafield="id" headertext="id" sortexpression="id" visible="false" />                     <asp:boundfield datafield="title" headertext="title" sortexpression="title" />                   </columns>             </asp:gridview>             <asp:sqldatasource id="sqldatasource" runat="server"                  providername="system.data.sqlclient" connectionstring="<%$ connectionstrings:connectionstring %>"                 selectcommand="select id, title table **where anotherid=@anotherid** order id">             </asp:sqldatasource> 

you can call methord bind gridview

protected void bindgridview(int anotherid) {    dataset ds = new dataset();    using (sqlconnection con = new sqlconnection("data source=source;integrated security=true;initial catalog=mysampledb"))    {       con.open();       sqlcommand cmd = new sqlcommand("select id, title table anotherid='"+anotherid+"' order id", con);       sqldataadapter da*emphasized text* = new sqldataadapter(cmd);       da.fill(ds);       con.close();       gvview.datasource = ds;       gvview.databind(); } 

if want bind sqldatasource

    sqldatasource sqldatasource = new sqldatasource();     sqldatasource.id = "sqldatasource";     this.page.controls.add(sqldatasource1);     sqldatasource.connectionstring = system.configuration.configurationmanager.connectionstrings["constring"].connectionstring;     sqldatasource.selectcommand = "select id, title table anotherid='"+anotherid+"' order id";     gvview.datasource = sqldatasource;     gvview.databind(); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -