有什么方法可以使用 AutoGenerateColumns = true 操作 GridView 中的列? [英] Any way to manipulate the columns in GridView with AutoGenerateColumns = true?

查看:25
本文介绍了有什么方法可以使用 AutoGenerateColumns = true 操作 GridView 中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 AutoGenerateColumns = true,似乎无法操作 Gridview 的列.这是我的场景:

It seems like there's no way to manipulate the columns of a Gridview if AutoGenerateColumns = true. Here's my scenario:

我有一个通用的 GridView,它根据用户的选择显示各种不同的 LINQ 查询的结果.我喜欢 AutoGenerateColumns 的工作原理,而且我不必指定所有 BoundField、TemplateField 列等...

I've got a generic GridView that displays the results of various different LINQ queries depending upon what the user selects. I like the fact that the AutoGenerateColumns works like it should and I don't have to specify all the BoundField, TemplateField columns, etc...

除此之外,我还根据需要以编程方式添加其他列.以编程方式添加的列呈现在自动生成的列的左侧.如果我想将它们向右移动怎么办?

On top of that, I'm also programatically adding other columns as needed. The columns that are programatically added are rendered to the left of the autogenerated columns. What if I wanted to move them to the right?

GridView.Columns.Count 只计算那些编程的而不是自动生成的,所以我不能重新排列我想要的列.如有必要,我可以挂钩 RowDataBound 事件并隐藏"某些内容,但我无法重新排列.

GridView.Columns.Count only counts those that are programmed, not autogenerated, so I can't rearrange the columns I want around. I can hook the RowDataBound event and "hide" something if necessary, but I can't rearrange.

我是否只需要放弃 AutoGeneratedColumns=true,并为每个查询使用 BoundFields 布置它们?有什么我可以做的吗?

Do I just have to give up AutoGeneratedColumns=true, and lay them out with BoundFields for each query? Is there anything I can do?

推荐答案

你可以像这样操作数据绑定的东西:

You can manipulate things on data bound like this:

Private Sub MyGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Me.RowDataBound
  If Me.AutoGenerateColumns = True Then
    If e.Row.RowType = DataControlRowType.DataRow Then
          e.row.cells.add(some code here to add your special column)
    End If
    End If
End Sub

您必须创建自己的标题,但这是非常可行的.

You'd have to create your own header to but it's very doable.

这篇关于有什么方法可以使用 AutoGenerateColumns = true 操作 GridView 中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆