c# - Empty GridView with dropdownlist having Datatable as it's datasource -
i've form shows empty gridview along add new button. have drop down list in column 1 gets populated database. based on selected dropdown list, query sql database , populate columns 2 , 3. i'm having trouble writing values of dropdown list datatable , add new row. worked when gridview had textboxes adding databound dropdownlist made lot complex. here code:
<asp:gridview id="gridview1" runat="server" showfooter="true" autogeneratecolumns="false"> <columns> <asp:boundfield datafield="rownumber" headertext="row number" /> <asp:templatefield headertext="item number"> <itemtemplate> <asp:dropdownlist id="ddlitemnumber" runat="server" datasourceid="sdssubjects" datatextfield="item_num" datavaluefield="item_num" onselectedindexchanged="index_changed" autopostback="true" ondatabound="adddefaultvalue_databound"> </asp:dropdownlist> <asp:sqldatasource id="sdssubjects" runat="server" connectionstring="<%$ connectionstrings:test%>" selectcommand="select item_num tblitemlist"></asp:sqldatasource> <asp:requiredfieldvalidator id="reqitemnumber" validationgroup="valgrpcust" controltovalidate="ddlitemnumber" runat="server" errormessage="required"></asp:requiredfieldvalidator> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="description"> <itemtemplate> <asp:textbox id="textbox1" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="unit"> <itemtemplate> <asp:textbox id="textbox2" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="quantity"> <itemtemplate> <asp:textbox id="textbox3" runat="server"></asp:textbox> </itemtemplate> <footerstyle horizontalalign="right" /> <footertemplate> <asp:button id="buttonadd" runat="server" text="add new row" onclick="buttonadd_click" /> </footertemplate> </asp:templatefield> </columns> </asp:gridview>
here code behind:
protected void page_load(object sender, eventargs e) { if (!page.ispostback) { setinitialrow(); } } protected void buttonadd_click(object sender, eventargs e) { addnewrowtogrid(); } protected void adddefaultvalue_databound(object sender, eventargs e) { dropdownlist list = sender dropdownlist; if (list != null) { list.items.insert(0, "--select one--"); } } private void setinitialrow() { datatable dt = new datatable(); datarow dr = null; dt.columns.add(new datacolumn("item number", typeof(string))); dt.columns.add(new datacolumn("description", typeof(string))); dt.columns.add(new datacolumn("unit", typeof(string))); dt.columns.add(new datacolumn("quantity", typeof(string))); dr = dt.newrow(); dr["item number"] = string.empty; dr["description"] = string.empty; dr["unit"] = string.empty; dr["quantity"] = string.empty; dt.rows.add(dr); //store datatable in viewstate viewstate["currenttable"] = dt; gridview1.datasource = dt; gridview1.databind(); } private void addnewrowtogrid() { 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++) { //extract textbox values dropdownlist ddllist1 = (dropdownlist)gridview1.rows[rowindex].findcontrol("ddlitemnumber"); textbox box1 = (textbox)gridview1.rows[rowindex].findcontrol("textbox1"); textbox box2 = (textbox)gridview1.rows[rowindex].findcontrol("textbox2"); textbox box3 = (textbox)gridview1.rows[rowindex].findcontrol("textbox3"); drcurrentrow = dtcurrenttable.newrow(); //drcurrentrow["ro wnumber"] = + 1; dtcurrenttable.rows[i - 1]["item number"] = ddllist1.selecteditem.text; dtcurrenttable.rows[i - 1]["description"] = box1.text; dtcurrenttable.rows[i - 1]["unit"] = box2.text; dtcurrenttable.rows[i - 1]["quantity"] = box3.text; rowindex++; } dtcurrenttable.rows.add(drcurrentrow); viewstate["currenttable"] = dtcurrenttable; gridview1.datasource = dtcurrenttable; gridview1.databind(); } } else { response.write("viewstate null"); } //set previous data on postbacks setpreviousdata(); } private void setpreviousdata() { int rowindex = 0; if (viewstate["currenttable"] != null) { datatable dtcurrenttable = (datatable)viewstate["currenttable"]; datatable dt = (datatable)viewstate["currenttable"]; if (dt.rows.count > 0) { (int = 0; < dt.rows.count; i++) { dropdownlist ddllist1 = (dropdownlist)gridview1.rows[rowindex].findcontrol("ddlitemnumber"); textbox box1 = (textbox)gridview1.rows[rowindex].findcontrol("textbox1"); textbox box2 = (textbox)gridview1.rows[rowindex].findcontrol("textbox2"); textbox box3 = (textbox)gridview1.rows[rowindex].findcontrol("textbox3"); ddllist1.selecteditem.text = dt.rows[i]["item number"].tostring(); box1.text = dt.rows[i]["description"].tostring(); box2.text = dt.rows[i]["unit"].tostring(); box3.text = dt.rows[i]["quantity"].tostring(); rowindex++; } } } } protected void index_changed(object sender, eventargs e) { dropdownlist ddlitemnumber = (dropdownlist)sender; gridviewrow grdrdropdownrow = ((gridviewrow)ddlitemnumber.parent.parent); int index = grdrdropdownrow.rowindex; //lblmessage.text = ddlitemnumber.selecteditem.text; dataset mydataset = getdata("select description, unit tblitemlist item_num='" + ddlitemnumber.selecteditem.text.trim () + "'"); grdrdropdownrow.cells[1].text = mydataset.tables[0].rows[0]["description"].tostring(); grdrdropdownrow.cells[2].text = mydataset.tables[0].rows[0]["unit"].tostring(); datatable dt = (datatable)viewstate["currenttable"]; if (dt.rows.count > 0) { (int = 0; < dt.rows.count; i++) { dt.rows[i]["item number"] = grdrdropdownrow.cells[1].text; dt.rows[i]["description"] = grdrdropdownrow.cells[2].text; dt.rows[i]["unit"] = grdrdropdownrow.cells[3].text; dt.rows[i]["quantity"] = grdrdropdownrow.cells[4].text; } } setpreviousdata(); } private dataset getdata(string query) { string constring = configurationmanager.connectionstrings["test"].connectionstring; sqlcommand cmd = new sqlcommand(query); using (sqlconnection con = new sqlconnection(constring)) { using (sqldataadapter sda = new sqldataadapter()) { cmd.connection = con; sda.selectcommand = cmd; using (dataset ds = new dataset()) { sda.fill(ds); return ds; } } } }
thanks clues , time.
joe
Comments
Post a Comment