You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DevExpress GridView行内按钮点击事件中获取行属性实现编辑

DevExpress GridView编辑按钮点击获取行属性解决方案

WinForms平台场景

DevExpress WinForms中,数据展示通常由GridControl承载GridView,如果你的GriviewLV1是GridControl,需先获取对应的GridView实例才能操作行数据:

获取当前点击行(编辑按钮对应行)

编辑操作一般只需要当前点击的行,推荐两种方式:

  • 通过按钮控件获取所在行
    private void button_Edit_Click(object sender, EventArgs e)
    {
        SimpleButton editBtn = sender as SimpleButton;
        GridRow row = editBtn.GetOwnerRow() as GridRow;
        if (row == null) return;
    
        // 获取行绑定的数据源对象(替换为你的实体类型)
        var targetData = row.DataItem as YourEntityClass;
        // 或直接获取指定单元格的值
        GridView gridView = GriviewLV1.MainView as GridView;
        object cellValue = gridView.GetRowCellValue(row.RowHandle, "TargetColumnName");
        
        // 执行编辑逻辑
    }
    
  • 通过GridView获取聚焦行(若点击按钮时行处于选中状态)
    private void button_Edit_Click(object sender, EventArgs e)
    {
        GridView gridView = GriviewLV1.MainView as GridView;
        if (gridView.FocusedRowHandle == GridControl.InvalidRowHandle) return;
    
        // 获取行数据源对象
        var targetData = gridView.GetRow(gridView.FocusedRowHandle) as YourEntityClass;
        // 或获取单元格值
        object cellValue = gridView.GetRowCellValue(gridView.FocusedRowHandle, "TargetColumnName");
        
        // 执行编辑逻辑
    }
    

遍历所有行(若业务需要)

如果确实需要遍历全部行,可通过RowCount循环:

private void button_Edit_Click(object sender, EventArgs e)
{
    GridView gridView = GriviewLV1.MainView as GridView;
    for (int i = 0; i < gridView.RowCount; i++)
    {
        var rowData = gridView.GetRow(i) as YourEntityClass;
        // 处理每一行逻辑
    }
}

ASP.NET平台场景

若使用DevExpress ASPxGridView,可通过按钮的命名容器获取当前行:

protected void button_Edit_Click(object sender, EventArgs e)
{
    ASPxButton editBtn = sender as ASPxButton;
    GridViewDataItemTemplateContainer container = editBtn.NamingContainer as GridViewDataItemTemplateContainer;
    if (container == null) return;

    int rowIndex = container.VisibleIndex;
    // 获取行数据源对象
    var targetData = GriviewLV1.GetRow(rowIndex) as YourEntityClass;
    // 或获取单元格值
    object cellValue = GriviewLV1.GetRowCellValue(rowIndex, "TargetColumnName");
    
    // 执行编辑逻辑
}

若需要遍历所有行,直接使用Rows集合即可:

protected void button_Edit_Click(object sender, EventArgs e)
{
    foreach (GridViewDataRow row in GriviewLV1.Rows)
    {
        // 处理每一行逻辑
    }
}

内容的提问来源于stack exchange,提问作者Obed Bustos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 13:39:35