[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Value of label in gridview when checkbox is checked

Posted on 2007-08-10
11
Medium Priority
?
310 Views
Last Modified: 2012-08-14
I have a gridview with a checkbox field. I am looping through the gridview to get all rows that the checkbox is checked. I then am wanting to insert those items into one table in a database and then update the table that the items were pulled from.  I am able to insert the items that are checked, but am I having problems updating the one table. I have one column in the gridview that is a label holding the primary key. This field is not visible. How do I pull the value of this label so I can use it in my update statement. Here is the code I currently have:

Dim rowCollection As GridViewRowCollection = gvItems.Rows

            For Each gridRow As GridViewRow In rowCollection

                Dim ctl As Control = gridRow.FindControl("CheckBox1")
                If TypeOf ctl Is System.Web.UI.WebControls.CheckBox Then

                    Dim chk As CheckBox = CType(ctl, CheckBox)                

                    If chk.Checked Then
                        lblRoom.Text = ddRoom.SelectedItem.ToString()

                        strDash = ConfigurationManager.AppSettings("conDash")
                        conDash = New SqlConnection(strDash)
                        conDash.Open()

                        cmdSelect = New SqlCommand("dbAddWOB", conDash)
                        cmdSelect.CommandType = CommandType.StoredProcedure
                        cmdSelect.Parameters.AddWithValue("@budgetID", lbl2)
                        cmdSelect.Parameters.AddWithValue("@poID", intID)
                        cmdSelect.Parameters.AddWithValue("@subID", intsubID)
                        cmdSelect.Parameters.AddWithValue("@jobID", intJobID)
                        cmdSelect.Parameters.AddWithValue("@cat", Label1.Text)
                        cmdSelect.Parameters.AddWithValue("@itemAction", ddAction.SelectedValue)
                        cmdSelect.Parameters.AddWithValue("@roomLevel", ddLevel.SelectedValue)
                        cmdSelect.Parameters.AddWithValue("@roomName", lblRoom.Text)
                        cmdSelect.ExecuteNonQuery()

                        cmdSelect = New SqlCommand("dbUpdateBalance", conDash)
                        cmdSelect.CommandType = CommandType.StoredProcedure
                        cmdSelect.Parameters.AddWithValue("@jobID", intJobID)
                        cmdSelect.Parameters.AddWithValue("@cat", lbl)
                        cmdSelect.ExecuteNonQuery()

                        cmdSelect = New SqlCommand("dbUpdateGP", conDash)
                        cmdSelect.CommandType = CommandType.StoredProcedure
                        cmdSelect.Parameters.AddWithValue("@jobID", intJobID)
                        cmdSelect.Parameters.AddWithValue("@cat", lbl)
                        cmdSelect.ExecuteNonQuery()

                        conDash.Close()

                    End If
                End If
            Next
0
Comment
Question by:ryapos
  • 5
  • 4
  • 2
11 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 19669130
>>have one column in the gridview that is a label holding the primary key. This field is not visible.

Gridviews handle "invisible" primary key data by themselves if you use datakeys...have a look at:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19669173
Hidden columns don't get rendered to the client. If you need to get the value after postback, hide the column with css rule display:none in the on item databound event.
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 19669200
>>hide the column with css rule display:none in the on item databound event.

Why would you give that advice?
Datakeys is a built-in property of a gridview specifically for the purpose of holding a primary key and not displaying it...
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 

Author Comment

by:ryapos
ID: 19669312
I reviewed the link that you sent. I added the DataKeyName and this is the error I am getting

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
0
 

Author Comment

by:ryapos
ID: 19669390
Here is what I did . I added on selectedindexchanged to my gridview. I then added the following code to Sub gvITems_SelectedIndexChanged:  index = gvItems.SelectedIndex
                                                               prmkey = gvItems.DataKeys(index).Value.ToString().

It works except for one problem. What I have is a gridview with a checkbox field. You check all the check boxes that you want to add to a form. After you have made your selections, you click a submit button. I then use a For Each loop to get all of the items checked and they are inserted into a new table and the table that populated the gridview is updated.

Now when I click the submit button it only adds the first row in the gridview to the new table whether I have checked it or not. Not sure what I need to do next to fix this.
Thanks
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19669393
I had not used it before, this is the method I've used because I've seen other people use it.  It works.
And from what I've seen of the code here, it'd be lightning to implement.  Albeit, the datakeys is probably the better way to go, that way your transmitting one less column of html and viewstate.
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 19669446
what event is your code in?

did you assign a datakey to the grid?

      <asp:gridview id="CustomersGridView"
        datasourceid="CustomersSource"
        autogeneratecolumns="true"
        emptydatatext="No data available."
        autogenerateselectbutton="true"    

        datakeynames="CustomerID"


If yes and it still doesn't wo
0
 

Author Comment

by:ryapos
ID: 19669490
I do have a datakey assigned to the grid. I have the onselectedindexchanged event. Where I am confused is to what event do I need to use.  I am looping through the rows to find all checked rows and for each row checked I am updating and inserting into the tables.
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 2000 total points
ID: 19669500
try something like this...


        For Each gridRow As GridViewRow In rowCollection
            ...etc.

            cmdSelect.Parameters.AddWithValue("@roomName", gvItems.DataKeys(gridRow.RowIndex).Value.ToString)

            ...etc.
        Next
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 19669535
>> I have the onselectedindexchanged event. Where I am confused is to what event do I need to use.  I am looping through the rows to find all checked rows and for each row checked I am updating and inserting into the tables.

If you use onselectindexchanged, then *every time* the selected gridview row changes, your code is firing and it is looping through the grid and doing this insert routine you posted.

From what it sounds like, that's not what you want....it looks like you could just put a "Update" button on your page and put the code in the button clicked event...
0
 

Author Comment

by:ryapos
ID: 19669540
Thanks. That did it. I appreciate your help and the quick response.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

830 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