1

I am using Dexexpress xtragrid column and have displayed all rows of my database.

I wanted to add a new column to the datagridview and for that i did the following:

Dim AddOperationsColumn As New GridColumn()
AddOperationsColumn.Name = "colOperations"
AddOperationsColumn.FieldName = "Operations"
AddOperationsColumn.Caption = "Operations"
Grid_view_patients.Columns.Insert(1, AddOperationsColumn)
AddOperationsColumn.Visible = True
AddOperationsColumn.VisibleIndex = TotalColumns + 1

Now i want to add two buttons to it Edit and Delete

Question: So how to add buttons to these fields.

I have already counted all the rows.

4

2 回答 2

1

使用存储库项

    RepositoryItemButtonEdit editButton;
    RepositoryItemButtonEdit deleteButton;

    private void InitRepositoryItems()
    {
        editButton = new RepositoryItemButtonEdit();
        deleteButton = new RepositoryItemButtonEdit();


        editButton.ButtonClick += editButton_ButtonClick;
        deleteButton.ButtonClick += deleteButton_ButtonClick;

        gridControl.RepositoryItems.Add(editButton);
        gridControl.RepositoryItems.Add(deleteButton);

    }

    void deleteButton_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
    {
        // do some delete on button click
    }

    void editButton_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
    {
       //do some edit on button click
    } 

    private void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
    {
        if (e.Column.Name == "colOperations")
            e.RepositoryItem = editButton;
    }

用于在相应的gridView1_CustomRowCellEdit列中分配 RepositoryItem(在我们的例子中是 colOperations 列)

于 2013-01-04T12:43:13.710 回答
1

解决方案 1

使用DataNavigator 非常简单。只需将 Grid 属性 UseEmbebedNavigator 设置为 true,它就会显示在网格的底部。

为了为按下的按钮提供自己的功能,您需要处理NavigatorBase.ButtonClick 事件

例如:

private void gridControl_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
    {
        if (e.Button.ButtonType == NavigatorButtonType.Remove)
        {
            //getting the selected object
            myObject selectedItem = dbGridView.GetRow(dbGridView.FocusedRowHandle) as myObejct;
            if (selectedItem == null) return;

            //now do something with that object
            .......

            e.Handled = true;
        }
    }

请注意,上面的代码覆盖了 DataNavigator 的默认行为

解决方案 2

另一种方法是就地编辑网格行。为此,您的数据源需要是 BindingList。您可以看到模板 DevExpress WinForms 项目。

于 2013-01-04T12:16:47.167 回答