Solved

Populate a DataGrid or Gridview

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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