views:

34

answers:

1

Hi all,

Following up on the question here: http://stackoverflow.com/questions/3936389/complex-editing-on-a-gridview-in-c

I have the following example gridview: alt text

What I'm trying to do is that whenever I click on "Edit", the Leaves At column becomes a gridview in which the Hours for the respective date appear. However, I don't know how to retrieve the respective date as a parameter for the child GridView's dataSource.

For example, I have tried:

<SelectParameters>
                <asp:ControlParameter ControlID="cphContent$EntryDate" Name="pDate"
                     PropertyName="MyDate" Type="DateTime" />
</SelectParameters>

But it doesn't work, and even if it did how can I tell its retrieving the respective date for the selected row?

If anyone knows a better approach it would be much appreciated.

Thanks in advance, Eton B.

+1  A: 

I don't think you can use a gridview BoundField as a control parameter in that way. What I would do is:

  1. Set the date as a datakey in the Parent GridView
  2. Add an event handler for the parent GridView RowEditing event.
  3. In that event set the SelectParameter of the datasource to the date of the edited row

The parent GridView would look like:

<asp:GridView ID="ParentGridView" runat="server" DataSourceID="ParentObjectDataSource"
    OnRowEditing="ParentGridView_RowEditing" DataKeyNames="Date">

Then you would have a datasource for the child GridView like so:

<asp:ObjectDataSource ID="ChildObjectDataSource" runat="server" SelectMethod="Blah" TypeName="Blah">
    <SelectParameters>
        <asp:Parameter Type="DateTime" Name="Date" />
    </SelectParameters>
</asp:ObjectDataSource>

Then in the code behind, you would have the RowEditing event handler like this:

protected void ParentGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    ChildObjectDataSource.SelectParameters["Date"].DefaultValue = GridView1.DataKeys[e.NewEditIndex][0].ToString();
}
kevev22
Giving this a shot, thanks. In the code behind, don't you mean ChildObjectDataSource.SelectParameters?
Eton B.
Thank you, yes I did. I edited the answer to reflect that correction.
kevev22
Thanks, so far your solution looks good. I am no longer getting errors and what you're doing makes sense. However, upon clicking on Edit the Leaves At column is literally empty. Here's the markup for the child gridview I'm using: http://pastebin.com/nUkCy4iu
Eton B.
Put a breakpoint in the SelectMethod for odsOutHours and see what it is returning. Could it be a problem with how you are comparing dates?
kevev22
That was it! I made a mistake in the comparison criteria. Awesome. Now I got the gridview displayed and able to move forward... expect another question popping up from me soon :P Thanks!
Eton B.