locked
Give serial number for girdview row RRS feed

  • Question

  • User-807418713 posted

    Hello

    In Gridview Row Databound I have used this code

     if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (DataBinder.Eval(e.Row.DataItem, "difference") != null)
                {
                  
                    if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "difference")) == 0)
                    {
    
                        e.Row.Visible = false;
                    }
                }
    
               
            }

    After visible false i want to show serial no for each record..

    How to do so..

    Monday, August 20, 2018 11:34 AM

Answers

  • User-893317190 posted

    Hi Gopi.MCA,

    You could use gridview’s ItemTemplate and use a counter in your OnRowDateBound event.

    Below is my code.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField>
    
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
                    <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
                    <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
                </Columns>
    

    Code behind.

    private int count = 1;
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
    
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                   
                    if (DataBinder.Eval(e.Row.DataItem, "ContactName") != null)
                    {
    
                        if ((DataBinder.Eval(e.Row.DataItem, "ContactName") as string).StartsWith("A"))
                        {
    
                            e.Row.Visible = false;
                        }
                        else
                        {
                            (e.Row.FindControl("Label1") as Label).Text = count.ToString();
                            count++;
                        }
                    }
    
    
                }
            }
    
    

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 21, 2018 2:15 AM