Solved

Populate a DataGrid or Gridview

Posted on 2010-08-27
4
500 Views
Last Modified: 2012-06-21
I want to populate a datagrid and the data is not going ot come from any database. what i have is an object and i am going through loop through different values in that object and then populate the datagrid with the values that I get after  each loop.

this is what i want to do:

loop through object:
{
string x = object.somevalue
string y = object.somevalue

populatedgrid(x,y)
}


Function populategrid (sting x, string y)

populate grid by appending new values.





0
Comment
Question by:pratikshahse
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Astynax777
ID: 33542552
Are the number of columns predefined?
0
 
LVL 3

Accepted Solution

by:
Astynax777 earned 500 total points
ID: 33542638
My suggestion would be to create a DataTable and store it in the viewstate (or session if you need to maintain it cross-page) and add rows to the datatable which will be bound to your gridview.


// DataTable accessor

protected DataTable DT

{

   get

   {

       return ViewState["DT"] AS DataTable;

   }

}





// This is roughly drawn up without VS help

// Make sure you check for DT being null if it's possible

// It also doesn't include the initialization for the DT

protected void populateGridView(string x, string y)

{

   DataRow dr = DT.NewRow();

   dr["x"] = x;

   dr["y"] = y;

   DT.Rows.Add(dr);

}





// Remember that databinding happens after PreRender event

protected void Page_PreRender(object sender, EventArgs e)

{

   myGridView.DataSource = DT;

   myGridView.DataBind();

}

Open in new window

0
 
LVL 5

Expert Comment

by:tvPrasad
ID: 33542651
myGridView.DataSource = <your object name goes here>;
   myGridView.DataBind();
0
 
LVL 3

Expert Comment

by:raaziq
ID: 33543508
It needs to be done like, Create a datatable, Add object values into the datatable, just bind the datatable.

thats it
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now