Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

Show all GridView Rows in EditMode

Sometimes, in the application we have a requirement that you need to show the GridView rows in edit mode. This means that all the rows will contain textboxes and the data should be present in the textboxes. Once, you are done with editing you can update and get back to the view mode. In this post I will simply show you how to change the mode of the GridView from view to edit. I will not be using any datasource controls.

The first task is the create a GridView. Check out the html code to create the GridView control:

<asp:GridView ID="gvUsers" AutoGenerateColumns="false" runat="server" OnRowDataBound="gvUsers_RowDataBound">

<Columns>

<asp:TemplateField HeaderText="User ID">

<ItemTemplate>

<asp:Label ID="lblUserID" runat="server" Text='<%# Eval("UserID") %>' />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="First Name">

<ItemTemplate>

<asp:Label ID="lblFirstName" Visible='<%# !(bool) IsInEditMode %>' runat="server" Text='<%# Eval("FirstName") %>' />

<asp:TextBox ID="txtFirstName" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Eval("FirstName") %>' />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Last Name">

<ItemTemplate>

<asp:Label ID="lblLastName" Visible='<%# !(bool) IsInEditMode %>' runat="server" Text='<%# Eval("LastName") %>' />

<asp:TextBox ID="txtLastName" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Eval("LastName") %>' />

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

Check out the screen shot below:

GridViewAllRowsEditMode1

 

As, you can see in the html code that both my label and the textbox are in the ItemTemplate and there visibility depends on the IsInEditMode property. Now, let's check out the code behind:

private bool isEditMode = false;

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

BindData();

}

}

private void BindData()

{

string connectionString = "Server=localhost;Database=School;Trusted_Connection=true";

SqlConnection myConnection = new SqlConnection(connectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT UserID, FirstName, LastName FROM Users", myConnection);

DataSet ds = new DataSet();

ad.Fill(ds);

gvUsers.DataSource = ds;

gvUsers.DataBind();

}

// This method will put the GridView in the edit mode

protected void Button1_Click(object sender, EventArgs e)

{

isEditMode = true;

BindData();

}

protected bool IsInEditMode

{

get { return this.isEditMode; }

set { this.isEditMode = value; }

}

protected void Button2_Click(object sender, EventArgs e)

{

isEditMode = false;

BindData();

}

When the button is clicked I simply change the isEditMode to true or depending depending that if I want to view the GridView in edit mode or view mode. Check out the effect below:

 

Published Wednesday, November 08, 2006 3:46 PM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Show all GridView Rows in EditMode @ Thursday, November 09, 2006 12:30 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

# re: Show all GridView Rows in EditMode @ Thursday, February 01, 2007 1:20 PM

If we had EditItemTemplate instead of ItemTemplate, the view mode will get rid of the data that is there.

csharper

# re: Show all GridView Rows in EditMode @ Tuesday, March 27, 2007 2:55 AM

Your GridView doesn't work if I dont implicitly cast the IsInVisible property to bool. For example, Raihan

# re: Show all GridView Rows in EditMode @ Tuesday, May 01, 2007 5:05 PM

How do you do this in VB?

Scott

# Problem TextBox @ Thursday, May 17, 2007 10:58 PM

Congratulations, it's an amazing example. So i would like to know, what can i do to get the value that the person had inserted in the textbox (txtfirstname) while it was in the editmode and put the new value into a variable. Thanks in advance.

L&#233;o

# re: Show all GridView Rows in EditMode @ Monday, July 09, 2007 9:27 AM

Hello. Many thanks, so beautiful coding. Can you show Visual Basic version, please?

Mesut

# Ultram. @ Tuesday, July 15, 2008 7:09 AM

Ultram.

Ultram.

# Buspar. @ Sunday, November 09, 2008 9:18 PM

Buspar anxiety. Buspar. Buspar buy online cheap.

Buspar.

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below