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

  • 相关阅读:
    curl获取HTTP返回状态码
    存储过程中如何实现从数组获取数据
    ElasticsearchParseException: malformed, expected settings to start with 'object', instead was [VALUE_STRING]
    【并发编程】如果让你用三个线程循环打印ABC,你有几种写法?
    【基础】IdentityHashMap
    【基础】ThreadPoolExecutor
    【算法】快速排序
    【Java8新特性Stream】list转map
    【算法】华为南研所-括号匹配
    windows sourceTree 密码错误
  • 原文地址:https://www.cnblogs.com/lijinchang/p/1869807.html
Copyright © 2020-2023  润新知