Hi ramco1917,
Inside the OnRowDataBound event handler, for each GridView Row you need to set JavaScript click event handler using the onclick attribute to the Swatch cell.
For more details refer below article.
Refer below example.
HTML
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="true"
OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="OnSelectedIndexChanged">
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
this.BindGrid();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvDetails, "Select$" + e.Row.RowIndex);
e.Row.Cells[3].Attributes["style"] = "cursor:pointer";
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
int index = gvDetails.SelectedRow.RowIndex;
string location = gvDetails.SelectedRow.Cells[0].Text.Trim().Replace(" ", string.Empty);
string swatch = gvDetails.SelectedRow.Cells[3].Text.Trim().Replace(" ", string.Empty);
string message = "Location: " + location + "\\nSwatch: " + swatch;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5] {
new DataColumn("Location"),
new DataColumn("Omega"),
new DataColumn("Radi"),
new DataColumn("Swatch"),
new DataColumn("Tissot") });
dt.Rows.Add("Delhi", 4, "", "", 6);
dt.Rows.Add("Bombay", 1, 2, "", "");
dt.Rows.Add("Pune", 3, "", 4, 1);
dt.Rows.Add("Chandigarh", 2, 5, "", "");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.BindGrid()
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(3).Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(gvDetails, "Select$" & e.Row.RowIndex)
e.Row.Cells(3).Attributes("style") = "cursor:pointer"
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim index As Integer = gvDetails.SelectedRow.RowIndex
Dim location As String = gvDetails.SelectedRow.Cells(0).Text.Trim().Replace(" ", String.Empty)
Dim swatch As String = gvDetails.SelectedRow.Cells(3).Text.Trim().Replace(" ", String.Empty)
Dim message As String = "Location: " & location & "\nSwatch: " & swatch
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Private Sub BindGrid()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(4) {
New DataColumn("Location"),
New DataColumn("Omega"),
New DataColumn("Radi"),
New DataColumn("Swatch"),
New DataColumn("Tissot")})
dt.Rows.Add("Delhi", 4, "", "", 6)
dt.Rows.Add("Bombay", 1, 2, "", "")
dt.Rows.Add("Pune", 3, "", 4, 1)
dt.Rows.Add("Chandigarh", 2, 5, "", "")
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
Screenshot