Display Message On MouseOver In an ASP.NET GridView

To display a message in an ASP.NET GridView on MouseOver, handle the RowDataBound event. As given in MSDN,
The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.



    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"


        SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [Address], [City] FROM [Customers]">


    </asp:SqlDataSource>


 


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"


            DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True"


            OnRowDataBound="GridView1_RowDataBound">


            <Columns>                          


                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />


                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />


                <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />


                <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />


                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />


            </Columns>


     </asp:GridView>




C#


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)


{


 if ( e.Row.RowType == DataControlRowType.DataRow )


 {


  e.Row.Attributes.Add("onmouseover", "alert('Display some message');");


 }


}




VB.NET


    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)


        If e.Row.RowType = DataControlRowType.DataRow Then


            e.Row.Attributes.Add("onmouseover", "alert('Display some message');")


        End If


    End Sub




Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

nikhil said...

Suppose I want to display a colour on each row, can I write
System.Drawing.Colour.Red instead of alert('Message');