I have an asp:Gridview
that displays the amount of time a staff member spends on a project and their comments. I wish to make the date a sub heading within the grid, so it is not displayed on every line of the grid. The grid cannot be sorted. The grid is within a usercontol, which is within another usercontrol. The columns from the database query are:
- Date
- Project ID
- Project Name/code
- Staff comment
- Time spent
The gridview is defined as:
<asp:GridView ID="grdTimeSheet"
runat="server"
AutoGenerateColumns="False"
CssClass="mGrid"
AllowPaging="false"
AllowSorting="false"
BorderStyle="None"
GridLines="None"
DataKeyNames="ItemID"
CaptionAlign="Top"
CellPadding="1"
CellSpacing="1"
ShowFooter="True"
OnRowDataBound="grdTimeSheet_RowDataBound"
Width="100%"
OnRowCommand="grdTimeSheet_OnRowCommand" >
Within the GridView I have a Link Button:
<asp:LinkButton
ID="ItemLinkButton"
runat="server"
CommandArgument='<%# Eval("ID") %>'
CommandName="Select" Text='<%# Eval("ProjectNo") %>' >
</asp:LinkButton>
The OnRowDataBound
event in the code behind checks to see whether the date on the record has changed and if so, adds a new row to the grid to display the date. This process displays okay.
The OnRowCommand
checks the CommandName
value and if it equals Select
I retrieve the project ID from the CommandArgument
. This works fine for all rows except the one directly under the date sub heading.
I have used the following code to create the sub heading:
Table tbl = e.Row.Parent as Table;
if (tbl != null)
{
GridViewRow row =
new GridViewRow(
0,
0,
DataControlRowType.DataRow,
DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.ColumnSpan = this.grdTimeSheet.Columns.Count;
cell.Width = Unit.Percentage(100);
cell.Style.Add("font-weight", "bold");
cell.Style.Add("color", "black");
HtmlGenericControl span = new HtmlGenericControl("span");
span.InnerHtml = currentDate;
cell.Controls.Add(span);
row.Cells.Add(cell);
tbl.Rows.AddAt(tbl.Rows.Count - 1, row);
}
The problem is the CommandArgument
is returning blank for the first record under the subheading. All other links are okay. If I get rid of the subheadings everything works okay.
protected void grdTimeSheet_OnRowCommand(
object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
selectedItemEvent(int.Parse(e.CommandArgument.ToString()));
}
}
I have found a couple of examples of people with the same issue, but no real answers.