Click here to Skip to main content
Licence 
First Posted 5 Jan 2006
Views 42,680
Bookmarked 17 times

Modify controls inside a GridView control

By | 5 Jan 2006 | Article
Do you have problems to access controls o read data from a GridView control? Don't worry here I show you how do that

Introduction

A beginner developer have problems with accessing to GridView Controls, why? because (for me)is hard to understand key concepts like databinding and templates. Understand really is more than read a visual studio help.

Key Concepts

Well like I said the key concepts are DataBinding and gridview's Templates

To understand DataBinding I'll prefer refer to

Templates are well explain in:

  • Book ASP.NET Data Web Controls / ISBN: 0-672-32501-2/ Chapter 1

Problem

I was trying access controls inside a GridView specially when I pressed a button (ButtonField)

I have a page with this gridview:

<P> </P><P><asp:GridView ID="gvFacturas" runat="server" OnRowCommand="gvFacturas_RowCommand" </P><P>AutoGenerateColumns="False" OnRowEditing="gvFacturas_RowEditing" Width="100%"></P><P><Columns></P><P><asp:BoundField DataField="DeteOrder" DataFormatString="{0:d}" HeaderText="Date"</P><P>HtmlEncode="False" /></P><P><asp:TemplateField HeaderText="Description"></P><P><EditItemTemplate></P><P><asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("Description") %>'></P><P></asp:TextBox></P><P></EditItemTemplate></P><P><ItemTemplate></P><P><asp:Label ID="lblDescription" runat="server" Text='<%# Bind(Description")%>'></P><P></asp:Label></P><P></ItemTemplate></P><P></asp:TemplateField> </P><P><asp:ButtonField ButtonType="Image" HeaderText="Something" </P><P>ImageUrl="~/images/anular.GIF" Text="Button" CommandName="dosomething"></P><P><ItemStyle HorizontalAlign="Center" /></P><P></asp:ButtonField></P><P></Columns></P><P></asp:GridView></P><P>   </P><P> </P>

As you see there are two columns types (BoundField and TemplateField)

This article is oriented to use TemplateFields, a BoundField would convert to TempleateField.

I wish to update lblDescription with another string HOW?

Solutions & Results

The "secret" is RowCommand Event and e.CommandArgument cast.


1. If you wish access and modify the grid but not affect DataSource make this:

if (e.CommandName.Equals("dosomething"))
((Label)gvGrid.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lblDemo")).Text = "Test!"
 
the key is e.CommandArgument cast to int to obtain the exact row

2. If you wish modify the data behind gridview use this:

if (e.CommandName.Equals("dosomething")) 
   ((Demo.OrderEntity)Profile.Period.Facturas[gvGrid.Rows[Convert.ToInt32(e.CommandArgument)].DataItemIndex]).Description = "Test!";

Comments?

Thanks

 

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

tecnocrata

Web Developer

Bolivia Bolivia

Member

Soon...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDimond in the rough PinmemberNuckles Nelson11:36 4 Oct '07  
JokeI see LISP... Pinmembernor_20004:30 18 Oct '06  
GeneralThe Row Index in not in CommandArguement Pinmemberutee7921:33 27 Jun '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 5 Jan 2006
Article Copyright 2006 by tecnocrata
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid