views:

202

answers:

2

Does anyone know how to use the gridview delete functionality with Subsonic 3? I am trying to delete rows that do not have the primary key displayed in the the gridview so I can't just pull that data from the gridview row. I was wondering if there is a way to do it with the DataKeyNames property.

Thanks..

+2  A: 

Figured it out:

This is what you do:

<asp:GridView ID="PageGrid" runat="server" OnRowDeleting="DeleteTheRow" AutoGenerateDeleteButton="true"
    AutoGenerateColumns="false" CssClass="centeredTableList" DataKeyNames="page_id">
    <Columns>
        <asp:BoundField DataField="page_name" HeaderText="Page Name" />
        <asp:HyperLinkField Text="Edit" DataNavigateUrlFormatString="p={0}"
            DataNavigateUrlFields="page_id" HeaderText="Edit"/>
    </Columns>
</asp:GridView>



protected void DeleteTheRow(Object sender, GridViewDeleteEventArgs e)
{
    int i = Convert.ToInt32(PageGrid.DataKeys[e.RowIndex].Value);    
}
A: 

I'm not sure if this is related or even applicable to a web-based Grid, but I usually use a BindingSource that I fill with my 'subsonicized' items like

bindingsource.DataSource = new ItemColleciton.Load();

Then when you need what is currently selected, I would get the Item with, for instance,

int pk = (bindingSource.Current as Item).PrimaryKey;

That way I think you get the actualy selected record, since maybe the RowIndex ina grid, may or may not be the correct one depending on the sorting or filtering?

Hope this helps.

John