简体   繁体   English

我可以在不使用数据库数据的情况下使用DataGridView吗?

[英]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. 我有一个客户端 - 服务器程序,服务器收到[进程ID,主机名,应用程序名称,文件路径],我想把它们放在一个表中。 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? DataGridView是否适用于即使它们不在数据库中还是有其他选项?

Thanks. 谢谢。

The short answer is Yes. 简短的回答是肯定的。

  • You can use a List<T> as DataSource 您可以使用List<T>作为DataSource
  • You can use a DataTable as DataSource (DataTable not related to db) 您可以使用DataTable作为DataSource (DataTable与db无关)
  • You can use it without DataSource and only defining columns and adding rows 您可以在没有DataSource情况下使用它,只定义列和添加行

Use with List<T> as DataSource for example: 例如,使用List<T>作为DataSource

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. 结果将是一个带有2列(Property1,property2)和3行的dataGridView。

In above example, DataClass is a class like the following: 在上面的示例中, DataClass是一个类如下的类:

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

For more advance scenarios you can use DataSource window to add new DataSource to your project. 对于更多高级方案,您可以使用DataSource窗口将新的DataSource添加到项目中。 You can add Object datasource as well. 您也可以添加Object数据源。

use with a DataTable as DataSource DataTable一起使用作为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. 您可以创建一个DataTable ,并使用添加列到它dataTable.Columns.Add然后使用添加行dataTable.Rows.Add ,然后将其设置为DataSource网格。

Use without DataSource 不使用DataSource

DataGridView can work even without datasource. 即使没有数据源,DataGridView也可以工作。 It's enough that you add some columns to DataGidView, then using datGridView1.Rows.Add add new rows to it. 向DataGidView添加一些列就足够了,然后使用datGridView1.Rows.Add向其中添加新行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用数据库的情况下在datagridview中显示数据 - How to show data in datagridview without using database 我可以将此数据从datagridview添加到数据库 - Can I add this data from datagridview to database 如何在C#Winforms中不使用数据库的情况下在列表上使用DatagridView - How to use DatagridView on a List without using Database in C# Winforms 如何从datagridview获取数据并将其插入数据库而不使用数据绑定? - how to get data from a datagridview and insert into database without using databinding? 嗨,我在winforms中使用datagridview,我已经使用数据库加载网格现在我想清除网格中的所有数据而不从数据库中删除数据 - Hi,i am using datagridview in winforms, i have load grid using database now i want to clear all the data from grid without removing data from database 如何在C#中将DataGridView的数据引用到SQL数据库中? - How can I reference data of DataGridView into SQL database in C#? 如何在数据库中引入数据并在DataGridView中查看日期? - How can I introduce data in database and to view dates in DataGridView? 我如何从datagridview获取所有数据并将其插入数据库 - How can i get all the data from datagridview and insert it into database 我可以在不使用DataSource属性的情况下在DataGridView中添加DataTable吗 - Can I add DataTable in DataGridView without using DataSource property 如何使用 EPPlus 将过滤后的 DataGridView 数据保存到 Excel 中? - How can I save filtered DataGridView data into Excel using EPPlus?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM