471,599 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

GridView DataKeys problem

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

Oct 30 '06 #1
6 17702
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
Oct 30 '06 #2
"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.RowInd ex).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
Oct 30 '06 #3
"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.RowInd ex).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

Oct 30 '06 #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.RowInd ex).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

Oct 30 '06 #5
"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.RowInd ex).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.
Nov 1 '06 #6
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..

Dec 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by cheloman12 | last post: by
1 post views Thread by cheloman12 | last post: by
reply views Thread by ssims | last post: by
4 posts views Thread by =?Utf-8?B?Y2hyaXM=?= | last post: by
reply views Thread by aj123 | last post: by
1 post views Thread by jc | last post: by
2 posts views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.