JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser .
Get DataKey value in GridView
Thread starter
Thread starter
David C
Start date
Start date
Dec 2, 2008
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
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
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