Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi, all

I have a gridview, the first column is checkbox, the page size of gridview is 50.
Now i want to give the range for selecting check boxes in the gridview. Ex.If i give 1-5 means that 1 to 5 rows checkboxes need to select in the gridview.Grid have the paging also. In every page the range must be in 1 to 50 only. Can any body help me. Its urgent. Thank in advance.


Here the code:
ASP.NET
<asp:GridView ID="grvRecords" runat ="server" AutoGenerateColumns="false" Font-Bold="True"
            Font-Names="Trebuchet MS" Font-Size="10px" AllowPaging="True" 
        onpageindexchanging="grvRecords_PageIndexChanging" PageSize="50" 
        Font-Underline="False" AllowSorting="True" onsorting="grvRecords_Sorting" >
<columns>
 <asp:TemplateField  >
            <HeaderTemplate >
           <input id="chkAllItems" type="checkbox"  onclick="CheckAllDataGridCheckBoxes('chkItemChecked',document.forms[0].chkAllItems.checked)" value="Check">
            </HeaderTemplate>
            <itemtemplate>
            <asp:CheckBox id="chkBulk" runat="server" Width="30px" AutoPostBack="False"> 
            </itemtemplate>



and the Script is:(firstno, lastno is the range)
JavaScript
var grid1 = document.getElementById("<%= grvRecords.ClientID %>");
if (grid1.rows.length > 0) {
    for (var i = firstno - 1; i < secondno; i++) {
        var cell = grid1.rows[i].cells[0];

        for (var j = 0; j < cell.childNodes.length; j++) {
            if (cell.childNodes[j].type == "checkbox" && cell.childNodes[j].name=="chkBulk" ) {
                cell.childNodes[j].checked = true;
            }
        }
    }
}


The above code is not getting output.
Posted
Updated 7-Aug-12 21:05pm
v2

1 solution

This is just an example for your scenario:-

C#
protected void Page_Load(object sender, EventArgs e)
        {
            ArrayList arr = new ArrayList();
            arr.Add("1");
            arr.Add("2");
            arr.Add("3");
            arr.Add("4");
            arr.Add("5");
            arr.Add("6");
            arr.Add("7");

            GridView1.DataSource = arr;
            GridView1.DataBind();
        }


C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chk = (CheckBox)e.Row.FindControl("CheckBox1");

                if (e.Row.RowIndex <= 4)
                {
                        chk.Checked=true;
                }
            }
        }
 
Share this answer
 
Comments
Murthy_RDV 8-Aug-12 3:40am    
Hi,Actually i am giving the range in the Textbox above the gridview, on the onblur() event, i need to check the checkboxes in the gridview according to the range
suppose Range 10-20 Means
From 10 row to 20 row check boxes need to select

If i am giving 1-25 Menas
From 1 Row to 25 row chekboxes need to slect in the gridview
I need in javascript solution
HemendraSingh88 8-Aug-12 3:48am    
You can call a button click (Server Button) with css visibility set to hidden and write the code in C# for checking the check boxes inside the Grid View, this would be easier rather than writing the whole code in JavaScript.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900