Solved

Populate a DataGrid or Gridview

Posted on 2010-08-27
4
501 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

939 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

8 Experts available now in Live!

Get 1:1 Help Now