Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 544
  • Last Modified:

Need help getting the value of a hidden BoundField from code-behind

I have a Gridview with a dropdown in each row that will automatically postback and update the current row based on the serviceid. However I need to hide the first column as the end users are not interested in the ID. If I make the BoundField invisble the code-behind can't see it either. Does anyone have any ideas for a way to either hide the serviceid column or another approach?

I've included the Gridview and appropriate code below to give you an idea of what I'm trying to do. Thanks!

      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSourceServices" BackColor="White" BorderColor="White" 
            BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" 
            GridLines="None" Width="800" DataKeyNames="serviceid" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
            <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
            <Columns>
                <asp:BoundField DataField="serviceid" InsertVisible="False" 
                    ReadOnly="True" SortExpression="serviceid" Visible="True" />
                <asp:BoundField DataField="servicename" HeaderText="Service" 
                    SortExpression="servicename" />
                <asp:BoundField DataField="servicetype" HeaderText="Service Type" 
                    SortExpression="servicetype" Visible="False" />
                <asp:TemplateField HeaderText="Current">
                    <ItemTemplate>
                      <asp:DropDownList ID="ddlUpdateStatus" EnableViewState="true" SelectedValue='<%# Bind("status") %>' 
                            runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlUpdateStatus_SelectedIndexChanged">
                           <asp:ListItem value="0">0</asp:ListItem>
                           <asp:ListItem value="1">1</asp:ListItem>
                           <asp:ListItem value="2">2</asp:ListItem>
                           <asp:ListItem value="3">3</asp:ListItem>
                      </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>




    Protected Sub ddlUpdateStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddlCurrentDropDownList As DropDownList = DirectCast(sender, DropDownList)
        Dim grdrDropDownRow As GridViewRow = DirectCast(ddlCurrentDropDownList.Parent.Parent, GridViewRow)
        Dim selectedStatus As Integer = ddlCurrentDropDownList.SelectedItem.Value
        Dim selectedLab As String = ddlLabList.SelectedValue
       	'get the value in the first column of the current row of the GridView for SQL update
	Dim selectedService As Integer = grdrDropDownRow.Cells(0).Text
	Me.lblService.Text = selectedSservice
	SqlDataSourceServices.Update()
    End Sub

    Private Sub SqlDataSourceServices_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles 		SqlDataSourceServices.Updating
        e.Command.Parameters("@selectedService").Value = Me.lblService.Text
    End Sub

Open in new window

0
migusto
Asked:
migusto
1 Solution
 
AngryBinaryCommented:
Add the ID column (serviceid, I think?) to the DataKeyNames for the GridView:

<asp:GridView DataKeyNames="serviceid" runat="server" etc...

Your GridView will store the value in a collection accessible in your code, as long as you know the row index that you want the ID for:

GridView1.DataKeys[rowIndex].Value
0
 
migustoAuthor Commented:
that works, thank you for the quick response. ()'s instead of [] for the datakeys parameter though.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now