简体   繁体   中英

Count rows in datasource/grid

How can I count the rows in this context before data binding?

This is what I tried but returns always 0 even if there are rows.

protected void OnRowDataBoundZeroLevel(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string code = zeroLevelGrid.DataKeys[e.Row.RowIndex].Value.ToString();
        GridView firstLevelGrid = e.Row.FindControl("firstLevelGrid") as GridView;
        firstLevelGrid.DataSource = GetData(string.Format("IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'{0}')) SELECT * from [{0}]", code));

        var isData = firstLevelGrid.Rows.Count;
        if (isData>0)
        {
            firstLevelGrid.DataBind();
        }
    }
}

Converting my comment to answer.

Actual binding works in the DataBind call, so before databind Rows.Count always will be empty. If GetData returns correct IEnumerable structure, then you can check it for rows count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM