asp.net - In Dynamic Data, how to merge cells with equal values in the generated GridView -
i using asp.net dynamic data generate web site several data tables, , far good.
but there request customer want merge cells equal values in gridview.
i have got solution codeproject , works in normal asp.net page.
however, merges rows 1 row in dynamic data generated gridview. traced source code , find out in gridview_prerender
method, row.cells[cellindex].text
empty!
so, can not judge if 2 cells same. have encountered such problem before?
based on research in order values of gridview's
cells on prerender
event in asp.net dynamic data need following things:
- find
fieldtemplate
in cell's controls collection - cast
fieldtemplate
fieldtemplateusercontrol
gain access properties
i have simulated problem based on project.
list.aspx.cs:
protected void gvoffices_prerender(object sender, eventargs e) { (int rowindex = gvoffices.rows.count - 2; rowindex >= 0; rowindex--) { gridviewrow row = gvoffices.rows[rowindex]; gridviewrow previousrow = gvoffices.rows[rowindex + 1]; if (row.rowtype == datacontrolrowtype.datarow) { (int = 0; < row.cells.count; i++) { string datafield = ((dynamicfield)((datacontrolfieldcell)row.cells[i]).containingfield).datafield; control datacontrol = ((fieldtemplateusercontrol)((dynamiccontrol)row.cells[i].finddynamiccontrolrecursive(datafield)).fieldtemplate).datacontrol; string celltext = ((literal)datacontrol).text; // text fields string datafieldprev = ((dynamicfield)((datacontrolfieldcell)previousrow.cells[i]).containingfield).datafield; control datacontrolprev = ((fieldtemplateusercontrol)((dynamiccontrol)previousrow.cells[i].finddynamiccontrolrecursive(datafield)).fieldtemplate).datacontrol; string celltextprev = ((literal)datacontrol).text; // text fields response.write(celltext); response.write(celltextprev); //if (celltext == celltextprev) //{ // row.cells[i].rowspan = previousrow.cells[i].rowspan < 2 ? 2 : // previousrow.cells[i].rowspan + 1; // previousrow.cells[i].visible = false; //} } } } }
finddynamiccontrolrecursive()
can find @ http://csharpbits.notaclue.net/2009/01/dynamic-data-cascading-fieldtemplates.html.
consider technique in project. hope help.
edit 1:
also need check if control inside gridview cell hyperlink
or literal
:
if (datacontrol literal) { celltext = ((literal)datacontrol).text; } else { if (datacontrol hyperlink) { celltext = ((hyperlink)datacontrol).text; } }
edit 2: (it works fine me)
check if control inside gridview cell dynamicfield
:
protected void gvoffices_prerender(object sender, eventargs e) { (int rowindex = gvoffices.rows.count - 2; rowindex >= 0; rowindex--) { gridviewrow row = gvoffices.rows[rowindex]; gridviewrow previousrow = gvoffices.rows[rowindex + 1]; if ((row.rowtype == datacontrolrowtype.datarow) && (previousrow.rowtype == datacontrolrowtype.datarow)) { (int = 0; < row.cells.count; i++) { // check current row if (((datacontrolfieldcell)row.cells[i]).containingfield dynamicfield) { string datafield = ((dynamicfield)((datacontrolfieldcell)row.cells[i]).containingfield).datafield; control datacontrol = ((fieldtemplateusercontrol)((dynamiccontrol)row.cells[i].finddynamiccontrolrecursive(datafield)).fieldtemplate).datacontrol; string celltext = string.empty; if (datacontrol literal) { celltext = ((literal)datacontrol).text; } else { if (datacontrol hyperlink) { celltext = ((hyperlink)datacontrol).text; } } // cells text of current row response.write(celltext); } // check previous row if (((datacontrolfieldcell)previousrow.cells[i]).containingfield dynamicfield) { string datafieldprev = ((dynamicfield)((datacontrolfieldcell)previousrow.cells[i]).containingfield).datafield; control datacontrolprev = ((fieldtemplateusercontrol)((dynamiccontrol)previousrow.cells[i].finddynamiccontrolrecursive(datafieldprev)).fieldtemplate).datacontrol; string celltextprev = string.empty; if (datacontrolprev literal) { celltextprev = ((literal)datacontrolprev).text; } else { if (datacontrolprev hyperlink) { celltextprev = ((hyperlink)datacontrolprev).text; } } // cells text of previous row response.write(celltextprev); } // try merge cells //if (celltext == celltextprev) //{ // row.cells[i].rowspan = previousrow.cells[i].rowspan < 2 ? 2 : // previousrow.cells[i].rowspan + 1; // previousrow.cells[i].visible = false; //} } } } }
Comments
Post a Comment