OnEditCommand : Nested DataGrid referencing : Code included

C

Chris

I've been searching all over and think I am close, but keep getting the
error "Index out of range" when trying to reference a nested datagrid when
an OnEditCommand event is raised.

When the OnEditCommand event is raised, I try to do the following:

1. Find the selected datalist item in which the nested datagrid raised the
event. (by calling getDataGridReference and returning the DataList's
selectedIndex)

2. Once I know the selected datalist item, find the datagrid control within
that datalist item and set the selected row to edit mode.

The error occurs in my getDataGridReference() function which tells me what
DataList item contains the "firing" datagrid. What am I doing wrong?



<asp:datalist OnItemDataBound="OnItemDataBoundEventHandler"
id="datalist_ProjectPhasesResources"
Runat="server" DataKeyField="project_phase_id_pk">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,
"project_phase_name").ToString() %>

---------> Here is my nested DataGrid<--------
<asp:datagrid id="grid_ProjectPhasesResources"
RUNAT="server" DataKeyField="ppr_id_pk"
OnItemDataBound="dgPROnItemDataBoundEventHandler"
OnEditCommand="pprOnEdit" OnCancelCommand="pprOnCancel"
OnUpdateCommand="pprUpdate">
<Columns>
<asp:BoundColumn DataField="ppr_id_pk" Visible="False"
Readonly="True" HeaderText=""></asp:BoundColumn>
<ItemTemplate>
<asp:Label Runat="server" ID="lblTotalHours" Text='<%#
Convert.ToString(DataBinder.Eval(Container.DataItem,
"ppr_project_resource_hours"))%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Font-Size="8pt" Font-Name="Arial" Runat="server"
ID="editTotalHours" Text='<%#
Convert.ToString(DataBinder.Eval(Container.DataItem,
"ppr_project_resource_hours"))%>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Save"/>
</Columns>
</asp:datagrid>
------> End of nested DataGrid <-----------

</ItemTemplate>
<SeparatorTemplate>
<br/>
</SeparatorTemplate>
</asp:datalist>


Code Behind event handler for datagrid
.............

//When edit button is clicked, switch to textbox mode
public void pprOnEdit(Object source, DataGridCommandEventArgs e)
{
//Get a reference to the Grid in the current DataList row
DataGrid gridSelected = getDataGridReference();

//Set the EditItemIndex to this Grid's item index
gridSelected.EditItemIndex = e.Item.ItemIndex;

//Call function to bind the grid again
BindGrid();
}


//Get the selected DataList item index so we can find the current DataGrid
item to edit
public System.Web.UI.WebControls.DataGrid getDataGridReference()
{
DataListItem datalistitem =
datalist_ProjectPhasesResources.Items[datalist_ProjectPhasesResources.SelectedIndex];
return (DataGrid)datalistitem.FindControl("grid_ProjectPhasesResources");
}
 

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

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top