c# - How to get a column of checkboxes in Gridview? -


one note i'm doing in visual studio 2013 express web.

i have gridview, , data being generated sqldatasource. currently, when test page. table below gets generated:

customerid    customername    customeraddress -----------   ------------    ---------------- 1             bob             address 2             john            address 3             smith           address 

however, want this:

customerid    customername    customeraddress -----------   ------------    ---------------- []             bob             address []             john            address []             smith           address 

i want customerid field "hidden field" , have checkbox in place. then, want value of checkboxes customerid row. however, can't life of me checkboxes in there, , it's still displaying customerid itself. in end, want create button delete rows of whatever checked, , have reflected in database via delete table. why want checkboxes "have" value of whatever customerid particular row.

here's code:

<asp:gridview id="gridview1" runat="server" allowsorting="true" autogeneratecolumns="false" datakeynames="customerid" datasourceid="rowsingroup" >             <columns>                 <asp:boundfield datafield="customerid" headertext="customerid" readonly="true" sortexpression="customerid" />                 <asp:boundfield datafield="customername" headertext="customername" sortexpression="customername" />                 <asp:boundfield datafield="customeraddress" headertext="customeraddress" sortexpression="customeraddress" />                 <asp:templatefield></asp:templatefield>             </columns> </asp:gridview> 

if there's better data object use in toolbox, i'm ears.

thanks advice can provide!

you have half of problem solved. using datakeynames="customerid", have no need hidden field hold value.

first, create check box column. there multiple ways accomplish this. here one:

<asp:templatefield>     <itemtemplate>         <asp:checkbox id="chkdelete" runat="server" />     </itemtemplate> </asp:templatefield> 

then in whatever event handles delete, iterate each row in gridview , find checkbox in each row. if checked, use datakey row customerid.

protected void btndelete_click(object sender, eventargs e) {     list<string> customerstodelete = new list<string>();     foreach(gridviewrow row in gridview1.rows)     {         checkbox chkdelete = (checkbox)row.findcontrol("chkdelete");         if(chkdelete.checked)         {             datakey key = gridview1.datakeys[row.dataitemindex];             customerstodelete.add(key.value.tostring());         }     } } 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -