[英]How to add rows in devexpress GridControl just like we add rows in a normal datagridview. (WinForms C#)
I wonder if there is an option to add rows in devexpress GridControl just like we add rows in a normal datagridview. 我想知道是否有一个选项可以在devexpress GridControl中添加行,就像我们在普通的datagridview中添加行一样。
I dont have a datatable or datasource at this point of adding rows. 此时我没有添加行的数据表或数据源。 When the application loads, I should be able to insert rows to the gridview manually.
当应用程序加载时,我应该能够手动将行插入gridview。 It can be done in a normal datagridview Like this :
它可以在普通的datagridview中完成,如下所示:
dataGridView1.Rows.Add();
I am working on WinForms in C#. 我在C#中使用WinForms。 I cant find an option in devexpress gridview to do this.
我无法在devexpress gridview中找到一个选项来执行此操作。
Thanks Rahul 谢谢Rahul
To be able to add rows in your grid : 为了能够在网格中添加行:
. 。 Your gridDataSource property should be set (Like Fares wrote)
你的gridDataSource属性应该设置(像Fares写的)
. 。 Make your view editable
使您的视图可编辑
myView.OptionsBehavior.Editable = true;
. 。 Set the position of the NewItemRowPosition of your view
设置视图的NewItemRowPosition的位置
myView.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
I don't get why you don't have a data source and what stops you from creating one. 我不明白为什么你没有数据源,什么阻止你创建一个数据源。
The most correct way to achieve what you want to do is to build a List
of objects that you want to show and then affect as the data source of the GridControl
实现您想要做的最正确的方法是构建一个您想要显示的对象
List
,然后将其作为GridControl
的数据源进行影响
List<MyClass> myClassList = new List<MyClass>();
// Here, you build your list as if you were to build your "rows"
// Finally, you can do this
gridControl1.DataSource = myClassList;
DevExpress creates the columns and fills the data. DevExpress创建列并填充数据。
This can be done, you have to use a DataTable assign it as DataSource - have a look at this code snipped: 这可以完成,你必须使用DataTable将其指定为DataSource - 看看这段代码剪切:
Dim dataTable As DataTable = New DataTable
dataTable.Columns.Add(Me.GridColumn1.FieldName)
dataTable.Columns.Add(Me.GridColumn2.FieldName)
dataTable.Columns.Add(Me.GridColumn3.FieldName)
dataTable.Columns.Add(Me.GridColumn4.FieldName)
For Each pic In collectionToShow
Dim row As DataRow = dataTable.NewRow()
row(Me.GridColumn1.FieldName) = pic.Name
row(Me.GridColumn2.FieldName) = pic.Town
row(Me.GridColumn3.FieldName) = pic.Alias
row(Me.GridColumn4.FieldName) = pic.Somevalue
dataTable.Rows.Add(row)
Next
gcPcoSelection.DataSource = dataTable
Don't forget do assign a FieldName in the GridColumns in DevExpress-Designer, otherwise the Fields will be empty. 不要忘记在DevExpress-Designer的GridColumns中指定FieldName,否则Fields将为空。 This is VB.net, but can be done 1:1 in C#, and btw, I don't understand how DevExpress could make simple tasks that hard.
这是VB.net,但可以在C#中以1:1完成,顺便说一下,我不明白DevExpress如何能够做出简单的任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.