Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 12th, 2009, 10:41 AM
Authorized User
 
Join Date: Apr 2006
Location: london, , United Kingdom.
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
Send a message via MSN to akshay144
Post can't get checkbox value from Gridview

Hi All

I am populating a grid at runtime/on-the-fly which is fine.

But when I want to get a value of a check-box in the grid I am getting error.
I can't use the findcontrol as it doesn't exist for any of the controls.

Please help me.



CodeBehind:-

Code:
protectedvoid GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
bool att = false;
GridViewRow gvrow = GridView1.Rows[e.RowIndex];
CheckBox chk_SelectCheckbox = (CheckBox)gvrow.Cells[4].Controls[1]; //checkbox to capture chkbox selection
}
Error screenshot





Server Error in '/Event' Application.

Specified argument was out of the range of valid values.
Parameter name: index

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index

Source Error:

Line 135: bool att = false;Line 136: GridViewRow gvrow = GridView1.Rows[e.RowIndex];Line 137: CheckBox chk_SelectCheckbox = (CheckBox)gvrow.Cells[4].Controls[1]; //checkbox to capture chkbox selectionLine 138: Line 139:
Source File: c:\Documents\Visual Studio 2008\Event\UpdateAttendance.aspx.cs Line: 137

Stack Trace:

[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.Parameter name: index] System.Web.UI.ControlCollection.get_Item(Int32 index) +8668910 UpdateAttendance.GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) in c:\Users\Mayur\Desktop\Data\urvashi\Documents\Visu al Studio 2008\IndianSpeedDating\UpdateAttendance.aspx.cs:13 7 System.Web.UI.WebControls.GridView.OnRowUpdating(G ridViewUpdateEventArgs e) +133 System.Web.UI.WebControls.GridView.HandleUpdate(Gr idViewRow row, Int32 rowIndex, Boolean causesValidation) +720 System.Web.UI.WebControls.GridView.HandleEvent(Eve ntArgs e, Boolean causesValidation, String validationGroup) +704 System.Web.UI.WebControls.GridView.OnBubbleEvent(O bject source, EventArgs e) +95 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.GridViewRow.OnBubbleEven t(Object source, EventArgs e) +123 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e) +118 System.Web.UI.WebControls.LinkButton.RaisePostBack Event(String eventArgument) +135 System.Web.UI.WebControls.LinkButton.System.Web.UI .IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +175 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074
  #2 (permalink)  
Old August 12th, 2009, 11:38 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Don't forget these indexes are zero-based.

If you want to target the 4th cell in the row, you'd use Cells[3].

If the checkbox is the only control in that cell, you'd use Controls[0].
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
akshay144 (August 13th, 2009)
  #3 (permalink)  
Old August 21st, 2009, 12:45 AM
Authorized User
Points: 198, Level: 3
Points: 198, Level: 3 Points: 198, Level: 3 Points: 198, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: singapore, singapore, Singapore.
Posts: 54
Thanks: 0
Thanked 4 Times in 4 Posts
Default

i would i advise you to use findControl of the respective row. This way even if you add some more controls in the future it wont break at runtime

gvrow.Cells[4].FindControl()
__________________
Pon Saravanan
VbKnowledgebase.com
The Following User Says Thank You to pons_saravanan For This Useful Post:
akshay144 (August 24th, 2009)
  #4 (permalink)  
Old March 12th, 2011, 02:59 AM
Registered User
 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Can't get Check Values from Gridview

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

TextBox1.Text = GridView1.SelectedRow.Cells[2].Text.ToString();

bool select;
select = ((CheckBox)GridView1.SelectedRow.Cells[3].Controls[0]).Checked;
if (select ==true)
{
DropDownList1.Text = "True";
}
if (select==false )
{
DropDownList1.Text = "False";
}

}
  #5 (permalink)  
Old March 31st, 2011, 02:52 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Index starts from 0 secondly find control is more recommended
__________________
Regards,
Rashida Rauf
http://www.questions-interviews.com


Similar Threads
Thread Thread Starter Forum Replies Last Post
Gridview and Checkbox krasnokojiy ASP.NET 2.0 Basics 14 February 28th, 2008 12:56 PM
Checkbox in Gridview yukijocelyn ASP.NET 2.0 Basics 0 September 24th, 2007 12:32 AM
GridView Checkbox ItemTemplate angelicagm ASP.NET 2.0 Professional 3 May 31st, 2007 04:44 AM
Checkbox in GridView aliirfan84 ASP.NET 2.0 Professional 1 May 15th, 2007 03:59 PM
Gridview checkbox problems pauliehaha ASP.NET 2.0 Professional 1 May 3rd, 2007 04:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.