Apply cursor pointer to ASP.Net GridView columns using C# and VB.Net

ramco1917
 
on Jul 11, 2022 07:10 AM
Sample_132249.zip
528 Views

Hi

How to show cursor:pointer on every GridView column on mouse hover except Header row in C#

Thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 11, 2022 08:01 AM

Hi ramco1917,

You need to loop through the each row and inside the row loop through each cell and set the onclick attributes with row index and cell index.

Then inside the OnRowCommand event, using the cellIndex get the column name from HeaderRow.

Refer below example.

HTML

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" OnRowCommand="OnRowCommand">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["style"] = "cursor:pointer";
        foreach (TableCell cell in e.Row.Cells)
        {
            if (cell is DataControlFieldCell)
            {
                cell.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvCustomers, string.Format("SelectedCell${0},{1}", e.Row.RowIndex, e.Row.Cells.GetCellIndex(cell)));
                Page.ClientScript.RegisterForEventValidation(gvCustomers.UniqueID, string.Format("SelectedCell${0},{1}", e.Row.RowIndex, e.Row.Cells.GetCellIndex(cell)));
            }
        }
    }
}

protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "SelectedCell")
    {
        string[] arguments = (e.CommandArgument).ToString().Split(',');
        int cellIndex = int.Parse(arguments[1]);
        string colName = gvCustomers.HeaderRow.Cells[cellIndex].Text.Trim();
        string message = "Cell Index : " + cellIndex + "\\nColumn Name: " + colName;
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
    }
}

private void BindGrid()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[]
    {
        new DataColumn("CustomerId"),
        new DataColumn("Name"),
        new DataColumn("Country")
    });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    gvCustomers.DataSource = dt;
    gvCustomers.DataBind();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGrid()
    End If
End Sub

Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Attributes("style") = "cursor:pointer"
        For Each cell As TableCell In e.Row.Cells
            If TypeOf cell Is DataControlFieldCell Then
                cell.Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(gvCustomers, String.Format("SelectedCell${0},{1}", e.Row.RowIndex, e.Row.Cells.GetCellIndex(cell)))
                Page.ClientScript.RegisterForEventValidation(gvCustomers.UniqueID, String.Format("SelectedCell${0},{1}", e.Row.RowIndex, e.Row.Cells.GetCellIndex(cell)))
            End If
        Next
    End If
End Sub

Protected Sub OnRowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    If e.CommandName = "SelectedCell" Then
        Dim arguments As String() = (e.CommandArgument).ToString().Split(","c)
        Dim cellIndex As Integer = Integer.Parse(arguments(1))
        Dim colName As String = gvCustomers.HeaderRow.Cells(cellIndex).Text.Trim()
        Dim message As String = "Cell Index : " & cellIndex & "\nColumn Name: " & colName
        ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('" & message & "');", True)
    End If
End Sub

Private Sub BindGrid()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {
                        New DataColumn("CustomerId"),
                        New DataColumn("Name"),
                        New DataColumn("Country")})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    gvCustomers.DataSource = dt
    gvCustomers.DataBind()
End Sub

Screenshot