GridView DataKeys problem

Discussion in 'ASP .Net' started by =?Utf-8?B?R3JlZw==?=, Oct 30, 2006.

  1. Hello,

    I have a GridView bound to a custom object. I set the DataKeyNames property
    along with the column DataField properties at design time, and bind the
    GridView to my object at run-time. In the RowDeleting event, I try to access
    the DataKeys property, but it contains no elements.

    This is the first time I'm trying to use DatKeyNames and DataKeys. What am
    I missing here?

    Here is my code for the GridView:

    <asp:GridView ID="grdPrograms" runat="server" AutoGenerateColumns="False"
    DataKeyNames="ProgramID">
    <Columns>
    <asp:CommandField DeleteText="Remove" ShowDeleteButton="True" />
    <asp:BoundField HeaderText="Residency Program" DataField="ProgramName"
    ReadOnly="True" />
    <asp:BoundField HeaderText="Start Date" DataField="StartDate"
    HtmlEncode="False" DataFormatString="{0:d}" />
    <asp:BoundField HeaderText="End Date" DataField="EndDate" HtmlEncode="False"
    DataFormatString="{0:d}" />
    </Columns>
    </asp:GridView>

    And here is my code to bind it:

    grdPrograms.DataSource = Me.EditUser.GetUserPrograms().Values
    grdPrograms.DataBind()

    GetUserPrograms() returns a SortedDictionary(Of Integer, UserProgram). The
    UserProgram class has properties ProgramName, StartDate, EndDate, and
    ProgramID.

    Any help here would be greatly appreciated!

    Thank you,
    Greg
     
    =?Utf-8?B?R3JlZw==?=, Oct 30, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?R3JlZw==?=

    Riki Guest

    Greg wrote:
    > Hello,
    >
    > I have a GridView bound to a custom object. I set the DataKeyNames
    > property along with the column DataField properties at design time,
    > and bind the GridView to my object at run-time. In the RowDeleting
    > event, I try to access the DataKeys property, but it contains no
    > elements.
    >
    > This is the first time I'm trying to use DatKeyNames and DataKeys.
    > What am I missing here?
    >
    > Here is my code for the GridView:
    >
    > <asp:GridView ID="grdPrograms" runat="server"
    > AutoGenerateColumns="False" DataKeyNames="ProgramID">
    > <Columns>
    > <asp:CommandField DeleteText="Remove" ShowDeleteButton="True" />
    > <asp:BoundField HeaderText="Residency Program" DataField="ProgramName"
    > ReadOnly="True" />
    > <asp:BoundField HeaderText="Start Date" DataField="StartDate"
    > HtmlEncode="False" DataFormatString="{0:d}" />
    > <asp:BoundField HeaderText="End Date" DataField="EndDate"
    > HtmlEncode="False" DataFormatString="{0:d}" />
    > </Columns>
    > </asp:GridView>
    >
    > And here is my code to bind it:
    >
    > grdPrograms.DataSource = Me.EditUser.GetUserPrograms().Values
    > grdPrograms.DataBind()
    >
    > GetUserPrograms() returns a SortedDictionary(Of Integer,
    > UserProgram). The UserProgram class has properties ProgramName,
    > StartDate, EndDate, and ProgramID.
    >
    > Any help here would be greatly appreciated!
    >
    > Thank you,
    > Greg


    This code seems ok to me, but you forgot to give us the code of your
    RowDeleting event.

    --

    Riki
     
    Riki, Oct 30, 2006
    #2
    1. Advertising

  3. "Riki" wrote:

    > Greg wrote:
    > > Hello,
    > >
    > > I have a GridView bound to a custom object. I set the DataKeyNames
    > > property along with the column DataField properties at design time,
    > > and bind the GridView to my object at run-time. In the RowDeleting
    > > event, I try to access the DataKeys property, but it contains no
    > > elements.
    > >
    > > This is the first time I'm trying to use DatKeyNames and DataKeys.
    > > What am I missing here?
    > >
    > > Here is my code for the GridView:
    > >
    > > <asp:GridView ID="grdPrograms" runat="server"
    > > AutoGenerateColumns="False" DataKeyNames="ProgramID">
    > > <Columns>
    > > <asp:CommandField DeleteText="Remove" ShowDeleteButton="True" />
    > > <asp:BoundField HeaderText="Residency Program" DataField="ProgramName"
    > > ReadOnly="True" />
    > > <asp:BoundField HeaderText="Start Date" DataField="StartDate"
    > > HtmlEncode="False" DataFormatString="{0:d}" />
    > > <asp:BoundField HeaderText="End Date" DataField="EndDate"
    > > HtmlEncode="False" DataFormatString="{0:d}" />
    > > </Columns>
    > > </asp:GridView>
    > >
    > > And here is my code to bind it:
    > >
    > > grdPrograms.DataSource = Me.EditUser.GetUserPrograms().Values
    > > grdPrograms.DataBind()
    > >
    > > GetUserPrograms() returns a SortedDictionary(Of Integer,
    > > UserProgram). The UserProgram class has properties ProgramName,
    > > StartDate, EndDate, and ProgramID.
    > >
    > > Any help here would be greatly appreciated!
    > >
    > > Thank you,
    > > Greg

    >
    > This code seems ok to me, but you forgot to give us the code of your
    > RowDeleting event.
    >
    > --
    >
    > Riki


    'handle Delete in object
    Dim gv As GridView = sender
    Me.EditUser.RemoveFromProgram(gv.DataKeys(e.RowIndex).Value)

    'rebind grid
    grdPrograms.DataSource = Me.EditUser.GetUserPrograms().Values
    grdPrograms.DataBind()


    I get an ArgumentOutOfRangeException on the second line. There are 2 rows
    in the grid, and e.RowIndex=0. gv.DataKeys(e.RowIndex).Value throws the
    exception. I checked gv.DataKeys.Count in the Immediate window and got 0,
    but gv.DataKeyNames.Length returns 1.

    I also tried adding a column to the GridView for ProgramID, to make sure it
    would show up there, and it does. It just doesn't show up in DataKeys.

    Thanks for taking a look at this, I've been pulling my hair out over this
    for a day and a half now!

    Greg
     
    =?Utf-8?B?R3JlZw==?=, Oct 30, 2006
    #3
  4. "Greg" wrote:

    > "Riki" wrote:
    >
    > > Greg wrote:
    > > > Hello,
    > > >
    > > > I have a GridView bound to a custom object. I set the DataKeyNames
    > > > property along with the column DataField properties at design time,
    > > > and bind the GridView to my object at run-time. In the RowDeleting
    > > > event, I try to access the DataKeys property, but it contains no
    > > > elements.
    > > >
    > > > This is the first time I'm trying to use DatKeyNames and DataKeys.
    > > > What am I missing here?
    > > >
    > > > Here is my code for the GridView:
    > > >
    > > > <asp:GridView ID="grdPrograms" runat="server"
    > > > AutoGenerateColumns="False" DataKeyNames="ProgramID">
    > > > <Columns>
    > > > <asp:CommandField DeleteText="Remove" ShowDeleteButton="True" />
    > > > <asp:BoundField HeaderText="Residency Program" DataField="ProgramName"
    > > > ReadOnly="True" />
    > > > <asp:BoundField HeaderText="Start Date" DataField="StartDate"
    > > > HtmlEncode="False" DataFormatString="{0:d}" />
    > > > <asp:BoundField HeaderText="End Date" DataField="EndDate"
    > > > HtmlEncode="False" DataFormatString="{0:d}" />
    > > > </Columns>
    > > > </asp:GridView>
    > > >
    > > > And here is my code to bind it:
    > > >
    > > > grdPrograms.DataSource = Me.EditUser.GetUserPrograms().Values
    > > > grdPrograms.DataBind()
    > > >
    > > > GetUserPrograms() returns a SortedDictionary(Of Integer,
    > > > UserProgram). The UserProgram class has properties ProgramName,
    > > > StartDate, EndDate, and ProgramID.
    > > >
    > > > Any help here would be greatly appreciated!
    > > >
    > > > Thank you,
    > > > Greg

    > >
    > > This code seems ok to me, but you forgot to give us the code of your
    > > RowDeleting event.
    > >
    > > --
    > >
    > > Riki

    >
    > 'handle Delete in object
    > Dim gv As GridView = sender
    > Me.EditUser.RemoveFromProgram(gv.DataKeys(e.RowIndex).Value)
    >
    > 'rebind grid
    > grdPrograms.DataSource = Me.EditUser.GetUserPrograms().Values
    > grdPrograms.DataBind()
    >
    >
    > I get an ArgumentOutOfRangeException on the second line. There are 2 rows
    > in the grid, and e.RowIndex=0. gv.DataKeys(e.RowIndex).Value throws the
    > exception. I checked gv.DataKeys.Count in the Immediate window and got 0,
    > but gv.DataKeyNames.Length returns 1.
    >
    > I also tried adding a column to the GridView for ProgramID, to make sure it
    > would show up there, and it does. It just doesn't show up in DataKeys.
    >
    > Thanks for taking a look at this, I've been pulling my hair out over this
    > for a day and a half now!
    >
    > Greg



    More information: grdPrograms.DataKeys *does* contain the keys for the rows
    after DataBind() is executed. However, it seems to lose its values as soon
    as I click anything that posts back to the server - the Delete button on the
    GridView, another button on the page,... Is this property supposed to lose
    its values on postback? I thought the DataKeys were supposed to be used in
    place of invisible columns because DataKeys would be accessible on postback
    and invisible columns were not.

    Thanks,
    Greg
     
    =?Utf-8?B?R3JlZw==?=, Oct 30, 2006
    #4
  5. "Greg" wrote:

    > "Riki" wrote:
    >
    > > Greg wrote:
    > > > Hello,
    > > >
    > > > I have a GridView bound to a custom object. I set the DataKeyNames
    > > > property along with the column DataField properties at design time,
    > > > and bind the GridView to my object at run-time. In the RowDeleting
    > > > event, I try to access the DataKeys property, but it contains no
    > > > elements.
    > > >
    > > > This is the first time I'm trying to use DatKeyNames and DataKeys.
    > > > What am I missing here?
    > > >
    > > > Here is my code for the GridView:
    > > >
    > > > <asp:GridView ID="grdPrograms" runat="server"
    > > > AutoGenerateColumns="False" DataKeyNames="ProgramID">
    > > > <Columns>
    > > > <asp:CommandField DeleteText="Remove" ShowDeleteButton="True" />
    > > > <asp:BoundField HeaderText="Residency Program" DataField="ProgramName"
    > > > ReadOnly="True" />
    > > > <asp:BoundField HeaderText="Start Date" DataField="StartDate"
    > > > HtmlEncode="False" DataFormatString="{0:d}" />
    > > > <asp:BoundField HeaderText="End Date" DataField="EndDate"
    > > > HtmlEncode="False" DataFormatString="{0:d}" />
    > > > </Columns>
    > > > </asp:GridView>
    > > >
    > > > And here is my code to bind it:
    > > >
    > > > grdPrograms.DataSource = Me.EditUser.GetUserPrograms().Values
    > > > grdPrograms.DataBind()
    > > >
    > > > GetUserPrograms() returns a SortedDictionary(Of Integer,
    > > > UserProgram). The UserProgram class has properties ProgramName,
    > > > StartDate, EndDate, and ProgramID.
    > > >
    > > > Any help here would be greatly appreciated!
    > > >
    > > > Thank you,
    > > > Greg

    > >
    > > This code seems ok to me, but you forgot to give us the code of your
    > > RowDeleting event.
    > >
    > > --
    > >
    > > Riki

    >
    > 'handle Delete in object
    > Dim gv As GridView = sender
    > Me.EditUser.RemoveFromProgram(gv.DataKeys(e.RowIndex).Value)
    >
    > 'rebind grid
    > grdPrograms.DataSource = Me.EditUser.GetUserPrograms().Values
    > grdPrograms.DataBind()
    >
    >
    > I get an ArgumentOutOfRangeException on the second line. There are 2 rows
    > in the grid, and e.RowIndex=0. gv.DataKeys(e.RowIndex).Value throws the
    > exception. I checked gv.DataKeys.Count in the Immediate window and got 0,
    > but gv.DataKeyNames.Length returns 1.
    >
    > I also tried adding a column to the GridView for ProgramID, to make sure it
    > would show up there, and it does. It just doesn't show up in DataKeys.
    >
    > Thanks for taking a look at this, I've been pulling my hair out over this
    > for a day and a half now!
    >
    > Greg



    More information: grdPrograms.DataKeys *does* contain the keys for the rows
    after DataBind() is executed. However, it seems to lose its values as soon
    as I click anything that posts back to the server - the Delete button on the
    GridView, another button on the page,... Is this property supposed to lose
    its values on postback? I thought the DataKeys were supposed to be used in
    place of invisible columns because DataKeys would be accessible on postback
    and invisible columns were not.

    Thanks,
    Greg
     
    =?Utf-8?B?R3JlZw==?=, Oct 30, 2006
    #5
  6. "Greg" wrote:

    > "Greg" wrote:
    >
    > > "Riki" wrote:
    > >
    > > > Greg wrote:
    > > > > Hello,
    > > > >
    > > > > I have a GridView bound to a custom object. I set the DataKeyNames
    > > > > property along with the column DataField properties at design time,
    > > > > and bind the GridView to my object at run-time. In the RowDeleting
    > > > > event, I try to access the DataKeys property, but it contains no
    > > > > elements.
    > > > >
    > > > > This is the first time I'm trying to use DatKeyNames and DataKeys.
    > > > > What am I missing here?
    > > > >
    > > > > Here is my code for the GridView:
    > > > >
    > > > > <asp:GridView ID="grdPrograms" runat="server"
    > > > > AutoGenerateColumns="False" DataKeyNames="ProgramID">
    > > > > <Columns>
    > > > > <asp:CommandField DeleteText="Remove" ShowDeleteButton="True" />
    > > > > <asp:BoundField HeaderText="Residency Program" DataField="ProgramName"
    > > > > ReadOnly="True" />
    > > > > <asp:BoundField HeaderText="Start Date" DataField="StartDate"
    > > > > HtmlEncode="False" DataFormatString="{0:d}" />
    > > > > <asp:BoundField HeaderText="End Date" DataField="EndDate"
    > > > > HtmlEncode="False" DataFormatString="{0:d}" />
    > > > > </Columns>
    > > > > </asp:GridView>
    > > > >
    > > > > And here is my code to bind it:
    > > > >
    > > > > grdPrograms.DataSource = Me.EditUser.GetUserPrograms().Values
    > > > > grdPrograms.DataBind()
    > > > >
    > > > > GetUserPrograms() returns a SortedDictionary(Of Integer,
    > > > > UserProgram). The UserProgram class has properties ProgramName,
    > > > > StartDate, EndDate, and ProgramID.
    > > > >
    > > > > Any help here would be greatly appreciated!
    > > > >
    > > > > Thank you,
    > > > > Greg
    > > >
    > > > This code seems ok to me, but you forgot to give us the code of your
    > > > RowDeleting event.
    > > >
    > > > --
    > > >
    > > > Riki

    > >
    > > 'handle Delete in object
    > > Dim gv As GridView = sender
    > > Me.EditUser.RemoveFromProgram(gv.DataKeys(e.RowIndex).Value)
    > >
    > > 'rebind grid
    > > grdPrograms.DataSource = Me.EditUser.GetUserPrograms().Values
    > > grdPrograms.DataBind()
    > >
    > >
    > > I get an ArgumentOutOfRangeException on the second line. There are 2 rows
    > > in the grid, and e.RowIndex=0. gv.DataKeys(e.RowIndex).Value throws the
    > > exception. I checked gv.DataKeys.Count in the Immediate window and got 0,
    > > but gv.DataKeyNames.Length returns 1.
    > >
    > > I also tried adding a column to the GridView for ProgramID, to make sure it
    > > would show up there, and it does. It just doesn't show up in DataKeys.
    > >
    > > Thanks for taking a look at this, I've been pulling my hair out over this
    > > for a day and a half now!
    > >
    > > Greg

    >
    >
    > More information: grdPrograms.DataKeys *does* contain the keys for the rows
    > after DataBind() is executed. However, it seems to lose its values as soon
    > as I click anything that posts back to the server - the Delete button on the
    > GridView, another button on the page,... Is this property supposed to lose
    > its values on postback? I thought the DataKeys were supposed to be used in
    > place of invisible columns because DataKeys would be accessible on postback
    > and invisible columns were not.
    >
    > Thanks,
    > Greg


    Sorry for the double-post before.

    If it helps anyone else get around this problem in the future, I ended up
    rebinding the GridView to my collection before accessing the DataKeys. It's
    not pretty, but it works.

    I still have no clue why the GridView was filled, but the DataKeys
    collection was empty.
     
    =?Utf-8?B?R3JlZw==?=, Nov 1, 2006
    #6
  7. =?Utf-8?B?R3JlZw==?=

    Davior Guest

    You aren't the only one that is having this problem. I have seem
    numerous posts on the issue. Yet to find a solution.

    I assume that the DataKeys array is supposed to be populated during
    databind and hang on to its values throughout all posts backs (until it
    requires binding again). These values are most likely being stored in
    viewstate by the grid but failing to be renewed after a postback.

    I have only heard of this issue occuring when using a datasource
    control.

    I hope MS does something about this as without keys the datasource
    control is unable to identify which item to delete or update..
     
    Davior, Dec 13, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TGluZ28=?=

    Gridview asp.net 2.0 datakeys not loading

    =?Utf-8?B?TGluZ28=?=, Oct 31, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    605
    =?Utf-8?B?TGluZ28=?=
    Oct 31, 2005
  2. Narshe
    Replies:
    2
    Views:
    1,047
    Narshe
    Jun 29, 2006
  3. Guest
    Replies:
    2
    Views:
    5,579
    wwwtar
    Nov 2, 2006
  4. =?Utf-8?B?UmljayBEcm9za2U=?=

    Gridview DataKeys property not set when PageIndexChanged fires

    =?Utf-8?B?UmljayBEcm9za2U=?=, Dec 13, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    397
    =?Utf-8?B?UmljayBEcm9za2U=?=
    Dec 13, 2006
  5. Benton
    Replies:
    0
    Views:
    726
    Benton
    Dec 19, 2006
Loading...

Share This Page