Filling Gridview without using database
Sometimes we might be in a situation where we need to fill a gridview from the Client itself and not from the database
for eg:-filling a gridview with values entered into a textbox.
For this we need to provide as data source a datatable that will fill based on the values we enter.
Declare as global or in the BLL(if you are using N-Tier Structure)
public static DataTable TempTable;
Global Varaible
DataRow Dtr;
Create Structure of the datatable
private void CreateStructure()
{
TempTable = new DataTable(“DatatableName”);
TempTable.Columns.Add(“ColumnName”, typeof(datatype));//eg-TempTable.Columns.Add(“Name”, typeof(string))
..add more..
}
Now to Fill the GridView
private void FillGrid()
{
Dtr = TempTable.NewRow();
Dtr["ColumnName"] = Textbox1.text;
..add the rest..
TempTable.Rows.Add(Dtr);
GridView1.DataSource = TempTable;
GridView1.DataBind();
}
Call CreateStructure() at pageLoad
Call FillGrid() at the event you want,eg button_Click
Note that you have to create boundfields for the gridview,ie the datafield of each field in the gridview,as those in the createstructure()
Related posts:








Leave your response!