c# - Programatically generate gridview with specific template -
i trying generate gridview
this.
the first 2 columns done boundfield
, grab based on data given in database.
however, problem comes in when i've have row of am
& pm
.
is there way colspan
header of date , create row am
& pm
? because am
& pm
not counted columns, instead 10/11/2016
column.
aspx
<asp:gridview id="gvattendance" runat="server"> <columns> <asp:boundfield datafield="traineeid" headertext="trainee id" /> <asp:boundfield datafield="idnum" headertext="id" /> </columns> </asp:gridview>
it can done, take @ snippet , adjust needs. snippet apply colspan
in default header , add header row gridview.
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.header) { int spancolumn_1_start = 2; int spancolumn_1_length = 2; //apply colspan e.row.cells[spancolumn_1_start].columnspan = spancolumn_1_length; //remove spanned cells (int = 1; < spancolumn_1_length; i++) { e.row.cells.removeat(spancolumn_1_start + 1); } //note startindex of 2nd colspan set after removing cells 1st colspan int spancolumn_2_start = 3; int spancolumn_2_length = 2; //apply colspan e.row.cells[spancolumn_2_start].columnspan = spancolumn_2_length; //remove spanned cells (int = 1; < spancolumn_2_length; i++) { e.row.cells.removeat(spancolumn_2_start + 1); } } } protected void gridview1_rowcreated(object sender, gridviewroweventargs e) { //to add row above normal gridview header, use: if (e.row.rowtype == datacontrolrowtype.header) if (e.row.rowtype == datacontrolrowtype.datarow && e.row.dataitemindex == 0) { //cast sender gridview gridview gridview = sender gridview; //create new gridviewrow gridviewrow gridviewrow = new gridviewrow(0, 0, datacontrolrowtype.header, datacontrolrowstate.insert); //add cells tablecell tablecell = new tablecell(); tablecell.text = ""; tablecell.columnspan = 2; gridviewrow.cells.add(tablecell); tablecell = new tablecell(); tablecell.text = "am"; gridviewrow.cells.add(tablecell); tablecell = new tablecell(); tablecell.text = "pm"; gridviewrow.cells.add(tablecell); tablecell = new tablecell(); tablecell.text = "am"; gridviewrow.cells.add(tablecell); tablecell = new tablecell(); tablecell.text = "pm"; gridviewrow.cells.add(tablecell); //add new row gridview gridview.controls[0].controls.addat(1, gridviewrow); } }
the result this
Comments
Post a Comment