是否可以在不使用数据库数据的情况下使用DataGridView? [英] Can I use DataGridView without using database data?

查看:173
本文介绍了是否可以在不使用数据库数据的情况下使用DataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端服务器程序,服务器收到[进程ID,主机名,应用程序名称,文件路径],我想将它们放在表中。截至目前,它们以一个字符串发送。即使DataGridView不在数据库中还是可以使用吗?

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?

谢谢。

推荐答案

简短的回答是。


  • 您可以使用 List< ; T> 作为 DataSource

  • 您可以使用 DataTable 作为 DataSource (与db不相关的DataTable)

  • 您可以在没有 DataSource <的情况下使用它/ code>,仅定义列并添加行

  • 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

List< T> ; 作为 DataSource 例如:

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;

结果将是具有2列(Property1,property2)和3行的dataGridView。

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

在上面的示例中, DataClass 是如下所示的类:

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

公共类DataClass
{
public int Property1 {get; set;}
公共字符串Property2 {get; set;}
}

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.

DataTable 一起用作数据源

use with a DataTable as DataSource

您可以创建 DataTable 并将列添加到使用 dataTable.Columns.Add ,然后使用 dataTable.Rows.Add 添加行,然后将其设置为 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.

在不使用 DataSource <的情况下使用/ strong>

Use without DataSource

即使没有数据源,DataGridView也可以工作。只需在DataGidView中添加一些列,然后使用 datGridView1.Rows.Add 向其中添加新行就足够了。

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.

这篇关于是否可以在不使用数据库数据的情况下使用DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆