如何在 devexpress gridcontrol 中添加新行?(WinForms C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19062111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:00:14  来源:igfitidea点击:

How to add new line of row in devexpress gridcontrol?(WinForms C#)

c#winformsdevexpress

提问by Crystal Mae Perez

I want to add a new line of row when pressing a button. In datagridview it would be: datagridview1.Rows.Add()

我想在按下按钮时添加一行新行。在 datagridview 中,它将是: datagridview1.Rows.Add()

What is the equivalent code for that in gridcontrol? Please help me.

gridcontrol 中的等效代码是什么?请帮我。

回答by pixelbadger

The DevExpress GridControlmust always be bound to a datasource: you cannot add rows directly to the GridControlobject or its child GridViews.

DevExpressGridControl必须始终绑定到数据源:您不能直接向GridControl对象或其子对象添加行GridViews

Instead, you must bind your GridControlto a data source (via the GridControl.DataSourceproperty), and add/remove rows via this data source.

相反,您必须将您GridControl的数据源绑定到数据源(通过GridControl.DataSource属性),并通过此数据源添加/删除行。

See the 'Binding To Data'documentation at the DevExpress site for more information on the kinds of data sources that can be used with a GridControl.

请参阅“绑定到数据”在DevExpress的网站文档上可与使用的各种数据源的详细信息GridControl

回答by Marko Juvan?i?

You cannot add a new row directly to your GridControl, since this is just a container for the views. However, if you're using a GridViewinside your GridControl(or any other descendant of ColumnView), you can add a new row using AddNewRow()method.

您不能直接向您的 中添加新行GridControl,因为这只是视图的容器。但是,如果您在(或 ColumnView 的任何其他后代)GridView内部使用 a,则GridControl可以使用AddNewRow()方法添加新行。

(myGridcontrol.MainView as DevExpress.XtraGrid.Views.Grid.GridView).AddNewRow();

Link to documentation

文档链接

EDIT: You can access your view in a different way, of course.

编辑:当然,您可以以不同的方式访问您的视图。

回答by Raz Mahato

You can use AddNewRow to add new row and SetRowCellValue to insert value to that row.

您可以使用 AddNewRow 添加新行并使用 SetRowCellValue 向该行插入值。

yourgridViewName.AddNewRow();
yourgridViewName.SetRowCellValue(rowhandle,columnName,value);
gridViewMappedFileds.UpdateCurrentRow();

Put yourgridName.RowCount-1 for rowhandle to insert the row at last.Put gridViewMappedFileds.Columns["ColumnName"] to give your columnname.

把 yourgridName.RowCount-1 作为 rowhandle 最后插入行。Put gridViewMappedFileds.Columns["ColumnName"] 给你的列名。