Code Data-Bound GridView & FindControl

 Rate It (0)

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
GridView1.DataSource = EVALAdapter.GetData 'my dataset
GridView1.DataBind()
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
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("CheckBox1")
Dim txtCtlID As TextBox
txtCtlID = GridView1.Rows(e.RowIndex).FindControl("TextBox2")

cmd.MGEvalUpdateEvalPeriod(FirstCellText , chkCheck.Checked, txtCtlID.Text) 'this 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

Revision number 2, Sunday, September 14, 2008 8:23:21 AM by anas

Comments

Nice and easy... Thanks

Related Articles

GridView

The GridView control was introducedwith ASP.NET 2.0 and Visual Studio 2005 as a replacement for the DataGrid control. While it has many similarities to the original DataGrid controlthe GridView is a more feature rich control. A variety of resources are linked

ASP.NET Controls - New in Version 3.5

Check out the Feature Specifications for Visual Studio 2008 and .NET Framework 3.5. These older documents give you an insight into what features made it (and didn't make it) into the Release. GridView- Take a look at all the articles that ScottGu has done

GridView Utilities in C# and VB.NET

GridView Utilities The GetColumnIndexByHeaderText method shown below finds the column index inside a GridView control by passing to it a GridView instance together with the header text. If column is not found, a value of -1 is returned. C# static public int

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (5)
  2. SGWellens (4)
  3. maartenba (2)
  4. rami_nassar (2)
  5. stiansol (2)
  6. MisterFantastic (2)
  7. satish1.v (1)
  8. raklos (1)
  9. mosessaur (1)
  10. Jos Branders (1)

Advertise Here

Microsoft Communities
Page view counter