namespace XXX.COM{ public class ComClass {
PRivate static string EmptyText = "没有记录";
/// <summary> /// 绑定数据到GridView,当表格数据为空时显示表头 /// </summary> /// <param name="gridview"></param> /// <param name="table"></param> public static void GridViewDataBind(GridView gridview, DataTable table) { //记录为空重新构造Gridview if (table.Rows.Count == 0) { table = table.Clone(); table.Rows.Add(table.NewRow()); gridview.DataSource = table; gridview.DataBind(); int columnCount = table.Columns.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 = EmptyText; gridview.Rows[0].Cells[0].Style.Add("text-align", "center"); } else { //数据不为空直接绑定 gridview.DataSource = table; gridview.DataBind(); }}
}}
新闻热点
疑难解答