asp.net - VB.net Mouse click single cell of gridview, change backcolor and output cell position (col and row) to textbox -
i have gridview in webform , know how can select single cell anywhere in gridview using mouse click.
the selected cell's background colour changes specific colour , textbox on form displays row number , column number pass parameters stored procedure.
when subsequent cell selected last selected cell reverts it's original colour , new cell's background colour changed , textbox updates new cell's row number , column number.
the closest i've got far selecting whole row, affects first cell of row background-wise. underline affects cells in row.
protected overrides sub render(byval writer system.web.ui.htmltextwriter) each row gridviewrow in gvprogressgrid.rows if row.rowtype = datacontrolrowtype.datarow row.attributes("onclick") = "this.style.cursor='pointer';this.style.ine';this.style.backgroundcolor ='#eee'" end if next mybase.render(writer) end sub
basically: in code behind set onclick script each cell, passing coordinates of cell , textbox results.
in aspx js script writes coordinates of clicked cell in textbox, iterates cells in table setting color white , sets backgrond color red clicked cell
aspx code:
<%@ page language="vb" autoeventwireup="false" codebehind="testevidenziazione.aspx.vb" inherits="web_test_2010.testevidenziazione" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function highlight(my_row, cell, textbox) { document.getelementbyid("<%=txtcellselected.clientid%>").value = my_row + ',' + cell; var table = document.getelementbyid("<%=gridview1.clientid%>"); (var = 0, row; row = table.rows[i]; i++) { //iterate through rows //rows accessed using "row" variable assigned in loop (var j = 0, col; col = row.cells[j]; j++) { //iterate through columns //columns accessed using "col" variable assigned in loop col.style.backgroundcolor = '#ffffff'; if (i == my_row && j == cell) { col.style.backgroundcolor = '#ff0000'; } } } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:textbox id="txtcellselected" runat="server"></asp:textbox> <asp:gridview id="gridview1" runat="server" autogeneratecolumns="true" showheader="false"> </asp:gridview> </div> </form> </body> </html>
vb.net code:
public class testevidenziazione inherits system.web.ui.page protected sub page_load(byval sender object, byval e system.eventargs) handles me.load loaddata() end sub private sub loaddata() dim list new arraylist dim row1 new myrowclass dim row2 new myrowclass dim row3 new myrowclass row1.a = "0,0" row1.b = "0,1" row1.c = "0,2" row2.a = "1,0" row2.b = "1,1" row2.c = "1,2" row3.a = "2,0" row3.b = "2,1" row3.c = "2,2" list.add(row1) list.add(row2) list.add(row3) gridview1.datasource = list gridview1.databind() end sub private class myrowclass public property string public property b string public property c string end class private sub gridview1_prerender(sender object, e system.eventargs) handles gridview1.prerender index_row = 0 gridview1.rows.count - 1 if gridview1.rows(index_row).rowtype = datacontrolrowtype.datarow index_cell = 0 gridview1.rows(index_row).cells.count - 1 gridview1.rows(index_row).cells(index_cell).attributes("onclick") = "highlight(" & index_row.tostring & "," & index_cell.tostring & ", " & txtcellselected.clientid & "); " next end if next end sub end class
Comments
Post a Comment