How to display the table header when the data source of the GridView is empty Solution: Method 1: Use its EmptyTemplate to implement it. Write a static table in the template; Disadvantage: It is troublesome. You need to set each GridVIew. Method 2: if the data source is able, a blank row of DataTable is always returned when no data exists; If the data source is a collection class (ArrayList, List <T>, etc.), when no data exists, an empty entity is generated and added to the Collection class. Disadvantage: it is still troublesome. Method 3: This is also the method to introduce: extend the GridView to implement. inherit GridVie and rewrite the Render method. When the data source is empty, perform the following processing: /// <Summary> /// GridView extension control /// @ Author: jianyi0115@163.com /// </Summary> Public class GridView: System. Web. UI. WebControls. GridView { Private bool _ enableEmptyContentRender = true; /// <Summary> /// Whether the title line is displayed when the data is null /// </Summary> Public bool EnableEmptyContentRender { Set {_ enableEmptyContentRender = value ;} Get {return _ enableEmptyContentRender ;} }Private string _ EmptyDataCellCssClass; /// <Summary> /// Information cell style class when it is null /// </Summary> Public string EmptyDataCellCssClass { Set {_ EmptyDataCellCssClass = value ;} Get {return _ EmptyDataCellCssClass ;} } /// <Summary> /// Output content when it is null /// </Summary> /// <Param name = "writer"> </param> Protected virtual void RenderEmptyContent (HtmlTextWriter writer) { Table t = new Table (); // create a table T. CssClass = this. CssClass; // copy all property T. GridLines = this. GridLines; T. BorderStyle = this. BorderStyle; T. BorderWidth = this. BorderWidth; T. CellPadding = this. CellPadding; T. CellSpacing = this. CellSpacing; T. HorizontalAlign = this. HorizontalAlign; T. Width = this. Width; T. CopyBaseAttributes (this ); TableRow row = new TableRow (); T. Rows. Add (row ); Foreach (DataControlField f in this. Columns) // generate table header { TableCell cell = new TableCell (); Cell. Text = f. HeaderText; Cell. CssClass = "TdHeaderStyle1 "; Row. Cells. Add (cell ); } TableRow row2 = new TableRow (); T. Rows. Add (row2 ); TableCell msgCell = new TableCell (); MsgCell. CssClass = this. _ EmptyDataCellCssClass; If (this. EmptyDataTemplate! = Null) // the second row, use the template { This. EmptyDataTemplate. InstantiateIn (msgCell ); } Else // the second row, use the EmptyDataText { MsgCell. Text = this. EmptyDataText; } MsgCell. HorizontalAlign = HorizontalAlign. Center; MsgCell. ColumnSpan = this. Columns. Count; Row2.Cells. Add (msgCell ); T. RenderControl (writer ); } Protected override void Render (HtmlTextWriter writer) { If (_ enableEmptyContentRender & (this. Rows. Count = 0 | this. Rows [0]. RowType = DataControlRowType. EmptyDataRow )) { RenderEmptyContent (writer ); } Else { Base. Render (writer ); } } } } Solution 1: DataTable dt_Test = new DataTable (); Dt_Test = obj. GetList (); If (dt_Test.Rows.Count = 0) { Dt_Test.Rows.Add (dt. NewRow ()); Gv_Test. DataSource = dt_Test; Gv_Test.DataBind (); Int columnCount = dt_Test.Rows [0]. Cells. Count; Gv_Test.Rows [0]. Cells. Clear (); Gv_Test.Rows [0]. Cells. Add (new TableCell ()); Gv_Test.Rows [0]. Cells [0]. ColumnSpan = columnCount; Gv_Test.Rows [0]. Cells [0]. Text = "no record "; Gv_Test.Rows [0]. Cells [0]. Style. Add ("text-align", "center "); } Else { Gv_Test. DataSource = dt_Test; Gv_Test.DataBind (); } Solution 2: Private void ShowNullTable (GridView grd) { If (grd. Rows. Count> 0) return; // if there is data, do not process it If (grd. DataSource! = Null) { If (DataTable) grd. DataSource). Rows. Count> 0) { Return; } } GridViewRow row = new GridViewRow (-1,-1, DataControlRowType. EmptyDataRow, DataControlRowState. Normal ); Foreach (DataControlField field in grd. Columns) { TableCell cell = new TableCell (); Cell. Text = field. HeaderText; Cell. Width = field. HeaderStyle. Width; Cell. Height = field. HeaderStyle. Height; Cell. ForeColor = field. HeaderStyle. ForeColor; Cell. Font. Size = field. HeaderStyle. Font. Size; Cell. Font. Bold = field. HeaderStyle. Font. Bold; Cell. Font. Name = field. HeaderStyle. Font. Name; Cell. Font. Strikeout = field. HeaderStyle. Font. Strikeout; Cell. Font. Underline = field. HeaderStyle. Font. Underline; Cell. BackColor = field. HeaderStyle. BackColor; Cell. VerticalAlign = field. HeaderStyle. VerticalAlign; Cell. HorizontalAlign = field. HeaderStyle. HorizontalAlign; Cell. CssClass = field. HeaderStyle. CssClass; Cell. BorderColor = field. HeaderStyle. BorderColor; Cell. BorderStyle = field. HeaderStyle. BorderStyle; Cell. BorderWidth = field. HeaderStyle. BorderWidth; Row. Cells. Add (cell ); } TableItemStyle headStyle = grd. HeaderStyle; TableItemStyle emptyStyle = grd. EmptyDataRowStyle; EmptyStyle. Width = headStyle. Width; EmptyStyle. Height = headStyle. Height; EmptyStyle. ForeColor = headStyle. ForeColor; EmptyStyle. Font. Size = headStyle. Font. Size; EmptyStyle. Font. Bold = headStyle. Font. Bold; EmptyStyle. Font. Name = headStyle. Font. Name; EmptyStyle. Font. Strikeout = headStyle. Font. Strikeout; EmptyStyle. Font. Underline = headStyle. Font. Underline; EmptyStyle. BackColor = headStyle. BackColor; EmptyStyle. VerticalAlign = headStyle. VerticalAlign; EmptyStyle. HorizontalAlign = headStyle. HorizontalAlign; EmptyStyle. CssClass = headStyle. CssClass; EmptyStyle. BorderColor = headStyle. BorderColor; EmptyStyle. BorderStyle = headStyle. BorderStyle; EmptyStyle. BorderWidth = headStyle. BorderWidth; If (grd. Controls. Count = 0) { Grd. Page. Response. Write ("<script language = 'javascript '> alert (' The DataBind method must be executed before the table class is initialized and the EmptyDataText attribute is set to not empty! '); </Script> "); } Else { Grd. Controls [0]. Controls. Clear (); // message displayed when no data is deleted Grd. Controls [0]. Controls. AddAt (0, row ); GridViewRow newRow = new GridViewRow (0, 0, DataControlRowType. DataRow, DataControlRowState. Normal ); NewRow. Cells. Add (new TableCell ()); NewRow. Cells [0]. ColumnSpan = Columns. Count; NewRow. Cells [0]. HorizontalAlign = HorizontalAlign. Center; NewRow. Cells [0]. Text = EmptyDataText; Grd. Controls [0]. Controls. Add (newRow ); } } |