DataGrid中没有数据时显示表头

if (dt.Rows.Count > 0)
{
     GridView1.DataSource = dt;
     GridView1.DataBind();
}
else
{
     dt.Rows.Add(dt.NewRow());
     GridView1.DataSource = dt;
     GridView1.DataBind();
     int columnCount = GridView1.Rows[0].Cells.Count;
     GridView1.Rows[0].Cells.Clear();
     GridView1.Rows[0].Cells.Add(new TableCell());
     GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
     GridView1.Rows[0].Cells[0].Text = "目前还没有数据";
}

你可能感兴趣的:(datagrid)