Kalpa-W
Kalpa-W

Reputation: 358

How do I display the retrieved data in a grid view

How do I display the data I get as output from a method, in a grid view line by line? This has to be done without using a database. Please advice.

Upvotes: 0

Views: 175

Answers (2)

Ryan Mann
Ryan Mann

Reputation: 5357

Now for Windows Forms, it's mostly the same but you don't have to do any markup etc... the control has a GUI and does all that, and you can control the width's of columns etc as such...

List<MyObject> items = new List<MyObject>();
items.Add(new MyObject() { Name = "Bob", Address = "101 Some Stree SomeCity SomeState, 11111" });
dataGridView1.DataSource = items;
dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

Upvotes: 0

Ryan Mann
Ryan Mann

Reputation: 5357

Are you talking GridView in asp.net webforms, or GridView in Windows Forms. If you are talking about a WebForms GridView you can bind any collection of data to the GridView. Generally though I use a ListView instead for more control over the markup.

Say you have an object and a collection for it,

public class MyObject
{
    public string Name { get; set; }
    public string Address { get; set; }
    //.......
}

//run this code in OnInit, public override OnInit(....)....

private List<MyObject> Items = new List<MyObject>();
MyObject mObj = new MyObject() { Name="Test", Address="SomeAddress" };
dataGrid.DataSource = Items;
dataGrid.DataBind();

And in the markup...

use <%# Eval("Name") %> and <%# Eval("Address") %> to get the values on that iteration of MyObject.

This applies to the ListView as well. The reason I like the listview is the ListView has templates for the Layout, Item, Alternating Item, EmptyData, and InsertItem methods. It gives you 100% control over the markup used to render the underlying data source.

You don't have to use a database in either platforms. In .Net DataSource doesn't mean backing database. It means something that serves data. That could be a collection like above, or a Database datasource..

Really it comes down to anything that implements IEnumerable.

Upvotes: 1

Related Questions