如何在C#中创建DataGrid?

时间:2021-08-24 06:29:51

I want to add a DataGrid to a Form. 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.

我想将一个DataGrid添加到窗体。程序执行时,用户输入值,我在问题中使用这些值。我需要一个具有两列的表的类似实现,当用户输入值时,我将它们用于程序中的计算。

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#中执行此操作?

4 个解决方案

#1


In a winforms environment, you can bind strongly typed collections as the datasource; 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)

在winforms环境中,您可以将强类型集合绑定为数据源;集合中对象的每个属性都会生成一个列(严格来说,我相信它可以解决集合返回的类型的属性,而不是其中的单个项)

#2


If you are writing a WinForms App then you can use a DataTable to store the data and a DataGridView to display it. Simply create the DataTable:

如果您正在编写WinForms应用程序,那么您可以使用DataTable存储数据,使用DataGridView来显示它。只需创建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:

使用Designer将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. 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

我在一个只读的情况下完成了这个操作,其中DataTable从程序中填充并显示它。用户可以做的就是调整列,重新排序或设置列的可见性。要添加新行,您需要挂钩到RowsAdded事件

#3


Re-wording Rowland Shaw

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.

如果您想要用户输入,那么我认为您应该考虑使用更好的网格控件。数据网格不适用于此目的。我不记得是否已经为.Net重做了flexgrid(ocx one)。

#4


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的datatasource设置为此数据表,并将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.

如果要在完成更新后进行计算,则可以使用RowPrePaint事件,或者如果要在每个单元格中的数据更新后进行计算,则可以使用datagridview的CurrentCellChanged事件。

Hope this helps....

希望这可以帮助....

#1


In a winforms environment, you can bind strongly typed collections as the datasource; 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)

在winforms环境中,您可以将强类型集合绑定为数据源;集合中对象的每个属性都会生成一个列(严格来说,我相信它可以解决集合返回的类型的属性,而不是其中的单个项)

#2


If you are writing a WinForms App then you can use a DataTable to store the data and a DataGridView to display it. Simply create the DataTable:

如果您正在编写WinForms应用程序,那么您可以使用DataTable存储数据,使用DataGridView来显示它。只需创建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:

使用Designer将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. 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

我在一个只读的情况下完成了这个操作,其中DataTable从程序中填充并显示它。用户可以做的就是调整列,重新排序或设置列的可见性。要添加新行,您需要挂钩到RowsAdded事件

#3


Re-wording Rowland Shaw

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.

如果您想要用户输入,那么我认为您应该考虑使用更好的网格控件。数据网格不适用于此目的。我不记得是否已经为.Net重做了flexgrid(ocx one)。

#4


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的datatasource设置为此数据表,并将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.

如果要在完成更新后进行计算,则可以使用RowPrePaint事件,或者如果要在每个单元格中的数据更新后进行计算,则可以使用datagridview的CurrentCellChanged事件。

Hope this helps....

希望这可以帮助....