Solved

Populate a DataGrid or Gridview

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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