Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Country" HeaderText="Country" />
</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)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, " ", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
bool isEmpty = false;
for (int i = 1; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Controls.Count > 0)
{
if (string.IsNullOrEmpty((e.Row.Cells[i].Controls[1] as Label).Text.Trim()))
{
isEmpty = true;
break;
}
}
else
{
if (string.IsNullOrEmpty(e.Row.Cells[i].Text.Trim()) || e.Row.Cells[i].Text.Trim() == " ")
{
isEmpty = true;
break;
}
}
}
if (!isEmpty)
{
(e.Row.Cells[0].Controls[0] as LinkButton).Enabled = false;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim isEmpty As Boolean = False
For i As Integer = 1 To e.Row.Cells.Count - 1
If e.Row.Cells(i).Controls.Count > 0 Then
If String.IsNullOrEmpty(TryCast(e.Row.Cells(i).Controls(1), Label).Text.Trim()) Then
isEmpty = True
Exit For
End If
Else
If String.IsNullOrEmpty(e.Row.Cells(i).Text.Trim()) OrElse e.Row.Cells(i).Text.Trim() = " " Then
isEmpty = True
Exit For
End If
End If
Next
If Not isEmpty Then
TryCast(e.Row.Cells(0).Controls(0), LinkButton).Enabled = False
End If
End If
End Sub
Screenshot