Click here to Skip to main content
Click here to Skip to main content

Code Data-Bound GridView & FindControl

, 11 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will show you how to deal with GridView control when you bind it from code behind and without using the DataSource Model.If

Editorial Note

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

This article will show you how to deal with GridView control when you bind it from code behind and without using the DataSource Model.

If you're going to bind a GridView via code there are a couple things to keep in mind. When you do this you must handle the events yourself, unlike when you configure the  GridView in design-time.

Namely, you must handle: RowEditing, RowDeleting ,RowCancelingEdit & RowUpdating (Events of the GridView)

1. Presumably if you're binding the GridView in code, you'll will need to Bind the GridView in Page Load event Handler

 

 <span class="kwd">If Not</span> IsPostBack
  GridView1.DataSource = EVALAdapter.GetData <span class="cmt">'my dataset</span>
  GridView1.DataBind() 
 <span class="kwd">End If</span>

2. In order to get your Gridview Row into 'Edit' mode you must do the following in the RowEditing event:

GridView2.EditIndex = e.NewEditIndex
GridView2.DataSource = EvalGroups.GetData 'my dataset
GridView2.DataBind()

3. The RowCancelingEdit is identical to RowEditing except for one small change:

GridView1.EditIndex = -1
GridView1.DataSource = EVALAdapter.GetData
GridView1.DataBind()

4. My RowUpdating looks like this:

Dim cmd As New dsEvalTableAdapters.tblEvalTableAdapter

Dim FirstCellText as String = GridView1.Rows(e.RowIndex).Cells(0).Text
Dim chkCheck As CheckBox
chkCheck = GridView1.Rows(e.RowIndex).FindControl(<span class="st">"CheckBox1"</span>)
Dim txtCtlID As TextBox
txtCtlID = GridView1.Rows(e.RowIndex).FindControl(<span class="st">"TextBox2"</span>)

cmd.MGEvalUpdateEvalPeriod(FirstCellText , chkCheck.Checked, txtCtlID.Text) '<span class="kwd">this</span> runs the stored procedure I added to my dataset
GridView1.EditIndex = -1
GridView1.DataSource = EVALAdapter.GetData
GridView1.DataBind()
  

Couple of things to keep in mind. FindControl is necessary because there isn't a direct reference to the TextBox control that is dynamically created when you go into edit mode. The name 'TextBox1' is generated automatically (much like when you first add a TextBox control to a web form and it is given a default ID value). You can see this if you view the source of the aspx page after you go into edit mode.

Important: The control field you're trying to find using FindControl, in this case, must be a template field and not merely a BoundField ( if they are a BoundField then you need to access them via GridViewRow.Cells collection like we do for the "FirstCellText " value ).


In this article , we show how to manually bind the GridView control using the Code behind and without using the Data Source Model .

Note that using the Data Source Model is much easier since you just need to configure the GridView dataSoruce using visual studio designer.Also using the Data source model allow you to automatically enable paging,editing,deleting,updating ,which requires a lot of work when you use the code behind approach ( as we show in this article).

Sometimes you have no choice but to use the code behind model , like if you want to bind the GridView to a list of Array elemnts or to a DataTable that is created in the page code behind.

Finally , Always try to use the DataSource model as much as you can because its easy and minimize the amount of  code, hence it will minimze the bugs and effort.

Links: 

Data Access Tutorials

GridView Class

License

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

Share

About the Author

ASP.NET Community

United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.
 
The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

411 members


Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid