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);
            }
        }



Combine Two DataTables Into One Datatable


How to combine two datatables into single datatable - C# 





public void getgroupRecentActivity()
        {
            DataTabledtjoingroup =   Group.GetAllGroupDetailbydate(Request.QueryString["GroupID"]);
            DataTabledtcreateGroupForum = Group.GetAllGroupForumDetailbydate(Request.QueryString["GroupID"]);
            DataSetdsjoin1 = new DataSet();
            DataSetdsjoin2 = new DataSet();        
            foreach(DataRow dr indtjoingroup.Rows)
            {
                dr["GroupName"] = "commented on the forum";
                dr.AcceptChanges();
            }
            foreach(DataRow dr1 indtcreateGroupForum.Rows)
            {
                dr1["GroupName"] = "joined the Group";
                dr1.AcceptChanges();
            }
            dsjoin1.Tables.Add(dtjoingroup);
            dsjoin2.Tables.Add(dtcreateGroupForum);
            dsjoin1.Merge(dsjoin2);
           
            DataTabledtrecentActivity = dsjoin1.Tables[0];
            dtrecentActivity.DefaultView.Sort = "date desc";
            dtrecentActivity.AcceptChanges();
            DataViewdv = new DataView(dtrecentActivity);
            dv.Sort = "date desc";
            dtRecentActivity = dv.ToTable("dtrecentActivity");
            //dtRecentActivity = dtrecentActivity;
            dtRecentActivity.AcceptChanges();
            //GridView1.DataSource = dtRecentActivity;
            //GridView1.DataBind();
            //DataView dv = new DataView(dtrecentActivity);
            //dv.Sort = "date desc";
            //DataTable dtrecentActivityNew = dv.Table;
       
        }

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger