Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

asp.net - Any way to manipulate the columns in GridView with AutoGenerateColumns = true?

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...