Hi softly21,
Check this sample. now take its reference and correct your code.
HTML
<asp:GridView ID="gvItems" runat="server" OnRowDataBound="OnRowDataBound">
</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.Add("Column0");
dt.Columns.Add("Column1");
dt.Rows.Add("", "a");
dt.Rows.Add("", "a");
dt.Rows.Add("", "a");
dt.Rows.Add("", "a");
dt.Rows.Add("", "");
dt.Rows.Add("", "a");
dt.Rows.Add("", "");
dt.Rows.Add("", "a");
dt.Rows.Add("", "a");
dt.Rows.Add("", "a");
this.gvItems.DataSource = dt;
this.gvItems.DataBind();
}
}
int m = 0;
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text != "" && e.Row.Cells[1].Text != " ")
{
m++;
e.Row.Cells[0].Text = m.ToString();
}
else
{
m = 0;
m++;
e.Row.Cells[0].Text = m.ToString();
}
}
}
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.Add("Column0")
dt.Columns.Add("Column1")
dt.Rows.Add("", "a")
dt.Rows.Add("", "a")
dt.Rows.Add("", "a")
dt.Rows.Add("", "a")
dt.Rows.Add("", "")
dt.Rows.Add("", "a")
dt.Rows.Add("", "")
dt.Rows.Add("", "a")
dt.Rows.Add("", "a")
dt.Rows.Add("", "a")
Me.gvItems.DataSource = dt
Me.gvItems.DataBind()
End If
End Sub
Private m As Integer = 0
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(1).Text <> "" AndAlso e.Row.Cells(1).Text <> " " Then
m += 1
e.Row.Cells(0).Text = m.ToString()
Else
m = 0
m += 1
e.Row.Cells(0).Text = m.ToString()
End If
End If
End Sub
Screenshot