Click here to Skip to main content
15,896,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I am using this code to gatch the value in id from a grid view.but i am getting an error(Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index)Here in mu code check box is working but i not getting the value id the id of the row.please help me out , where i am going wrong.And plese do not be angry if you are getting problem in understanding, my english is not so good.


ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" 
            CellPadding="3" CellSpacing="1" GridLines="None">
            <footerstyle backcolor="#C6C3C6" forecolor="Black" />
            <rowstyle backcolor="#DEDFDE" forecolor="Black" />
            <columns>
            <asp:TemplateField HeaderText="Select">
                                <itemtemplate>
                                    <asp:CheckBox ID="chk" runat="server" />
                                </itemtemplate>
            
                <asp:BoundField HeaderText="id" DataField="id" />
                
                <asp:BoundField HeaderText="Question" DataField="Question" />
                
                <asp:BoundField DataField="Option1" HeaderText="Option1" />
                
                <asp:BoundField DataField="Option2" HeaderText="Option2" />
                <asp:BoundField DataField="Option3" HeaderText="Option3" />
                <asp:BoundField DataField="Option4" HeaderText="Option4" />
                <asp:BoundField DataField="Correct" HeaderText="Answer" />
            </columns>
            <pagerstyle backcolor="#C6C3C6" forecolor="Black" horizontalalign="Right" />
            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />

Dfault.aspx.cs code

C#
 for (int num = 0; num &lt; i; num++)
       {

     CheckBox chk_box = (CheckBox)GridView1.Rows[num].FindControl("chk");
     string ques_id=GridView1.DataKeys[num].Values["id"].ToString();
           if (chk_box.Checked == true)
           {
            // code
           }
}

</pre>
Posted
Updated 27-Apr-12 23:23pm
v5

Looks like you are iterating more times then number of rows in grid view. Use foreach instead and the problem should not come.
 
Share this answer
 
change your bound field to <asp:templatefield xmlns:asp="#unknown"> and label in item template.after that try this on your cs page...
C#
foreach (GridViewRow gr in Gridview1.Rows)
       {
           CheckBox chk = (CheckBox)gr.FindControl("CheckBox1");
           if (chk.Checked == true)
           {
               Label lbl = (Label)gr.FindControl("label1"); // id of label from which tou want to fetch values
            //code
           }
       }</asp:templatefield>
 
Share this answer
 
v2

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