Wednesday 6 April 2011

How to show Empty gridview using C#?


How to show Empty grid view using ASP.NET

 C#


protected void Page_Load(objectsender, EventArgs e)
        {
            if(!IsPostBack)
                GetAllReportAbuse();
        }
        privatevoid GetAllReportAbuse()
        {
            try
            {
                grdReportAbuse.DataSource = Forum.GetAllAbuseReportData();
                grdReportAbuse.DataBind();
                if(grdReportAbuse.Rows.Count == 0)
                    EmptyGridFix(grdReportAbuse);  // call the Function
            }
            catch(Exception ex)
            {
                AppBase.handleException("Form:ReportAbues, Function:GetAllReportAbuse()", ex);
            }
        }



  protected void EmptyGridFix(GridViewgrdView)
        {
            try
            {
                if(grdView.Rows.Count == 0 &&
                    grdView.DataSource != null)
                {
                    DataTabledt = null;
                    if(grdView.DataSource is DataSet)
                    {
                        dt = ((DataSet)grdView.DataSource).Tables[0].Clone();
                    }
                    elseif (grdView.DataSource isDataTable)
                        dt = ((DataTable)grdView.DataSource).Clone();

                    if(dt == null)
                        return;

                    dt.Rows.Add(dt.NewRow()); // add empty row
                    grdView.DataSource = dt;
                    grdView.DataBind();

                    // hide row
                    grdView.Rows[0].Visible = false;
                    grdView.Rows[0].Controls.Clear();
                }

                // normally executes at all postbacks
                if(grdView.Rows.Count == 1 &&
                    grdView.DataSource == null)
                {
                    boolbIsGridEmpty = true;

                    // check first row that all cells empty
                    for(int i = 0; i < grdView.Rows[0].Cells.Count; i++)
                    {
                        if (grdView.Rows[0].Cells[i].Text != string.Empty)
                        {
                            bIsGridEmpty = false;
                        }
                    }
                    // hide row
                    if(bIsGridEmpty)
                    {
                        grdView.Rows[0].Visible = false;
                        grdView.Rows[0].Controls.Clear();
                    }
                }
            }
            catch(Exception ex)
            {
                AppBase.handleException("Form:ReportAbues, Function:EmptyGridFix", ex);
            }
        }



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger