Get DataKey value in GridView

D

David C

I am using a GridView to Edit data in a table. The data key in the table is
the column named ContactID and it is in a TemplateField that is
Visible=false. I want to use its value and place it in a textbox with an
ID=txtcnid and have the following code that is failing. Can someone help on
this? Thanks.
David

Protected Sub gvContacts_RowEditing(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs)
txtcnid.Text = gvContacts.DataKeys(gvContacts.EditIndex).ToString
End Sub
 
D

David C

Sorry. I am trying to get the Int32 value of the database column named
ContactID when I edit a GridView row. The GridView has the following info

<asp:GridView ID="gvContacts" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ContactID" DataSourceID="SqlContacts"
EmptyDataText="There are no contacts linked to this
company."
CellPadding="4" ForeColor="#333333" GridLines="None"
PagerSettings-PageButtonCount="10"
PageSize="6" EmptyDataRowStyle-ForeColor="Red"
OnRowUpdated="gvContacts_RowUpdated"
OnRowDataBound="gvContacts_RowDataBound"
OnRowEditing="gvContacts_RowEditing">
<Columns>
<asp:TemplateField HeaderText="ContactID"
InsertVisible="False" ShowHeader="False"
SortExpression="ContactID" Visible="False">
<EditItemTemplate>
<asp:Label ID="LblContactID" runat="server"
Text='<%# Eval("ContactID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblContactID" runat="server"
Text='<%# Bind("ContactID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="More Phones">
<ItemTemplate>
<asp:LinkButton ID="LBtnMorePhones" runat="server"
CausesValidation="False" Text="View/Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LBtnCnEdit" runat="server"
CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LBtnCnUpdate" runat="server"
CausesValidation="False" CommandName="Update"
Text="Update"></asp:LinkButton>
<br /><br />
<asp:LinkButton ID="LBtnCnCancel" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact"
SortExpression="Contact">
<EditItemTemplate>
<table>
<tr>
<td><asp:Label ID="LblFirstName" runat="server"
Text="First Name:"></asp:Label></td>
<td><asp:TextBox ID="txtFirstName"
runat="server" Text='<%# Bind("FirstName") %>'
MaxLength="50"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="LblMiddleName" runat="server"
Text="Middle Name:"></asp:Label></td>
<td><asp:TextBox ID="txtMiddleName"
runat="server" Text='<%# Bind("MiddleName") %>'
Width="100px"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="LblLastName" runat="server"
Text="Last Name:"></asp:Label></td>
<td><asp:TextBox ID="txtLastName" runat="server"
Text='<%# Bind("LastName") %>' MaxLength="50"></asp:TextBox></td>
</tr>
</table>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblContact" runat="server" Text='<%#
Bind("Contact") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title"
SortExpression="Title">
<EditItemTemplate>
<asp:TextBox ID="txtContactTitle" runat="server"
Text='<%# Bind("Title") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblContactTitle" runat="server"
Text='<%# Bind("Title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Direct"
SortExpression="MainPhone">
<EditItemTemplate>
<asp:TextBox ID="txtMainPhone" runat="server"
Text='<%# Bind("MainPhone") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblMainPhone" runat="server"
Text='<%# Bind("MainPhone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cell Phone"
SortExpression="CellPhone">
<EditItemTemplate>
<asp:TextBox ID="txtCellPhone" runat="server"
Text='<%# Bind("CellPhone") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblCellPhone" runat="server"
Text='<%# Bind("CellPhone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email"
SortExpression="Email">
<EditItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text='<%#
Bind("Email") %>' MaxLength="75" Width="250px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HyperLink ID="HLEmail" runat="server" Text='<%#
Eval("Email") %>' NavigateUrl='<%# Eval("EmailURL") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes"
SortExpression="Notes">
<EditItemTemplate>
<asp:TextBox ID="txtNotes" runat="server" Text='<%#
Bind("Notes") %>' TextMode="MultiLine" Rows="6" Width="300"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblNotes" runat="server" Text='<%#
Eval("Notes") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Active">
<EditItemTemplate>
<asp:CheckBox ID="ckActive" runat="server"
Checked='<%# Bind("Active") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="ckActive" runat="server"
Checked='<%# Bind("Active") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ContactsID"
HeaderText="ContactsID" InsertVisible="False"
ReadOnly="True" SortExpression="ContactsID"
Visible="False" />
<asp:BoundField DataField="CompanyID" HeaderText="CompanyID"
InsertVisible="False"
ReadOnly="True" SortExpression="CompanyID"
Visible="False" />
<asp:TemplateField InsertVisible="False" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LBtnDelContact" runat="server"
CausesValidation="False" CommandName="Delete"
Text="Delete" OnClientClick="return
ConfirmDel('Contact');" ForeColor="Red"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PhonesCount" HeaderText="Addl
Phones" InsertVisible="False"
ReadOnly="True" SortExpression="PhonesCount"
Visible="False" />
<asp:TemplateField HeaderImageUrl="Images/mailto.gif">
<ItemTemplate>
<asp:HyperLink ID="HLEnv" runat="server"
NavigateUrl="frmShowReport.aspx"
Target="_blank" Text="Env"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="Images/letter.gif">
<ItemTemplate>
<asp:HyperLink ID="HLLtr" runat="server"
NavigateUrl="frmToWord.aspx"
Target="_blank" Text="Ltr"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLinkContactID" runat="server"
Text='<%# Bind("LinkContactID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="LightBlue" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"
ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<PagerSettings PageButtonCount="20" />
<EmptyDataRowStyle ForeColor="Red" />
</asp:GridView>

I am trying to grab the value in the LblContactID.Text control using the
following code:

Protected Sub gvContacts_RowEditing(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs)
txtcnid.Text = gvContacts.DataKeys(gvContacts.EditIndex).ToString
End Sub

The above code is returning

Exception: System.ArgumentOutOfRangeException
Message: Index was out of range. Must be non-negative and less than the size
of the collection. Parameter name: index
 
D

David C

It is meaningless to the user. I tried using the Command event of the
LinkButton used to edit the GridView. I added the CommandArgument and set
its value to the ContactID and it worked. Should have thought of that
before. I just thought there was a way to retrieve it in the Editing event.

David
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top