Hi mahjaubi,
Please refer the below modified sample.
HTML
<asp:GridView ID="gvCustomer2" runat="server" OnRowDataBound="OnRowDataBound">
<AlternatingRowStyle BackColor="#CCCCCC" />
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#000099" ForeColor="White" BorderStyle="Dashed" BorderWidth="2px" Font-Bold="True" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]{
new DataColumn("CustomerId"),
new DataColumn("Name"),
new DataColumn("Value",typeof(int))});
dt.Rows.Add(1, "abc", 5);
dt.Rows.Add(2, "xyz", -6);
gvCustomer2.DataSource = dt;
gvCustomer2.DataBind();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int value = int.Parse(e.Row.Cells[2].Text);
if (value > 0)
{
LinkButton lnkSubmit = new LinkButton();
lnkSubmit.ID = "lnkSubmit";
lnkSubmit.Text = e.Row.Cells[2].Text;
lnkSubmit.CommandArgument = (e.Row.RowIndex + 1).ToString();
lnkSubmit.Click += new EventHandler(this.OnSubmit);
e.Row.Cells[2].Controls.Add(new Label() { Text = " " });
e.Row.Cells[2].Controls.Add(lnkSubmit);
}
}
}
protected void OnSubmit(object sender, EventArgs e)
{
LinkButton lnkSubmit = sender as LinkButton;
GridViewRow row = lnkSubmit.NamingContainer as GridViewRow;
string message = "CustomerId: " + row.Cells(0).Text + @"\n Name: " + row.Cells(1).Text + @"\n Value: " + row.Cells(2).Text;
MsgBox("aa");
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("CustomerId"),
New DataColumn("Name"),
New DataColumn("Value", GetType(Integer))})
dt.Rows.Add(1, "abc", 5)
dt.Rows.Add(2, "xyz", -6)
gvCustomer2.DataSource = dt
gvCustomer2.DataBind()
End Sub
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Dim lnkSubmit As LinkButton = TryCast(sender, LinkButton)
Dim row As GridViewRow = TryCast(lnkSubmit.NamingContainer, GridViewRow)
Dim message As String = "CustomerId: " & row.Cells(0).Text & "\n Name: " + row.Cells(1).Text & "\n Value: " + row.Cells(2).Text
MsgBox("aa")
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If Not e.Row.Cells(2).Text = " " Then
Dim value As Integer = Integer.Parse(e.Row.Cells(2).Text)
If value > 0 Then
Dim lnkSubmit = New LinkButton()
lnkSubmit.ID = "lnkSubmit"
lnkSubmit.Text = e.Row.Cells(2).Text
lnkSubmit.CommandArgument = (e.Row.RowIndex + 1).ToString()
AddHandler lnkSubmit.Click, AddressOf OnSubmit
e.Row.Cells(2).Controls.Add(New Label() With {.Text = " "})
e.Row.Cells(2).Controls.Add(lnkSubmit)
End If
End If
End If
End Sub
Screenshot