GridView DataKeys problem

G

Guest

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
 
R

Riki

Greg said:
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.
 
G

Guest

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

'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
 
G

Guest

Greg said:
'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
 
G

Guest

Greg said:
'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
 
G

Guest

Greg said:
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.
 
D

Davior

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..
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top