Home » ASP.Net, GridView Tips, Tech Corner

Filling Gridview without using database

27 March 2009 No Comments Posted By:Vivek

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:

  1. Edit,Update,Cancel In a GridView

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>