Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview: Accessing Only Those Rows Displayed on the Screen

Status
Not open for further replies.

HardingR2000

Programmer
Oct 6, 2008
40
US
[sad]I have a Gridview with a data source containing 100 rows. I use page control to display 40 rows at a time. On each row is a check box that flags the row for processing later should the user click on another button on the screen. Additionally, I have a check box (call it ALL) outside the grid which, when checked, programmatically checks the check box on all 100 rows in the data source when it is checked. If ALL is unchecked then the process will programmatically uncheck the check box on all 100 rows.

What I want to do is have the ALL check box only check and uncheck those 40 rows that are being displayed on the page currently being displayed on the screen. There is a column (called Account) which is unique for each row that is used to perform the update.

My problem is that I don't know how to programmatically capture/identify the 40 rows being displayed so that I can perform the update on only those 40 accounts. Can someone tell me how to process only the 40 rows being displayed on the screen out of the 100 rows in the data source?
 

If using an external control that causes a Postback you can always loop through the Gridview Rows collection (the rows shown in the current page) and check the checkbox in each row. The data source will have to be updated as the selections will be lost when the gridview is paged. You can extract the Account column the same way and use for your update.

Code:
Dim chk As CheckBox
Dim lbl as Label
For Each itm As GridViewRow In GridView1.Rows
    If itm.RowType = DataControlRowType.DataRow Then
        chk = CType(itm.FindControl("CheckBox1"), CheckBox)
        lbl = CType(itm.FindControl("lblAccount"), Label)
        If chk IsNot Nothing Then
            chk.Checked = True
            YourCheckBoxRoutine(lbl.Text)
        End If
    End If
Next


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
This almost works. It does limit the number of rows that are checked to 40, however, keep reading.

The actual update of certain information on the account is done via another button (call it Save) which appears if the checkbox on one or more rows have been checked. Account does not play into any processing until after the selected rows have been checked and the Save button is clicked. I think I have that part of the process working. I only threw it into the explaination in case it was needed.

It appears that my routine that you called "YourCheckboxRoutine" needs to be smartened up to process each account it finds being displayed on the screen against the data source for the GridView. I was hoping that somehow in the loop through the 40 rows being displayed, the checkbox on those rows could be set at that time. Am I going to have to smarten up "" or is there another way to check the 40 checkboxes (or however many are left on the last page) on the page I am displaying?



 

The "YourCheckBoxRoutine" line was simply an indicator that you "could" update the data when looping through the rows, you can leave it out.

This example assumed that the checkbox is in a TemplateField since you wanted to manipulate it outside the gridview. If it is bound checkbox control, you may want to consider changing to a TemplateField in order to accomplish what you want. Then the previous code will make more sense.

Code:
<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox runat="server" ID="chkProcess"/>
    </ItemTemplate>
</asp:TemplateField>

Code:
Dim chk As CheckBox
For Each itm As GridViewRow In GridView1.Rows
    If itm.RowType = DataControlRowType.DataRow Then
        chk = CType(itm.FindControl("chkProcess"), CheckBox)
        If chk IsNot Nothing Then
            chk.Checked = True
        End If
    End If
Next







Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top