This proved to be quite a little challenge. Here's the code that shows how you can accomplish this.
In the aspx page:
<asp:GridView ID="GridView1" runat="server" SelectedRowStyle-BackColor
</asp:GridView>
<asp:Button ID="Edit" runat="server" Text="Edit" OnClick="Edit_Click1" />
<asp:Button ID="Cancel" runat="server" Text="Cancel" OnClick="Cancel_Click1" />
See the code snippet for the code behind code.
You can add a SelectedIndexChanged event if you want to but the gridview handles this automaitcally.
Main Topics
Browse All Topics





by: b1xml2Posted on 2008-08-02 at 23:53:59ID: 22146676
Happy Coding
ASPX Page (Default.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtm
<html xmlns="http://www.w3.org/1
<head runat="server">
<title>GridView Example</title>
<style type="text/css">
.rowStyle td { background:#fffbd6; color:#333333; cursor:pointer; }
.alternateRowStyle td { background:#ffffff; color:#000000; cursor:pointer;}
.selectedRowStyle td { background:#ffcc66; font-weight:bold; color:Navy;}
.headerStyle td { background:#990000;font-we
.pagerStyle td { background:#ffcc66;color:#
.footerStyle td { background:#990000;font-we
.mouseOver td { background:purple; color:Yellow; cursor:pointer; }
</style>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
function selectingRow(index) {
var eSelect = document.getElementById('<%
document.forms[0]["RowInde
eSelect.click();
}
// -->
</script>
<form id="form1" runat="server">
<input type="hidden" name="RowIndex" />
<div>
<asp:Button ID="SelectButton" runat="server" Text="Select" OnClick="SelectButton_Clic
<asp:Button ID="DeleteButton" runat="server" Text="Delete" OnClick="DeleteButton_Clic
<asp:Button ID="EditButton" runat="server" Text="Edit" OnClick="EditButton_Click"
<asp:Button ID="SaveButton" runat="server" Text="Save" OnClick="SaveButton_Click"
<asp:Button ID="CancelButton" runat="server" Text="Cancel" OnClick="CancelButton_Clic
<asp:GridView ID="ContactGridView" runat="server" AutoGenerateColumns="False
DataSourceID="ContactObjec
GridLines="None" onrowcreated="ContactGridV
<FooterStyle CssClass="footerStyle" />
<RowStyle CssClass="rowStyle" />
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
<PagerStyle CssClass="pagerStyle"/>
<SelectedRowStyle CssClass="selectedRowStyle
<HeaderStyle CssClass="headerStyle" />
<AlternatingRowStyle CssClass="alternateRowStyl
</asp:GridView>
</div>
<asp:ObjectDataSource ID="ContactObjectDataSourc
DataObjectTypeName="Expert
DeleteMethod="DeleteEntity
OldValuesParameterFormatSt
onselecting="ContactObject
onupdating="ContactObjectD
TypeName="ExpertsExchange.
UpdateMethod="UpdateEntity
<SelectParameters>
<asp:Parameter Name="entities" Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
</form>
</body>
</html>
Code-Behind
using System;
using System.Collections.Generic
using System.Web.UI.WebControls;
using System.Web.UI;
namespace ExpertsExchange.Q23614580
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
#region Buttons
protected void DeleteButton_Click(object sender, EventArgs e)
{
int index = this.ContactGridView.Selec
this.ContactGridView.Selec
this.ContactGridView.Delet
}
protected void EditButton_Click(object sender, EventArgs e)
{
this.ContactGridView.EditI
}
protected void SaveButton_Click(object sender, EventArgs e)
{
int index = this.ContactGridView.EditI
if (index != -1)
{
this.ContactGridView.Updat
}
}
protected void CancelButton_Click(object sender, EventArgs e)
{
int index = this.ContactGridView.EditI
if (index != -1)
{
this.ContactGridView.EditI
SetSelectedClass(this.Cont
}
}
protected void SelectButton_Click(object sender, EventArgs e)
{
int currentIndex = this.ContactGridView.Selec
if (currentIndex != -1)
{
SetHoverClass(this.Contact
}
int newIndex = int.Parse(Request.Form["Ro
if (newIndex != -1)
{
SetSelectedClass(this.Cont
}
this.ContactGridView.Selec
}
#endregion
#region Data Calls
private List<Contact> Contacts
{
get
{
if (ViewState["Contacts"] == null)
{
List<Contact> list = new List<Contact>();
list.Add(new Contact(1, "Joe", "Bloggs", "joe.bloggs@gmail.com"));
list.Add(new Contact(2, "Jane", "Bloggs", "jane.bloggs@gmail.com"));
list.Add(new Contact(3, "John", "Doe", "john.doe@gmail.com"));
ViewState["Contacts"] = list;
}
return (List<Contact>)ViewState["Co
}
}
private Contact FindById(int id)
{
foreach (Contact entity in Contacts)
{
if (entity.Id == id)
return entity;
}
return null;
}
protected void ContactObjectDataSource_Se
{
e.InputParameters[0] = Contacts;
}
protected void ContactObjectDataSource_Up
{
Contact entity = (Contact)e.InputParameters
Contact actualEntity = FindById(entity.Id);
if (actualEntity != null)
actualEntity.CopyFrom(enti
}
protected void ContactObjectDataSource_De
{
Contact entity = (Contact)e.InputParameters
Contact actualEntity = FindById(entity.Id);
if (actualEntity != null)
Contacts.Remove(actualEnti
}
#endregion
#region ContactGridView Events
protected void ContactGridView_RowCreated
{
if (e.Row.RowType == DataControlRowType.DataRow
{
switch (e.Row.RowState)
{
case DataControlRowState.Normal
case DataControlRowState.Altern
SetHoverClass(e.Row);
break;
}
}
}
#endregion
#region UI Helpers
private void SetHoverClass(GridViewRow row)
{
row.Attributes["originalCl
row.Attributes["onmouseove
row.Attributes["onmouseout
row.Attributes["onclick"] = string.Format("selectingRo
}
private void SetSelectedClass(GridViewR
{
row.Attributes.Remove("ori
row.Attributes.Remove("onm
row.Attributes.Remove("onm
row.CssClass = "selectedRowStyle";
}
#endregion
}
}
Abstract Factory Class
using System;
using System.ComponentModel;
usin
namespace
{
[DataObject]
public abstract class ObjectCacheFactory<T>
{
[DataObjectMethod(DataObje
public IEnumerable<T> GetEntities(IEnumerable<T> entities)
{
return entities;
}
[DataObjectMethod(DataObje
public void UpdateEntity(T entity)
{
// do nothing
}
[DataObjectMethod(DataObje
public void InsertEntity(T entity)
{
// do nothing
}
[DataObjectMethod(DataObje
public void DeleteEntity(T entity)
{
// do nothing
}
}
}
Factory Class
using System;
using System.ComponentModel;
usin
namespace
{
[DataObject]
public class ContactFactory : ObjectCacheFactory<Contact>
{
}
}
Business Object Class
using System;
namespace ExpertsExchange.Q23614580
{
[Serializable]
public class Contact
{
public Contact() { }
public Contact(int id, string firstName, string lastName, string email)
{
this._id = id;
this._firstName = firstName;
this._lastName = lastName;
this._email = email;
}
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private string _email;
public string Email
{
get { return _email; }
set { _email = value; }
}
public void CopyFrom(Contact item)
{
this.FirstName = item.FirstName;
this.LastName = item.LastName;
this.Email = item.Email;
}
}
}