Solved

Populate a DataGrid or Gridview

Posted on 2010-08-27
4
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 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