c# - Gridview with multiple dropdown list with add row & delete button in asp.net -


iam new asp.net can me on this. want make dynamic grid in asp.net have 5 drop down list binding database , on selection of first dropdownvalue second dropdown bind.and has add row & delete row button.

<asp:updatepanel runat="server"><contenttemplate> <asp:gridview id="grvparameterdetail" runat="server"              showfooter="true" autogeneratecolumns="false"             cellpadding="4" forecolor="#333333"              gridlines="none" onrowdeleting="grvparameterdetails_rowdeleting"       > <columns>     <asp:boundfield datafield="rownumber" headertext="sno" />     <asp:templatefield headertext="company">          <itemtemplate>              <asp:dropdownlist datasource='<%# bindcompany1() %>' datatextfield="comp_name" datavaluefield="comp_code" id="drpcompany" runat="server" appenddatabounditems="true" autopostback="true" onselectedindexchanged="drpcompany_indexedchanged">              <asp:listitem value="-1">select</asp:listitem>              </asp:dropdownlist>          </itemtemplate>      </asp:templatefield>     <asp:templatefield headertext="center">          <itemtemplate>              <asp:dropdownlist datasource='<%# bindunit1() %>' datatextfield="center" datavaluefield="pub_cent_code" id="drpcenter" runat="server" appenddatabounditems="true">              <asp:listitem value="-1">select</asp:listitem>              </asp:dropdownlist>          </itemtemplate>      </asp:templatefield>     <asp:templatefield headertext="publ.type">          <itemtemplate>              <asp:dropdownlist datasource='<%# bindptype1() %>' datatextfield="pubname" datavaluefield="pubtypecode" id="drppubtype" runat="server" appenddatabounditems="true">              <asp:listitem value="-1">select</asp:listitem>              </asp:dropdownlist>          </itemtemplate>         </asp:templatefield>     <asp:templatefield headertext="publication">          <itemtemplate>              <asp:dropdownlist id="drppub" runat="server" appenddatabounditems="true">              <asp:listitem value="-1">select</asp:listitem>              </asp:dropdownlist>          </itemtemplate>          </asp:templatefield>     <asp:templatefield headertext="edition">          <itemtemplate>              <asp:dropdownlist id="drppubed" runat="server" appenddatabounditems="true">              <asp:listitem value="-1">select</asp:listitem>              </asp:dropdownlist>          </itemtemplate>         <footerstyle horizontalalign="right" />         <footertemplate>             <asp:button id="buttonadd" runat="server"                      text="add new row" onclick="buttonadd_click" />         </footertemplate>     </asp:templatefield>     <asp:commandfield showdeletebutton="true" /> </columns> <footerstyle backcolor="#507cd1" font-bold="true" forecolor="white" /> <rowstyle backcolor="#eff3fb" /> <editrowstyle backcolor="#2461bf" /> <selectedrowstyle backcolor="#d1ddf1" font-bold="true" forecolor="#333333" /> <pagerstyle backcolor="#2461bf" forecolor="white" horizontalalign="center" /> <headerstyle backcolor="#507cd1" font-bold="true" forecolor="white" /> <alternatingrowstyle backcolor="white" /> 

aspx.cs code

if (!page.ispostback)     {          firstgridviewrow();     }  private void firstgridviewrow() {      datatable dt = new datatable();      datarow dr = null;     dt.columns.add(new datacolumn("rownumber", typeof(string)));     dt.columns.add(new datacolumn("col1", typeof(string)));     dt.columns.add(new datacolumn("col2", typeof(string)));     dt.columns.add(new datacolumn("col3", typeof(string)));     dt.columns.add(new datacolumn("col4", typeof(string)));     dt.columns.add(new datacolumn("col5", typeof(string)));     dr = dt.newrow();     dr["rownumber"] = 1;     //dr["col2"] = string.empty;     //dr["col3"] = string.empty;     //dr["col4"] = string.empty;     //dr["col5"] = string.empty;     dt.rows.add(dr);      viewstate["currenttable"] = dt;      grvparameterdetail.datasource = dt;     grvparameterdetail.databind();   }  private void addnewrow() {     int rowindex = 0;      if (viewstate["currenttable"] != null)     {         datatable dtcurrenttable = (datatable)viewstate["currenttable"];         datarow drcurrentrow = null;         if (dtcurrenttable.rows.count > 0)         {             drcurrentrow = dtcurrenttable.newrow();             drcurrentrow["rownumber"] = dtcurrenttable.rows.count + 1;              dtcurrenttable.rows.add(drcurrentrow);             viewstate["currenttable"] = dtcurrenttable;             (int = 1; <= dtcurrenttable.rows.count - 1; i++)             {                 dropdownlist drpcompany =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[1].findcontrol("drpcompany");                  dropdownlist drpcenter =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[2].findcontrol("drpcenter");                  dropdownlist drppubtype =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[3].findcontrol("drppubtype");                  dropdownlist drppublication =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[4].findcontrol("drppub");                  dropdownlist drpedition =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[5].findcontrol("drppubed");                   //drcurrentrow = dtcurrenttable.newrow();                 //drcurrentrow["rownumber"] = + 1;                  dtcurrenttable.rows[i]["col1"] = drpcompany.selectedvalue;                 dtcurrenttable.rows[i]["col2"] = drpcenter.selectedvalue;                 dtcurrenttable.rows[i]["col3"] = drppubtype.selectedvalue;                 dtcurrenttable.rows[i]["col4"] = drppublication.selectedindex;                 dtcurrenttable.rows[i]["col5"] = drpedition.selectedvalue;                 rowindex++;             }             //dtcurrenttable.rows.add(drcurrentrow);             //viewstate["currenttable"] = dtcurrenttable;              grvparameterdetail.datasource = dtcurrenttable;             grvparameterdetail.databind();         }     }     else     {         response.write("viewstate null");     }     setpreviousdata(); }  private void setpreviousdata() {     int rowindex = 0;     if (viewstate["currenttable"] != null)     {         datatable dt = (datatable)viewstate["currenttable"];         if (dt.rows.count > 0)         {             (int = 0; < dt.rows.count; i++)             {                 dropdownlist drpcompany =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[1].findcontrol("drpcompany");                  dropdownlist drpcenter =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[2].findcontrol("drpcenter");                  dropdownlist drppubtype =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[3].findcontrol("drppubtype");                  dropdownlist drppublication =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[4].findcontrol("drppub");                  dropdownlist drpedition =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[5].findcontrol("drppubed");                   drpcompany.selectedvalue = dt.rows[i]["col1"].tostring();                 drpcenter.selectedvalue = dt.rows[i]["col2"].tostring();                 drppubtype.selectedvalue = dt.rows[i]["col3"].tostring();                 drppublication.selectedvalue = dt.rows[i]["col4"].tostring();                 drpedition.selectedvalue = dt.rows[i]["col5"].tostring();                 rowindex++;             }         }     } }  protected void grvparameterdetails_rowdeleting(object sender, gridviewdeleteeventargs e) {     setrowdata();     if (viewstate["currenttable"] != null)     {         datatable dt = (datatable)viewstate["currenttable"];         datarow drcurrentrow = null;         int rowindex = convert.toint32(e.rowindex);         if (dt.rows.count > 1)         {             dt.rows.remove(dt.rows[rowindex]);             drcurrentrow = dt.newrow();             viewstate["currenttable"] = dt;             grvparameterdetail.datasource = dt;             grvparameterdetail.databind();              (int = 0; < grvparameterdetail.rows.count - 1; i++)             {                 grvparameterdetail.rows[i].cells[0].text = convert.tostring(i + 1);             }             setpreviousdata();         }     } }  private void setrowdata() {     int rowindex = 0;      if (viewstate["currenttable"] != null)     {         datatable dtcurrenttable = (datatable)viewstate["currenttable"];         datarow drcurrentrow = null;         if (dtcurrenttable.rows.count > 0)         {             (int = 1; <= dtcurrenttable.rows.count; i++)             {                 dropdownlist drpcompany =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[1].findcontrol("drpcompany");                  dropdownlist drpcenter =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[2].findcontrol("drpcenter");                  dropdownlist drppubtype =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[3].findcontrol("drppubtype");                  dropdownlist drppublication =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[4].findcontrol("drppub");                  dropdownlist drpedition =                   (dropdownlist)grvparameterdetail.rows[rowindex].cells[5].findcontrol("drppubed");                 drcurrentrow = dtcurrenttable.newrow();                 drcurrentrow["rownumber"] = + 1;                 dtcurrenttable.rows[i - 1]["col1"] = drpcompany.selectedvalue;                 dtcurrenttable.rows[i - 1]["col2"] = drpcenter.selectedvalue;                 dtcurrenttable.rows[i - 1]["col3"] = drppubtype.selectedvalue;                 dtcurrenttable.rows[i - 1]["col4"] = drppublication.selectedvalue;                 dtcurrenttable.rows[i - 1]["col5"] = drpedition.selectedvalue;                 rowindex++;             }              viewstate["currenttable"] = dtcurrenttable;             //grvstudentdetails.datasource = dtcurrenttable;             //grvstudentdetails.databind();         }     }     else     {         response.write("viewstate null");     }     //setpreviousdata(); }  protected void drpcompany_indexedchanged(object sender, eventargs e) {   //what write here } 

thanks in advance.

i have made sample code , wil easy idea how start ur task.. have done harcoded data.. instead of hardcoding use data db.. chk comments ...


page 1 : aspx file

    <%@ page language="c#" autoeventwireup="true" codebehind="webform1.aspx.cs" inherits="webapplication1.webform1" %>      <!doctype html>      <html xmlns="http://www.w3.org/1999/xhtml">     <head runat="server">         <title></title>         <script src="jquery.js.js"></script>         <script type="text/javascript">          </script> </head> <body>     <form id="form1" runat="server">          <table>             <tr>                 <td>last name</td>                 <td>                     <asp:textbox id="txtlastname" runat="server"></asp:textbox></td>             </tr>             <tr>                 <td>speciality</td>                 <td>                     <asp:dropdownlist id="ddlspeciality" runat="server">                         <asp:listitem text="" />                         <asp:listitem text="aaa" />                         <asp:listitem text="bbb" />                         <asp:listitem text="ccc" />                     </asp:dropdownlist></td>             </tr>             <tr>                 <td>location</td>                 <td>                     <asp:dropdownlist id="ddllocation" runat="server">                         <asp:listitem text="" />                         <asp:listitem text="bangalore" />                         <asp:listitem text="chennai" />                         <asp:listitem text="hyderabad" />                     </asp:dropdownlist></td>             </tr>             <tr>                  <td colspan="2" align="center">                     <asp:button id="btnsearch" runat="server" text="search" onclick="btnsearch_click" /></td>             </tr>         </table>       </form> </body> </html> 

page 1: cs file

    using system;     using system.collections.generic;     using system.linq;     using system.web;     using system.web.ui;     using system.web.ui.webcontrols;      namespace webapplication1     {         public partial class webform1 : system.web.ui.page         {             protected void page_load(object sender, eventargs e)             {             }              protected void btnsearch_click(object sender, eventargs e)             {                 string lastname = txtlastname.text.trim();                 string speciality = ddlspeciality.text;                 string location = ddllocation.text;                 response.redirect(string.format("page2.aspx?lastname={0}&speciality={1}&location={2}",lastname ,speciality , location));               }         }       } 

page 2: aspx file

    <%@ page language="c#" autoeventwireup="true" codebehind="page2.aspx.cs" inherits="webapplication1.test" %>      <!doctype html>      <html xmlns="http://www.w3.org/1999/xhtml">     <head runat="server">         <title></title>     </head>     <body>         <form id="form1" runat="server">             <div>                  <asp:gridview id="gvdata" runat="server">                     <columns>                         <asp:templatefield>                             <itemtemplate>                                 <asp:linkbutton id="lnkid"  onclick="linkbutton1_click" text="details" runat="server"></asp:linkbutton>                             </itemtemplate>                         </asp:templatefield>                     </columns>                 </asp:gridview>             </div>         </form>     </body>     </html> 

page 2: cs file

    using system;     using system.collections.generic;     using system.data;     using system.linq;     using system.web;     using system.web.ui;     using system.web.ui.webcontrols;      namespace webapplication1     {         public partial class test : system.web.ui.page         {             protected void page_load(object sender, eventargs e)             {                 if (page.ispostback) { }                 else                 {                     string lastname = request.querystring["lastname"];                     string speciality = request.querystring["speciality"];                     string location = request.querystring["location"];                      string query = lastname.length == 0 ? "" : "lastname ='" + lastname + "' and";                     query += speciality.length == 0 ? "" : "speciality ='" + speciality + "' and";                     query += location.length == 0 ? "" : "location ='" + location + "' ";                      query = query.trim().trimstart(new char[] { 'a', 'n', 'd' }).trimend(new char[] { 'a', 'n', 'd' });                      query = "select * tablename " +( query.length == 0 ? "1=1" : query);                     // use query data sql database                     // use shld data db                     // example,  have hardcoded datatable  values                      datatable dt = new datatable();                     dt.columns.add("id", typeof(int));                     dt.columns.add("lastname", typeof(string));                     dt.columns.add("speciality", typeof(string));                     dt.columns.add("location", typeof(string));                     dt.rows.add(1, "karthik", "aaa", "bangalore");                     dt.rows.add(2, "parthip", "aaa", "chennai");                     dt.rows.add(3, "krishna", "aaa", "hyderabad");                       gvdata.datasource = dt;                     gvdata.databind();                    }             }              protected void linkbutton1_click(object sender, eventargs e)             {                 string id  = ((sender linkbutton).parent.parent gridviewrow).cells[1].text;                 response.redirect("page3.aspx?id=" + id);             }         }     } 

page 3: aspx file

    <%@ page language="c#" autoeventwireup="true" codebehind="page3.aspx.cs" inherits="webapplication1.page3" %>      <!doctype html>      <html xmlns="http://www.w3.org/1999/xhtml">     <head runat="server">         <title></title>     </head>     <body>         <form id="form1" runat="server">             <div runat="server" id="divcontent">             </div>         </form>     </body>     </html> 

page 3: cs file

        using system;         using system.collections.generic;         using system.data;         using system.linq;         using system.web;         using system.web.ui;         using system.web.ui.webcontrols;           namespace webapplication1         {         public partial class page3 : system.web.ui.page         {             protected void page_load(object sender, eventargs e)             {                 if (page.ispostback) { }                 else                 {                      string id = request.querystring["id"];                     // use id fetch data db details.                       // data db..                     // have hardcoded                      datatable dt = new datatable();                      dt.columns.add("id", typeof(int));                      dt.columns.add("lastname", typeof(string));                      dt.columns.add("speciality", typeof(string));                      dt.columns.add("location", typeof(string));                      dt.rows.add(1, "karthik", "aaa", "bangalore");                      table tbl = new table() { cellpadding=1 , cellspacing=2 , bordercolor = system.drawing.color.red, borderwidth=1 };                       foreach (datacolumn col in dt.columns)                      {                          tablerow tr = new tablerow() { borderwidth=1 , bordercolor = system.drawing.color.red };                          tr.cells.add( new tablecell () { borderwidth=1 , bordercolor = system.drawing.color.red ,text = col.columnname});                          tr.cells.add( new tablecell () { borderwidth=1 , bordercolor = system.drawing.color.red ,text = dt.rows[0][col].tostring()});                          tbl.rows.add(tr);                          }                       divcontent.controls.add(tbl);                      }                }         }     } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -