Solved

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

Posted on 2010-11-16
2
529 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
ID: 34149888
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
ID: 34150078
that works, thank you for the quick response. ()'s instead of [] for the datakeys parameter though.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

809 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