如何在隐藏某一行后在gridview列中显示行号(仅可见行)

时间:2017-07-13 01:26:08

标签: c# asp.net gridview

这是我的前端代码:

<asp:TemplateField HeaderText="NO.">
                                      <ItemTemplate ><div ID="PicCounter" style="text-align: center; color:black"><%# Container.DataItemIndex + 1 %></div></ItemTemplate>
                             </asp:TemplateField>     

虽然这是我的后端代码:

 int sum = 0;
    protected void gv_Action_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                Label Action = (Label)e.Row.FindControl("ACTION");//take lable id

                if (Action.Text.ToString() == "0")
                {
                    e.Row.Visible = false;
                }
                else if (Action.Text.ToString() != "")
                {
                    sum += int.Parse(Action.Text);
                    lblTotalNoti.Text = sum.ToString();


                }



            }
        }

        catch (Exception ex)
        {
            Log.Write("gv_Action_RowDataBound() Error: " + ex.Message.ToString() + ex.StackTrace.ToString());
            ERRORMSG.Text = "gv_Action_RowDataBound() Error: " + ex.Message.ToString();

        }

    }

我想在隐藏一些行之后为网格视图的列中的每个可见行添加行编号。我的问题是我隐藏的行也编号。请帮助我,我是初学者。

1 个答案:

答案 0 :(得分:0)

我自己解决了:把它添加到后端

if (e.Row.Visible == true)
                {
                    numVisible += 1;

                    Label lblRowCounter = (Label)e.Row.FindControl("lblRowCounter");//take lable id

                    lblRowCounter.Text = numVisible.ToString();

                }