user2488166
user2488166

Reputation: 61

GridView.Row.Count always 0, How to solve?

I am new in ASP.NET. I use a GridView. When I Count rows of GridView, it shows 0 always. I am giving code. But when I run the code browser, It shows data. How solve this problem?

My CS code is:

protected void btnSend_OnClick(object sender, EventArgs e)
    {
        string MemberList= (ViewState["Memberlist"]).ToString();
        List<PreparedEmail> preparedEmail = new List<PreparedEmail>();
        Utility util = new Utility();
        int count = 0;

        int i1 = dlClients.Rows.Count; // it shows always 0
        #region Send Invitation
        foreach (GridViewRow mail in dlClients.Rows)
        {
          //some code here, It is never execute as dlClients.Rows shows 0
         }                            
}

My aspx code is :

<asp:GridView ID="dlClients" EnableViewState="false" runat="server" AutoGenerateColumns="False" ClientIDMode="Static" GridLines="None" AllowPaging="false" CssClass="table table-bordered table-striped" OnPreRender="dlClients_PreRender" DataKeyNames="MemberId" PageSize="10" ShowHeader="true"> 

And My data binding code:

Protected void dlClients_PreRender(object sender, EventArgs e)
    {
        List<CircleUser> memberList = new CircleUserBusinessLogic().GetAll();
        dlClients.DataSource = memberList;

        dlClients.DataBind();
        //GridUpdatePanel.Update();
        ViewState["Memberlist"] = memberList;

        if (dlClients.Rows.Count > 0)
        {
            //Replace the <td> with <th> and adds the scope attribute
            dlClients.UseAccessibleHeader = true;

            //Adds the <thead> and <tbody> elements required for DataTables to work
            dlClients.HeaderRow.TableSection = TableRowSection.TableHeader;

            //Adds the <tfoot> element required for DataTables to work
            dlClients.FooterRow.TableSection = TableRowSection.TableFooter;
        }
    }

Upvotes: 1

Views: 5263

Answers (4)

Razvan
Razvan

Reputation: 326

EnableViewState = "true" is a way to pass you data from one page to another

Upvotes: 1

Ponmani Chinnaswamy
Ponmani Chinnaswamy

Reputation: 855

Normally asp button click event called first then gridview prerender hits second. So the Gridview rows count is 0.

enter image description here

Try to bind the gridview in PageLoad Method or Button click.

here sample bind...

protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack )
            {
                DataTable dt = new DataTable();
                dt = GetData();
                dlClients.DataSource = dt;

                dlClients.DataBind();
            }
        }

Upvotes: 4

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28423

Call your Gridview PreRender Event in Button click event like below:

        protected void btnSend_OnClick(object sender, EventArgs e)
        {
            dlClients_PreRender(sender, args);
            string MemberList = (ViewState["Memberlist"]).ToString();
            List<PreparedEmail> preparedEmail = new List<PreparedEmail>();
            Utility util = new Utility();
            int count = 0;

            int i1 = dlClients.Rows.Count;             
            foreach (GridViewRow mail in dlClients.Rows)
            {
                //some code here, It is never execute as dlClients.Rows shows 0
            }
        }

Upvotes: 1

Poul Bak
Poul Bak

Reputation: 10940

The button_click event is called before your prerender event. This Means, that when the button click event is running the databind has not run, so the GridView is empty. To solve it, you can databind in the page's load event. That is called before the click events.

Upvotes: 1

Related Questions