views:

44

answers:

3

Is it possible to have a Checkbox that only shows up when Editing the last row of a GridView?

I have tried something like this in the EditItemTemplate:

<asp:CheckBox ID="chkNextDay" runat="server" 
              ToolTip="Is it a next-day departure?"
              Enabled="true" 
              Checked='<%# DateTime.Parse(Eval("OutHour","{0:d}")).Date >
                           DateTime.Parse(Eval("InHour","{0:d}")).Date  %>'/>

Then on code-behind I tried hiding it for rows other than the last one like this:

protected void grvOutHour_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView grvOutHour = (GridView)this.grvReport.Rows[grvReport.EditIndex].FindControl("grvOutHour");
            TextBox txtBox = (TextBox)grvOutHour.Rows[e.NewEditIndex].FindControl("txtEditOutHour");
            CheckBox nextDay = (CheckBox)grvOutHour.Rows[e.NewEditIndex].FindControl("chkNextDay");
            if (grvOutHour.Rows.Count-1 != e.NewEditIndex)
                nextDay.Visible = false;
        }

This ALMOST worked, but the checkbox kept showing for all fields, I think because the RowDataBound is called AFTER RowEditing again so it renders the whole thing again :(

Any suggestions?

Thanks, EtonB.

A: 

I guess it's more of a hack than an elegant solution, but I would probably just hide the other checkboxes via JavaScript if the condition is true.

GCATNM
+3  A: 

Use RowDataBound instead...

protected void grvOutHour_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowState == DataControlRowState.Edit)
    {
        GridView grid = (GridView)sender;
        CheckBox nextDay = (CheckBox)e.Row.FindControl("chkNextDay");
        nextDay.Visible = (e.Row.RowIndex == (grid.Rows.Count - 1));
    }
}
Josh Stodola
Makes sense to me, however nextDay is coming up as null. I assume it can't find the control because its inside a EditItemTemplate or something ?
Eton B.
@Eton Try latest edit, changed to check RowState to be in Edit mode
Josh Stodola
@JoshStodola Thanks, now I got a weird problem.. grid.Rows.Count changes depending on what row I am editing ? Also, I have 3 rows but when Editing the middle one it doesn't enter that method, as if it never goes into Edit state..
Eton B.
Solved my problem by modifying the condition inside your if and using LINQ's count to count the number of results instead of rows.count . Thanks!
Eton B.
A: 

You will need to handle hiding the checkbox in the RowDataBound event.

You'll need to determine what the last row is, and set the checkboxes visible property to true when that condition is true, obviously.

Jack Marchetti