Reset serial (row) number in GridView based on condition using C# and VB.Net in ASP.Net

softly21
 
on Mar 10, 2020 04:14 AM
Sample_141519.zip
1475 Views

I want to display row number in column0 of gridview with condition if column1 = "" do nothing and start count form 1 again

col0 col1

1      a

2      a

3      a

4      a

1     

2      a      

1     

2      a

3      a

4      a

    Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim m As Integer
        If e.Row.Cells(1).Text = "" Then

            m = e.Row.RowIndex + 1
        Else
            e.Row.Cells(0).Text = (i + 1 - m).ToString

        End If

        If e.Row.Cells(0).Text = "0" Or e.Row.Cells(0).Text = "-1" Then
            e.Row.Cells(0).Text = ""
        End If
   End Sub
Download FREE API for Word, Excel and PDF in ASP.Net: Download
Andrea
 
on Mar 10, 2020 04:14 AM
Hi @softly21,
Please try the following

How to display Row Number in GridView in ASP.Net

It might help you.

Cheers Andrea.
softly21
 
on Mar 10, 2020 04:23 AM
on Mar 10, 2020 07:52 AM

It's not work
Container.DataItemIndex
not count form 1 again but really thankyou

And this time I can resloved,Just declare variable in module because in rowdatabound envent will declare new variable every row sorry for my bad question

arunkurmi
 
on Mar 11, 2020 05:01 AM

Hi softly21,

I am working on it will get back to you soon.

arunkurmi
 
on Mar 11, 2020 06:45 AM

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 != "&nbsp;")
        {
            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 <> "&nbsp;" 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