简体   繁体   English

如何在C#中创建DataGrid?

[英]How do I create a DataGrid in C#?

I want to add a DataGrid to a Form. 我想将DataGrid添加到窗体。 When the program executes, the user enters values and I use those values in the problem. 程序执行时,用户输入值,我在问题中使用这些值。 I need a similar implementation for a table with two columns and when the user enters values I use them to for calculation in the program. 对于具有两列的表,我需要类似的实现,并且当用户输入值时,我将它们用于程序中的计算。

There is no requirement to save these values to a database, they are just going to be used in the program. 不需要将这些值保存到数据库中,它们仅将在程序中使用。

How do I do this in C#? 如何在C#中执行此操作?

In a winforms environment, you can bind strongly typed collections as the datasource; 在winforms环境中,您可以将强类型的集合绑定为数据源。 Each property of the objects in the collection makes a column (Strictly speaking, I believe it works out the properties for the type that the collection returns, rather than the individual items in it) 集合中对象的每个属性都构成一列(严格来说,我相信它可以计算出集合返回的类型的属性,而不是其中的单个项)

If you are writing a WinForms App then you can use a DataTable to store the data and a DataGridView to display it. 如果要编写WinForms应用程序,则可以使用DataTable存储数据,并使用DataGridView显示数据。 Simply create the DataTable: 只需创建DataTable:

dataTable = new DataTable();

Create the columns you need programatically: 以编程方式创建所需的列:

var columnSpec = new DataColumn();
columSpec.DataType = typeof(decimal);  // If it holds a decimal
columSpec.ColumnName = "Interest Rate";
dataTable.Columns.Add(columnSpec);

Add the DataGridView to the form using the Designer - but don't and then once the table has been created bind it to the grid using: 使用设计器将DataGridView添加到表单-但是不要这样做,一旦创建了表,就可以使用以下方法将其绑定到网格:

dataGridView.DataSource = dataTable;

You can set the properties on the grid from the designer view. 您可以从设计器视图在网格上设置属性。

I have done this in a read-only case where the DataTable is populated from the program and just displayed it. 我是在只读情况下完成此操作的,其中从程序中填充了DataTable并仅显示了它。 All the user can do is resize, reorder or set the visibility on the columns. 用户所能做的就是调整大小,重新排序或设置列的可见性。 To add new rows you'll need to hook into the RowsAdded event 要添加新行,你需要挂接到RowsAdded事件

Re-wording Rowland Shaw 重新措辞罗兰·肖

You need not have a database to bind to the datagrid. 您无需具有数据库即可绑定到数据网格。 If you have the data filled in a strongly typed or a generic collection you can bind the datagrid to the collection. 如果您将数据填充在强类型或通用集合中,则可以将数据网格绑定到该集合。 The datagrid will fill the data from the collection. 数据网格将填充集合中的数据。

It will take the property names as the columns, and the rows will display as per the rows in the collection. 它将属性名称作为列,行将按照集合中的行显示。

If you want user input, then I think you should consider using a better grid control. 如果您需要用户输入,那么我认为您应该考虑使用更好的网格控件。 The datagrid is not suitable for this purpose. 数据网格不适用于此目的。 I dont' remember if flexgrid (the ocx one) has been redone for .Net. 我不记得是否已经将Flexgrid(OCX版本)重做为.Net。

You can use a datagridview and build a datatable and add columns through your code and set the datatasource of your datagridview as this datatable and set AllowUserToAddRows in properties window of Dataggridview to true ( true is the default value ). 您可以使用datagridview并构建数据表,并通过代码添加列,并将datagridview的数据源设置为此数据表,并将Dataggridview的属性窗口中的AllowUserToAddRows设置为true(true是默认值)。

If you want to make the calculation after a full ro update is made then you can use RowPrePaint event or if you want the calculation to be made after the data in each cell gets updated then you can use the CurrentCellChanged event of the datagridview. 如果要在进行完全ro更新之后进行计算,则可以使用RowPrePaint事件,或者如果要在更新每个单元格中的数据之后进行计算,则可以使用datagridview的CurrentCellChanged事件。

Hope this helps.... 希望这可以帮助....

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM