c# - Add new rows to gridview in asp.net -


as add new rows, want default values new rows zero. new rows added when click addnewrows button blank. how can preassign 0 value each of column in row?

 private void addnewrow()     {       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 ddltaskcurrweek = (dropdownlist)mygridview.rows[rowindex].cells[1].findcontrol("ddltaskcurrweek");             textbox textboxday1 = (textbox)mygridview.rows[rowindex].cells[2].findcontrol("txtday1");             textbox textboxday2 = (textbox)mygridview.rows[rowindex].cells[3].findcontrol("txtday2");             textbox textboxday3 = (textbox)mygridview.rows[rowindex].cells[4].findcontrol("txtday3");             textbox textboxday4 = (textbox)mygridview.rows[rowindex].cells[5].findcontrol("txtday4");             textbox textboxday5 = (textbox)mygridview.rows[rowindex].cells[6].findcontrol("txtday5");             textbox textboxday6 = (textbox)mygridview.rows[rowindex].cells[7].findcontrol("txtday6");             textbox textboxday7 = (textbox)mygridview.rows[rowindex].cells[8].findcontrol("txtday7");             label lbl8 = (label)mygridview.rows[rowindex].cells[9].findcontrol("lbltotal");             // textbox textboxday8 = (textbox)mygridview.rows[rowindex].cells[9].findcontrol("txtday8");               drcurrentrow = dtcurrenttable.newrow();             drcurrentrow["rownumber"] = + 1;             dtcurrenttable.rows[i - 1]["col1"] = ddltaskcurrweek.selectedvalue;             dtcurrenttable.rows[i - 1]["col2"] = textboxday1.text;             dtcurrenttable.rows[i - 1]["col3"] = textboxday2.text;             dtcurrenttable.rows[i - 1]["col4"] = textboxday3.text;             dtcurrenttable.rows[i - 1]["col5"] = textboxday4.text;             dtcurrenttable.rows[i - 1]["col6"] = textboxday5.text;             dtcurrenttable.rows[i - 1]["col7"] = textboxday6.text;             dtcurrenttable.rows[i - 1]["col8"] = textboxday7.text;             dtcurrenttable.rows[i - 1]["col9"] = lbl8.text;             //  dtcurrenttable.rows[i - 1]["col9"] = textboxday8.text;                rowindex++;           }           dtcurrenttable.rows.add(drcurrentrow);           viewstate["currenttable"] = dtcurrenttable;            mygridview.datasource = dtcurrenttable;           mygridview.databind();                  }       }       else       {         response.write("viewstate null");       }       setpreviousdata();     } 

here setpreviousdata code

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 ddltaskcurrweek = (dropdownlist)mygridview.rows[rowindex].cells[1].findcontrol("ddltaskcurrweek");             textbox textboxday1 = (textbox)mygridview.rows[rowindex].cells[2].findcontrol("txtday1");             textbox textboxday2 = (textbox)mygridview.rows[rowindex].cells[3].findcontrol("txtday2");             textbox textboxday3 = (textbox)mygridview.rows[rowindex].cells[4].findcontrol("txtday3");             textbox textboxday4 = (textbox)mygridview.rows[rowindex].cells[5].findcontrol("txtday4");             textbox textboxday5 = (textbox)mygridview.rows[rowindex].cells[6].findcontrol("txtday5");             textbox textboxday6 = (textbox)mygridview.rows[rowindex].cells[7].findcontrol("txtday6");             textbox textboxday7 = (textbox)mygridview.rows[rowindex].cells[8].findcontrol("txtday7");             label lbl8 = (label)mygridview.rows[rowindex].cells[9].findcontrol("lbltotal");             // textbox textboxday8 = (textbox)mygridview.rows[rowindex].cells[9].findcontrol("txtday8");              // drcurrentrow["rownumber"] = + 1;              mygridview.rows[i].cells[0].text = convert.tostring(i + 1);             ddltaskcurrweek.selectedvalue = dt.rows[i]["col1"].tostring();             textboxday1.text = dt.rows[i]["col2"].tostring();             textboxday2.text = dt.rows[i]["col3"].tostring();             textboxday3.text = dt.rows[i]["col4"].tostring();             textboxday4.text = dt.rows[i]["col5"].tostring();             textboxday5.text = dt.rows[i]["col6"].tostring();             textboxday6.text = dt.rows[i]["col7"].tostring();             textboxday7.text = dt.rows[i]["col8"].tostring();             lbl8.text = dt.rows[i]["col9"].tostring();              rowindex++;           }         }       }     } 

first, think have problem existing logic. appears iterating on existing rows in table, , @ each iteration, creating new row @ current index + 1. add row table @ end. means if have 10 existing rows in table, you're creating 10 new rows, last iteration adds row table, net affect 1 new row. if not intention, better approach be:

//outside loop drnewrow = dtcurrenttable.newrow(); drnewrow["rownumber"] = dtcurrenttable.rows.count + 1; drnewrow["col1"] = "0"; //assuming drop down has default value of 0. drnewrow["col2"] = "0"; //etc.. dtcurrenttable.rows.add(drnewrow); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -