Gridview extra header row with command button and paging

P

pickedaname

Hi,
I have a SQL bound gridview with select button and paging enabled.
In the gridviews prerender event, I am inserting another header in row 0.

Dim table As Table = DirectCast(GridView1.Controls(0), Table)
Dim hrow As GridViewRow = New GridViewRow(0, -1, DataControlRowType.Header,
DataControlRowState.Normal)
Dim th As TableCell = New TableHeaderCell()
th.HorizontalAlign = HorizontalAlign.Center
th.ColumnSpan = 2
th.BackColor = System.Drawing.Color.FromArgb(214, 211, 206)
th.ForeColor = Drawing.Color.Black
th.BorderColor = Drawing.Color.Black
th.BorderWidth = 1
th.Font.Bold = True
th.Text = "Employee"
th.ID = "hdrEmp"
hrow.Cells.Add(th)
table.Rows.AddAt(0, hrow)

There are other columns added to hrow.Cells above before I add hrow to
table.rows using the same exact coding (different names and colspans) trying
to save space here.
The problem I am having is whenever i click the select button control on any
record,
I lose my pager row and I get an empty row inserted below the header rows.
So if I click another select button, I get yet another blank row inserted
below the headers. I can keep this up until all rows in the grid are blank.
If I load the page and click the pager control to go to page 2 or 3, I don't
get the blank row and I don't lose my pager row, it only happens when select
is clicked. I have nothing going on (yet) when a record is selected, it is
simply selected so there is no other code on the page to respond to
rowselected.
Am I messing up the indexing?
Please Help
 
P

pickedaname

Hi Phillip,
I checked out the page you sent, but I don't see what there is to help me with
my problem. Can you guide me a little?
Thanks,
-Lance R.
 
P

Phillip Williams

Hi Lance,

I would have tried what you did below while handling the databound event on
the GridView as the example in the link did. Take the reference to the
header row reformat it, e.g. take its cells with their controls and move them
to a new row in a new Table to which you can add the header row that you
attempted to insert below.
 
P

pickedaname

Hi Phillip,
I am getting a null ref exception when adding the cellarray to the row.
Since 'row' is given a value (= New TableRow) I am assuming this is
referring to the
TableCell array aTD(). When an array is declared, is it also instantiated by
default?
I cannot instantiate the array as I can the row as there is no 'new' function
allowed at that point.
row.Cells.AddRange(aTD)
 
P

Phillip Williams

Hi Lance,

You are right. My demo runs on the C# code (in which the array was
initialized using the New keyword and therefore I did not get an error). But
my VB code would not run (the cause is that I set the array size ot
Cells.Count instead of Cells.Count-1). The proper syntax should be:
Dim aTD(grvrow.Cells.Count-1) As TableCell
 
P

pickedaname

Hi Phillip,
Ok now I have 2 header rows using the example off the link you provided. I
now have my header row as the second row and my new header row as the first.
New problem: It's as if the 2 header rows are a seperate entity from my data
rows because the columns don't mate up. In my custom headerrow, I set
cell(0).colspan = 2, it spans the 2 columns of the next row (which is my
original headers), but not my datarows. header row col1 is of a different
width than datarow col1. I have (laborously) set the width of every column in
the custom rows to match what I set
the datarows cols to be,taking padding into account, but still different. To
be sure, I wrapped it all in a div with a width greater than my gridview and
set all wrapping to false. I just want an extra header row at top to span
some columns of the original header row and have it still behave just like a
normal gridview. I even tried foregoing this and created a table above the
grid to match and all worked well, that is until I went to print a report.
This produced the same alignment issue. Please help.
Thanks

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.Header Then
Dim grvrow As GridViewRow = e.Row
Dim tbl As Table = New Table()
Dim row As TableRow = New TableRow
Dim row2 As TableRow = New TableRow()
Dim deletecell As TableCell
Dim aTD(grvrow.Cells.Count - 1) As TableCell

grvrow.Cells.CopyTo(aTD, 0) 'my headers copied to aTD
grvrow.Cells.Clear() ' Original row cleared
row.Cells.AddRange(aTD) ' my headers go from aTD to row
deletecell = row.Cells(row.Cells.Count - 1)
'removes my select header, which is not visible
row.Cells.Remove(deletecell)
tbl.Rows.Add(row)

'second row
row2 = New TableRow()
Dim cell As TableCell = New TableCell()
Dim cell2 As TableCell = New TableCell()
Dim cell3 As TableCell = New TableCell()
Dim cell4 As TableCell = New TableCell()
Dim cell5 As TableCell = New TableCell()
cell.ColumnSpan = 2
row2.Cells.Add(cell)
cell.Text = "Employee Info"
'cell.Width = 181
cell.CssClass = "Header"
cell.Style.Add("border-right-width", "1px")
cell.Style.Add("border-right-color", "black")
cell.Style.Add("border-right-style", "solid")

cell2.ColumnSpan = 5
row2.Cells.Add(cell2)
cell2.Text = "Troubles"
cell2.CssClass = "Header"
cell2.Style.Add("border-right-width", "1px")
cell2.Style.Add("border-right-color", "black")
cell2.Style.Add("border-right-style", "solid")

cell3.ColumnSpan = 4
row2.Cells.Add(cell3)
cell3.Text = "Service Orders"
cell3.CssClass = "Header"
cell3.BorderColor = System.Drawing.Color.Black
cell3.Style.Add("border-right-width", "1px")
cell3.Style.Add("border-right-color", "black")
cell3.Style.Add("border-right-style", "solid")

cell4.ColumnSpan = 2
row2.Cells.Add(cell4)
cell4.Text = "Work Requests"
cell4.CssClass = "Header"
cell4.Style.Add("border-right-width", "1px")
cell4.Style.Add("border-right-color", "black")
cell4.Style.Add("border-right-style", "solid")

cell5.ColumnSpan = 2
row2.Cells.Add(cell5)
cell5.Text = "PRs"
cell5.CssClass = "Header"
cell5.BorderColor = System.Drawing.Color.Black
For Each cell In row2.Cells
cell.Font.Size = 11
Next
tbl.Rows.Add(row2)
'create a new cell within the gridview row
Dim cellGRV As TableCell = New TableCell()
cellGRV.ColumnSpan = aTD.Length
grvrow.Cells.Add(cellGRV)
cellGRV.Controls.Add(tbl)
For Each cell In row.Cells
cell.Font.Size = 8
cell.ForeColor = Drawing.Color.Black
Next
Dim tech As TableCell = row.Cells(0)
tech.Width = Unit.Pixel(139)
Dim ID As TableCell = row.Cells(1)
ID.Width = Unit.Pixel(42)
ID.Style.Add("border-right-width", "1px")
ID.Style.Add("border-right-color", "black")
ID.Style.Add("border-right-style", "solid")
Dim TTs As TableCell = row.Cells(2)
TTs.Width = Unit.Pixel(42)
Dim TThours As TableCell = row.Cells(3)
TThours.Width = Unit.Pixel(64)
Dim TTHPU As TableCell = row.Cells(4)
TTHPU.Width = 54
Dim TTRPT As TableCell = row.Cells(5)
TTRPT.Width = 64
Dim SPUtil As TableCell = row.Cells(6)
SPUtil.Width = 54
SPUtil.Style.Add("border-right-width", "1px")
SPUtil.Style.Add("border-right-color", "black")
SPUtil.Style.Add("border-right-style", "solid")
Dim SOs As TableCell = row.Cells(7)
SOs.Width = 42
Dim SOHours As TableCell = row.Cells(8)
SOHours.Width = 64
Dim SOHPU As TableCell = row.Cells(9)
SOHPU.Width = 54
Dim IRPT As TableCell = row.Cells(10)
IRPT.Width = 64
IRPT.Style.Add("border-right-width", "1px")
IRPT.Style.Add("border-right-color", "black")
IRPT.Style.Add("border-right-style", "solid")
Dim WR As TableCell = row.Cells(11)
WR.Width = 42
Dim WRHours As TableCell = row.Cells(12)
WRHours.Width = 64
WRHours.Style.Add("border-right-width", "1px")
WRHours.Style.Add("border-right-color", "black")
WRHours.Style.Add("border-right-style", "solid")

Dim PR As TableCell = row.Cells(13)
PR.Width = 42
Dim PRHours As TableCell = row.Cells(14)
PRHours.Width = 64


tbl.Rows.Add(row)

End If
End Sub
 
W

Walter Wang [MSFT]

Hi,

Thank you for your post.

Based on my understanding, you want to create an additional row above the
existing header row of GridView.

During the RowCreated event, the e.Row is not added to the GridView's
table.Rows collection yet. Thus it's a good oppotunity to add your own row
to the rows collection.

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If (e.Row.RowType = DataControlRowType.Header) Then
Dim row As GridViewRow = New GridViewRow(0, 0,
DataControlRowType.Header, DataControlRowState.Normal)
row.Cells.Add(CreateCell(1, ""))
row.Cells.Add(CreateCell(2, "Product Info"))
row.Cells.Add(CreateCell(2, "Category Info"))
CType(GridView1.Controls(0), Table).Rows.Add(row)
End If
End Sub

Private Function CreateCell(ByVal colSpan As Integer, ByVal text As
String) As TableCell
Dim cell As New TableCell
cell.Text = text
cell.ColumnSpan = colSpan
Return cell
End Function

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi,

In my previous reply, I'm using "CType(GridView.Controls(0), Table)" which
is undocumented and may change in future version of GridView.

Although the GridView doesn't expose the Table directly, fortunately it has
an overrideable method called CreateChildTable() which is used to create
the Table control. So a more reliable solution would be derive from
GridView and overrides this CreateChildTable() method to get this Table
control reference:

Namespace MyControls
Public Class MyGridView
Inherits GridView

Private _table As Table

Public ReadOnly Property Table() As Table
Get
Return _table
End Get
End Property

Protected Overrides Function CreateChildTable() As Table
_table = MyBase.CreateChildTable()
Return _table
End Function

End Class
End Namespace

Then use this GridView in your WebForm and handle its RowCreated event:

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If (e.Row.RowType = DataControlRowType.Header) Then
Dim row As GridViewRow = New GridViewRow(0, 0,
DataControlRowType.Header, DataControlRowState.Normal)
row.Cells.Add(CreateCell(1, ""))
row.Cells.Add(CreateCell(2, "Product Info"))
row.Cells.Add(CreateCell(2, "Category Info"))
GridView1.Table.Rows.Add(row)
End If
End Sub

Hope this helps. Please feel free to post here if anything is unclear.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

pickedaname

Thanks Walter, when you posted about the row created event, I went back to
it. The createcell() function was alsoa a helpful idea (should have thought
of it) thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top