Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use DataGridView without using database data?

I have a client-server program and the server receives [Process ID, Hostname, App Name, File Path] and I want to put them in a table. As of now they are sent in one string. Is DataGridView applicable to use even if they are not inside a database or is there another option?

Thanks.

like image 776
user3406220 Avatar asked Apr 17 '26 18:04

user3406220


1 Answers

The short answer is Yes.

  • You can use a List<T> as DataSource
  • You can use a DataTable as DataSource (DataTable not related to db)
  • You can use it without DataSource and only defining columns and adding rows

Use with List<T> as DataSource for example:

var data= new List<DataClass>();
data.Add(new DataClass(){Property1=1 , Property2= "One"   });
data.Add(new DataClass(){Property1=2 , Property2= "Two"   });
data.Add(new DataClass(){Property1=3 , Property2= "Three" });

dataGridView1.DataSource= data;

And the result will be a dataGridView with 2 columns (Property1, property2) and 3 rows.

In above example, DataClass is a class like the following:

public class DataClass { public int Property1 {get; set;} public string Property2 {get; set;} }

For more advance scenarios you can use DataSource window to add new DataSource to your project. You can add Object datasource as well.

use with a DataTable as DataSource

You can create a DataTable and add your columns to it using dataTable.Columns.Add then add your rows using dataTable.Rows.Add and then set it as DataSource of grid.

Use without DataSource

DataGridView can work even without datasource. It's enough that you add some columns to DataGidView, then using datGridView1.Rows.Add add new rows to it.

like image 129
Reza Aghaei Avatar answered Apr 20 '26 06:04

Reza Aghaei