lijinchang

导航

GridView绑定空表头

 /// <summary>
        /// 绑定空表头事件
        /// </summary>
        /// <param name="sender"></param>
        protected override void Render(HtmlTextWriter sender)
        {
            //绑定空表头
            //DataTable dtYeWuZB = _BWCK_YeWuZBCountViewBLL.SelectBWCK_YeWuZBCountViewsDynamic("1=2", "", null);
            DataSet ds = SpHelper.Report_GetDataDetail("Business_393253_History", "620756993", 5, "2010-04-21 00:00:00", "2010-04-20 00:00:00", null);
            BLL.BusinessTool.BuildNoRecords(GV_Result, ds.Tables[0]);
            base.Render(sender);
        }
 
  /// <summary>
        /// Bind Empty Grid Header
        /// 根据一个object 绑定空表头 可以是一个实体,也可以是一个datatable
        ///
        /// Eg:
        ///  <asp:GridView ID="GridView1" runat="server">
        ///  </asp:GridView>
        /// 
        ///  protected override void Render(HtmlTextWriter writer)
        ///{
        ///    BLL.BusinessTool.BuildNoRecords(GridView1,new Entity.Share_ChargePlan());
        ///    base.Render(writer);
        ///}
        /// </summary>
        /// <param name="GV"></param>
        /// <param name="DataSource"></param>
        public static void BuildNoRecords( GridView GV, object DataSource )
        {
            if( GV.Rows.Count == 0 || GV.Rows[ 0 ].Cells[ 0 ].Text == "没有数据" )
            {
                if( DataSource is DataSet )
                {
                    _BusinessTool.BuildNoRecords( GV, ( ( DataSet ) DataSource ).Tables[ 0 ] );
                }
                else if( DataSource is DataTable )
                {
                    _BusinessTool.BuildNoRecords( GV, ( ( DataTable ) DataSource ) );
                }
                else
                {
                    _BusinessTool.BuildNoRecords( GV, DataSource, true );
                }
            }
        }
      private void BuildNoRecords( GridView gridView, DataTable dt )
        {
            if( gridView.Rows.Count == 0 || gridView.Rows[ 0 ].Cells[ 0 ].Text == "没有数据" )
            {
                dt.Clear();
                DataTable tmp = dt;
                DataRow dr = tmp.NewRow();
                tmp.Rows.Add( dr );
                SetDefaultValue( tmp );
                gridView.DataSource = tmp;
                gridView.DataBind();
                int columnCount = gridView.Rows[ 0 ].Cells.Count;
                gridView.Rows[ 0 ].Cells.Clear();
                gridView.Rows[ 0 ].Cells.Add( new TableCell() );
                gridView.Rows[ 0 ].Cells[ 0 ].ColumnSpan = columnCount;
                gridView.Rows[ 0 ].Cells[ 0 ].Text = "没有数据";
            }
            dt.Clear();
        }
 /// <summary>
        /// 根据实体绑定空表头
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="entity"></param>
        /// <param name="isEntity"></param>
        private void BuildNoRecords( GridView gridView, object entity, bool isEntity )
        {
            bool gvv = gridView.Visible;
            if( gridView.Rows.Count == 0 )
            {
                DataTable tmp = new DataTable();
                Type t = entity.GetType();
                PropertyInfo[] _PropertyInformation = t.GetProperties();
                for( int i = 0; i <= _PropertyInformation.Length - 1; i++ )
                {
                    if( _PropertyInformation[ 0 ].MemberType == MemberTypes.Property )
                    {
                        if( _PropertyInformation[ i ].Name == "IsBlankOut" )
                        {
                            System.Diagnostics.Debug.WriteLine( "tr2" + DateTime.Now.ToString() );
                        }
                        try
                        {
                            if( _PropertyInformation[ i ].PropertyType.Name != "Nullable`1" )
                                tmp.Columns.Add( _PropertyInformation[ i ].Name, _PropertyInformation[ i ].PropertyType );
                            else if( _PropertyInformation[ i ].PropertyType.ToString() == "System.Nullable`1[System.DateTime]" )
                                tmp.Columns.Add( _PropertyInformation[ i ].Name, typeof( DateTime ) );
                            else if( _PropertyInformation[ i ].PropertyType.ToString() == "System.Nullable`1[System.Int16]" )
                                tmp.Columns.Add( _PropertyInformation[ i ].Name, typeof( Int16 ) );
                            else if( _PropertyInformation[ i ].PropertyType.ToString() == "System.Nullable`1[System.Boolean]" )
                                tmp.Columns.Add( _PropertyInformation[ i ].Name, typeof( Boolean ) );
                            else if( _PropertyInformation[ i ].PropertyType.ToString() == "System.Nullable`1[System.Int32]" )
                                tmp.Columns.Add( _PropertyInformation[ i ].Name, typeof( Int32 ) );
                            else if( _PropertyInformation[ i ].PropertyType.ToString() == "System.Nullable`1[System.Decimal]" )
                                tmp.Columns.Add( _PropertyInformation[ i ].Name, typeof( decimal ) );
                            else if( _PropertyInformation[ i ].PropertyType.ToString() == "System.Nullable`1[System.Double]" )
                                tmp.Columns.Add( _PropertyInformation[ i ].Name, typeof( double ) );
                            else if( _PropertyInformation[ i ].PropertyType.ToString() == "System.Nullable`1[System.Byte]" )
                                tmp.Columns.Add( _PropertyInformation[ i ].Name, typeof( Byte ) );
                        }
                        catch
                        {
                        }
                    }
                }
                DataRow _DataRow = tmp.NewRow();
                tmp.Rows.Add( _DataRow );
                SetDefaultValue( tmp );
                gridView.DataSource = tmp;
                gridView.DataBind();
                int columnCount = gridView.Rows[ 0 ].Cells.Count;
                gridView.Rows[ 0 ].Cells.Clear();
                gridView.Rows[ 0 ].Cells.Add( new TableCell() );
                gridView.Rows[ 0 ].Cells[ 0 ].ColumnSpan = columnCount;
                gridView.Rows[ 0 ].Cells[ 0 ].Text = "没有数据";
                gridView.Rows[ 0 ].Attributes[ "onclick" ] = "";
                gridView.Visible = true;
            }
        }

posted on 2010-11-05 12:14  lijinchang  阅读(694)  评论(0编辑  收藏  举报