Recently I have facing gridview header display issues when DataSource is empty or null.
So for same i have googing and find the solution like how to show header even DataSource is empty or null.
Before I was written the blog post, think it like it's a chunk of code but might be useful to someone who facing the same issue.
Generally, writing the condition to check any DataTable, DataSet, List or any collection have data then we assign to that collection/DataTable to GridView DataSource.
So let's see example. How to create and assign dummy table to DataSource then display the header without any data record.
So for same i have googing and find the solution like how to show header even DataSource is empty or null.
Before I was written the blog post, think it like it's a chunk of code but might be useful to someone who facing the same issue.
Generally, writing the condition to check any DataTable, DataSet, List or any collection have data then we assign to that collection/DataTable to GridView DataSource.
So let's see example. How to create and assign dummy table to DataSource then display the header without any data record.
if (dt.rows.count > 0) { grdview.DataSource = dt; grdview.DataBind(); } else { var dummyTable = new DataTable(); dummyTable.Columns.Add("Column1"); dummyTable.Columns.Add("Column2"); dummyTable.Rows.Add(dummyTable.NewRow()); grdview.DataSource = dummyTable; grdview.DataBind(); grdview.Rows[0].Style.Add(HtmlTextWriterStyle.Display, "none"); }Hope you would like it and please share your suggestions and comments for improvement.
0 comments:
Post a Comment