Solved

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

Posted on 2010-11-16
2
526 Views
Last Modified: 2012-05-10
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
Comment
Question by:migusto
2 Comments
 
LVL 13

Accepted Solution

by:
AngryBinary earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:migusto
Comment Utility
that works, thank you for the quick response. ()'s instead of [] for the datakeys parameter though.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now