473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

mouse rollover row on datagrid

I have a data grid with data, which each row will tie to a link to open that
row in a seperate window of data for it, the data grid has multiple columns.
I want it so when a mouse rolls over a row, the entire row (all columns) are
displayed in a background color. I have figured out how to do it cell by
cell, just not the entier row at once. How would i do this? thanks!
Nov 18 '05 #1
4 1853
Brian -
I think the best way to do this is to define two tr classes - on and
off. Then define td classes that inherit from the tr's with the right
background colors. Then you just have mouseover and mouseout events
that set the class. Sounds more complex than it is.

Sample code (tested in IE6 and Firefox):
<html>
<head>
<style>
TABLE
{
border: none;
border-collapse: collapse;
}
TR.off TD
{
background-color:white;
}
TR.on TD
{
background-color:silver;
}
</style>
</head>
<body>
<table>
<tr class=off onmouseover="this.className='on'"
onmouseout="this.className='off'">
<td>column 1</td>
<td>column 2</td>
</tr>
<tr class=off onmouseover="this.className='on'"
onmouseout="this.className='off'">
<td>column 1</td>
<td>column 2</td>
</tr>
<tr class=off onmouseover="this.className='on'"
onmouseout="this.className='off'">
<td>column 1</td>
<td>column 2</td>
</tr>
<tr class=off onmouseover="this.className='on'"
onmouseout="this.className='off'">
<td>column 1</td>
<td>column 2</td>
</tr>
</table>
</body>
</html>

Some other references:
Article on AListApart for a system that does this automatically for all
tables on a page: http://www.alistapart.com/articles/tableruler/

CodeProject server control that extends a DataGrid to write out the
javascript for you:
http://www.codeproject.com/aspnet/AlPaMOD.asp
- Jon
http://weblogs.asp.net/jgalloway

Nov 18 '05 #2
Thanks for Jon's good suggestions.

Hi Brian,

I think Jon's idea is pretty good and as for webform datagrid, the "<tr>"
should mapping to the DAtaGridItem class and we can reference it and set
the "onmouseover" and "onmouseout" attribute for it. And I suggest that you
set the attributes in the DataGrid's "ItemCreated" event. Here is a simple
demo page, you may have a look :

===============aspx ================
<HTML>
<HEAD>
<title>mouseeffect</title>
<style>

TR.off { BACKGROUND-COLOR: white }

TR.on { BACKGROUND-COLOR: yellow }

</style>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:DataGrid id="dgStyle" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Column1"
HeaderText="Column1"></asp:BoundColumn>
<asp:BoundColumn DataField="Column2"
HeaderText="Column2"></asp:BoundColumn>
<asp:BoundColumn DataField="Column3"
HeaderText="Column3"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</table>
</form>
</body>
</HTML>

========code behind=================
public class mouseeffect : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgStyle;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind_Data();
}
}

private void Bind_Data()
{
DataTable dt = new DataTable("dt");
dt.Columns.Add("Column1",typeof(string));
dt.Columns.Add("Column2",typeof(string));
dt.Columns.Add("Column3",typeof(string));

for(int i=0;i<5;i++)
{
DataRow row = dt.NewRow();
row[0] = "Column1Value"+ i;
row[1] = "Column2Value"+ i;
row[2] = "Column3Value"+ i;
dt.Rows.Add(row);
}

dgStyle.DataSource = dt;
dgStyle.DataBind();

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.dgStyle.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler (this.dgStyle_ItemCreated)
;
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgStyle_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","this.classNam e='on'");
e.Item.Attributes.Add("onmouseout","this.className ='off'");
}
}
}

=======================================

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #3
thanks both of you!

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:J7*************@cpmsftngxa06.phx.gbl...
Thanks for Jon's good suggestions.

Hi Brian,

I think Jon's idea is pretty good and as for webform datagrid, the "<tr>"
should mapping to the DAtaGridItem class and we can reference it and set
the "onmouseover" and "onmouseout" attribute for it. And I suggest that
you
set the attributes in the DataGrid's "ItemCreated" event. Here is a simple
demo page, you may have a look :

===============aspx ================
<HTML>
<HEAD>
<title>mouseeffect</title>
<style>

TR.off { BACKGROUND-COLOR: white }

TR.on { BACKGROUND-COLOR: yellow }

</style>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:DataGrid id="dgStyle" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Column1"
HeaderText="Column1"></asp:BoundColumn>
<asp:BoundColumn DataField="Column2"
HeaderText="Column2"></asp:BoundColumn>
<asp:BoundColumn DataField="Column3"
HeaderText="Column3"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</table>
</form>
</body>
</HTML>

========code behind=================
public class mouseeffect : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgStyle;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind_Data();
}
}

private void Bind_Data()
{
DataTable dt = new DataTable("dt");
dt.Columns.Add("Column1",typeof(string));
dt.Columns.Add("Column2",typeof(string));
dt.Columns.Add("Column3",typeof(string));

for(int i=0;i<5;i++)
{
DataRow row = dt.NewRow();
row[0] = "Column1Value"+ i;
row[1] = "Column2Value"+ i;
row[2] = "Column3Value"+ i;
dt.Rows.Add(row);
}

dgStyle.DataSource = dt;
dgStyle.DataBind();

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.dgStyle.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler (this.dgStyle_ItemCreated)
;
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgStyle_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","this.classNam e='on'");
e.Item.Attributes.Add("onmouseout","this.className ='off'");
}
}
}

=======================================

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4
You're welcome Brian.

I'm also glad that it's of assistance. Have a good day!

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bryan Masephol | last post by:
Hi All I got a datagrid and a ComboBox on a form. I populate the combobox with years for all the data avaiable. When the user chooses a year the datagrid is populated with the specific years...
2
by: melanieab | last post by:
Hi, How can you determine which cell the mouse is positioned over during the mousehover event? Thanks so much! Mel
5
by: Nick | last post by:
Hey guys, I have 2 events on a windows forms datagrid, the mouse move as well as the double click events. What's happening is that when I double click on a row in the grid, the mouse move event...
3
by: Ryan Liu | last post by:
Can someone give a sample to prevent a row from being deleted in a datatable? I tried e.Row.RejectChanges(); in dt_RowDeleting() but seems does not work. I need verify if there other data...
1
by: Informedone | last post by:
Looking for a cut and past script. Mouse rollover text shows a text box. Any help will be so appreciated. Email to informedone@gmail.com Thanks, Scott Wilson Crestview, Florida.
5
by: Sabbaath | last post by:
First off, I've pretty much convinced myself that the issue is that there are onmouseover events inside the div that contains an onmouseover event on it... I wrote the message below first. This was...
2
by: jpk872 | last post by:
i created some javascript DHTML that highlights which menu selection the mouse is currently at, but it won't work on Firefox. Any idea? The source is old, so probably things have changed. ...
0
by: ravitunk | last post by:
can anyone tell me how to open a popup window(.aspx page) on Mouse Rollover...everytime cursor is moved to a row of a gridview..this should also send a parameter to the popup window....where the...
3
by: Jon Slaughter | last post by:
What is the general procedure to optimize hit tests on objects with a mouse cursor? Just loop through all the objects and check for intersection and break when found or is there something better? ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.