简体   繁体   中英

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.

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. It can be done in a normal datagridview Like this :

dataGridView1.Rows.Add();

I am working on WinForms in C#. I cant find an option in devexpress gridview to do this.

Thanks Rahul

To be able to add rows in your grid :

. Your gridDataSource property should be set (Like Fares wrote)

. Make your view editable

myView.OptionsBehavior.Editable = true;

. Set the position of the NewItemRowPosition of your view

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<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.

This can be done, you have to use a DataTable assign it as DataSource - have a look at this code snipped:

    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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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