Click here to Skip to main content
15,881,281 members
Articles / Binding
Article

Code Data-Bound GridView & FindControl

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 12.9K   4  
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

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

 

If Not IsPostBack<br />  GridView1.DataSource = EVALAdapter.GetData 'my dataset<br />  GridView1.DataBind() <br /> End If

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

GridView2.EditIndex = e.NewEditIndex<br />GridView2.DataSource = EvalGroups.GetData 'my dataset<br />GridView2.DataBind()

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

GridView1.EditIndex = -1<br />GridView1.DataSource = EVALAdapter.GetData<br />GridView1.DataBind()

4. My RowUpdating looks like this:

Dim cmd As New dsEvalTableAdapters.tblEvalTableAdapter<br /><br />Dim FirstCellText as String = GridView1.Rows(e.RowIndex).Cells(0).Text<br />Dim chkCheck As CheckBox<br />chkCheck = GridView1.Rows(e.RowIndex).FindControl("CheckBox1")<br />Dim txtCtlID As TextBox<br />txtCtlID = GridView1.Rows(e.RowIndex).FindControl("TextBox2")<br /><br />cmd.MGEvalUpdateEvalPeriod(FirstCellText , chkCheck.Checked, txtCtlID.Text) 'this runs the stored procedure I added to my dataset<br />GridView1.EditIndex = -1<br />GridView1.DataSource = EVALAdapter.GetData<br />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)


Written By
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.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --