the berserker
the berserker

Reputation: 1594

ASP.Net GridView GridViewDeleteEventArgs.Keys empty

I have following Gridview:

<asp:GridView ID="GridView1" runat="server" CssClass="table" DataKeyNames="groupId"
          DataSource="<%# dsUserGroupsSelected %>" DataMember="Group" etc....>

and after firing RowDeleting event handler:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

e.Keys is empty. Moreover, in runtime if I check

dsUserGroupsSelected.Group.PrimaryKey

it is poulated with:

{System.Data.DataColumn[1]}
    [0]: {groupId}

so it's really odd to me. Am I missing something? I have this kind of a workaround:

int groupId = (int)GridView1.DataKeys[e.RowIndex].Value;

which will work just fine, but I just can't get it why e.Keys (and e.Values) would be empty!? Any ideas?

Upvotes: 2

Views: 3252

Answers (4)

Biff MaGriff
Biff MaGriff

Reputation: 8241

It looks like this behaviour is intentional.

From http://forums.asp.net/p/1050092/2128091.aspx

Looking in Reflector at Gridview.HandleDelete(), it appears that e.Keys and e.Values are only populated if gridview.IsBoundUsingDataSourceID. That is, if you set the DataSource in code then none of this will work. Good one, Microsoft! Might have been useful to mention that in the help perhaps??!! Lachlan

Edit:

I ended up making my own data objects and put them in the app_code folder

ex.

public class CustomDataViews
{
    public class FileQuery
    {
        public class File
        {
            public DateTime CreatedDate { get; set; }
            public string FileName { get; set; }
            public string Path { get; set; }
            public void Delete() { }
        }

        public ArrayList GetFiles()
        {
            System.Collections.ArrayList files = new ArrayList();
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath("~/UserUpload/"));
            foreach (System.IO.FileInfo fi in (from a in di.GetFiles() orderby a.CreationTime descending select a))
            {
                File myFile = new File();
                myFile.CreatedDate = fi.CreationTime;
                myFile.FileName = fi.Name;
                myFile.Path = "/VACWeb/UserUpload/" + fi.Name;
                files.Add(myFile);
            }
            return files;
        }

        public void Delete(string FileName)
        {
            if (FileName != null)
            {
                string path = HttpContext.Current.Server.MapPath("~/UserUpload/") + FileName;
                if (System.IO.File.Exists(path))
                    System.IO.File.Delete(path);
            }
        }
    }
}

aspx

<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="False" DataKeyNames="FileName"
    DataSourceID="ods1">
    <Columns>
        <cc:ExtendedCommandField DeleteConfirmationText="Are you sure you wish to delete this file?"
            DeleteText="Delete" ShowDeleteButton="true" />
        <asp:BoundField DataField="CreatedDate" HeaderText="Created Date" DataFormatString="{0:MM/dd/yyyy}" />
        <asp:BoundField DataField="FileName" HeaderText="File Name" />
        <asp:ImageField DataImageUrlField="Path" AlternateText="No Image" HeaderText="Image Preview"
            ControlStyle-Width="100px">
            <ControlStyle Width="100px" />
        </asp:ImageField>
        <asp:BoundField DataField="Path" HeaderText="Path" />
        <asp:HyperLinkField DataNavigateUrlFields="Path" DataTextField="FileName" HeaderText="Link" />
    </Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ods1" runat="server" DeleteMethod="Delete" SelectMethod="GetFiles"
    TypeName="CustomDataViews+FileQuery">
    <DeleteParameters>
        <asp:Parameter Name="FileName" Type="String" />
    </DeleteParameters>
</asp:ObjectDataSource>

Upvotes: 4

Cobra
Cobra

Reputation: 352

Apparently, the GridViewDeleteEventArgs.Keys property only works if you set the GridView's DataSource by setting the DataSourceID property -- i.e. setting the DataSource property and then manually calling DataBind() leaves the Keys property (and the Values property as well) empty.

Source: http://forums.asp.net/t/1050092.aspx

Upvotes: 0

codes_occasionally
codes_occasionally

Reputation: 9566

Is it possible you're programatically sorting your gridview in your Page_Load method? If so, try moving the sort into the Page_Init method, and see if that fixes the problem.

Upvotes: 0

angryprogrammer
angryprogrammer

Reputation: 21

Simple! ASP is a big piece of .... and GridView is a small piece in the big peace. Have the same problem, and while the workaround is ok for deleting, updating becomes really interesting... The actual problem is it seems that for no good or apparent reason a looooooooot of the functionality of GridView is missing if the DataSource is not a DataSourceControl: Like filling the keys & values properties in the eventargs. Enjoy! Viva Microsoft!

Upvotes: 1

Related Questions