Saturday, March 24, 2012

Trouble with ArrayList and GridView

Hi all,

I am trying to display somedata from a postgres database, and display it using an asp.net page, using a GridView. The basic thing I am doing is, getting data from the database, storing it in an ArrayList, and passing the ArrayList to the DataSource for the GridView.

The problem is the data that I need to display is 3 dimensional. My questions are:

How can I create an ArrayList that can hold 3 columns for each row? How do I populate such an ArrayList? How can I pass this data to the GridView? How can I getHyperlinks on all the values displayed in the first column?

I am a beginner atasp.net. Any help will be appreciated.

Thanks
George

Hey,

The preferred data object for this is a DataTable. Though I don't know if there is ADO.NET support for a postgres database (I don't know anything about that database system), you could still make use of a DataTable, as that works well with the GridView.

The other alternative is that you get the data back from the postgres database and populate it into a business object that contains the three properties of information you are returning. An arraylist can be a list of business objects, which each object has the three properties.


Ok, I tried using the DataTable, so now I am retrieving data from my table, populating the data table and displaying it. This works fine.

But what I need is a query that retrieves data in a 'for' loop, since I need to pass different variables to the query everytime. So I pass a variable, run the query, store the result somewhere(an ArrayList?), then pass the next variable, run the query, append the result to my storage and so on...

I am not sure if we can append data to a DataTable(if we can, I would like to know how). And incase ArrayList is the best option available, how would I be able to create an ArrayList that would contain 3 columns? How can this be displayed in a GridView?

Thanks for the reply. Hoping to get around this problem soon.

George


to add hyper link all you have to do is right click over the grid veiw and select Show Smart Tag >>>

Then in the Smart Tag of the GridVeiw click the Edit columns link

A field wizard will pop, in that make sureAuto-generate field option is checked

In the Available field sectionselect the HyperLinkField and click the Add button

Then Select the HyperLinkField in the selected field section

Inside the HyperLinkField Properties section :-

? In appearrance section make the text to go

? In data section make the DataNagivationUrlFormatString to topic.aspx?TopicId={0}

(Here topic.aspx is the page you wan to redirect and TopicId is the QueryString Variable)

? In data section make the DataNavigationUrlField toTopicId

(This actually specify the value of {0})


At the first, DataTable is the best option for you!

Yes, you can add a DataRow to a DataTable:

// Define a DataTable

DataTable tbl =newDataTable();

// Add your Columns to DataTable

tbl.Columns.Add("Col1");

tbl.Columns.Add("Col2");

// Create a DataRow for this table

DataRow row = tbl.NewRow();

// Set you row values

row[0] ="c1";

row["Col2"] = 25;

// Add row to table

tbl.Rows.Add(row);

Also, you can use Rows of your table in "for" loop:

foreach (DataRow rwin tbl.Rows)

{

Response.Write(rw["Col1"]);

}

or

for (int i = 0; i < tbl.Rows.Count; i++)

{

Response.Write(tbl.Rows[i]["Col2"]);

}


Hello,

Yes, the code above can be used to iterate through a data table. You can also use a DataView to filter the data by using the RowFilter property:

http://msdn2.microsoft.com/en-us/library/system.data.dataview(VS.71).aspx
http://davidhayden.com/blog/dave/archive/2006/02/11/2798.aspx

0 comments:

Post a Comment