2014年7月13日 星期日

[RESOLVED] How to Insert A Row in a GridView if GridView is empty DataSet


How to insert a new Row in a GridView if GridView is empty DataSet in C#?


A GridView does not even appear if there is no records in a DataSet, so how can a user add a new row if the GridView is not displayed?

If there is even 1 record in the dataset, the NEW option is there, but not if empty ... 


Thank you



Hi MiguelRVR,


There is an EmptyDataTemplate that you can use to add a button that calls code behind where you can then easily insert a datarow.  


This causes a postback that then displays the GridView the way it should be, with the row just added to start it off.


More about EmptyDataTemplate:



Hope this helps.


Thanks,


Jatin



Hi


You may check this option. You are binding the data from a data table so if there are no rows in the datatable then insert an empty data row into the table and add the text 'No data found' for grid view first cell.


Below is the example.


if (dt.Rows.Count != 0) //There are records in the table bind it to the grid
{
grid_Data.DataSource = dt;
grid_Data.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
grid_Data.DataSource = dt;
grid_Data.DataBind();
int columncount = grid_Data.Rows[0].Cells.Count;
grid_Data.Rows[0].Cells.Clear();
grid_Data.Rows[0].Cells.Add(new TableCell());
grid_Data.Rows[0].Cells[0].ColumnSpan = columncount;
grid_Data.Rows[0].Cells[0].Text = "No data found";
}






沒有留言:

張貼留言