c# - 在 RowCommand 中获取 GridView Cell 的值

标签 c# asp.net vb.net gridview

我需要从 RowCommand 事件中获取单元格的值,但该值不在 GridView 的 PrimaryKeyNames 参数中。

目前我有:

if (e.CommandName == "DeleteBanner")
        {
            GridViewRow row = gvCurrentPubBanner.SelectedRow;
            string BannerName = row.Cells[1].Text;

这不起作用(索引超出范围错误),我也试过:

    int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvCurrentBanners.Rows[index];

这也不起作用,因为 CommandArgument(横幅 ID)不是行 ID。

最佳答案

Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)

然后获取 key 或获取单元格并转换为数据控制字段。

Dim id As Guid = GridView1.DataKeys(row.RowIndex).Value

Dim email As String = CType(row.Cells(2), DataControlFieldCell).Text

注意:这只适用于 Boundfields。

关于c# - 在 RowCommand 中获取 GridView Cell 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10783962/

相关文章:

asp.net - Javascript 和 ASP.net 之间的区别

c# - 用户控件或网格中的元素列表

c# - 来自其基类的传递构造函数

c# - 从 DLL 动态加载 ASP.NET 页面

javascript - 将 W 减小到 X 和 Y 之间

.net - Entity Framework 4.1 - 非键列之间的关系

c# - IEnumerator IEnumerable vb 到 C#

c# - 如何通过Visual Studio安装项目降级程序?

asp.net - 将 ObjectDataSource 绑定(bind)到数据访问层中的现有方法

asp.net - 加密 web.config 的自定义部分