Hi Steve,
>thank you. I've send you a couple of projects that repro the issue.
Thanks for your project. Please set breakpoints to debug in the following
method:
Private Sub GetColumns(ByVal gv As GridView)
Dim li As ListItem
If Not gv Is Nothing Then
Dim dcfc As DataControlFieldCollection
dcfc = gv.Columns
For Each dcf As DataControlField In dcfc
If dcf.Visible Then
li = New ListItem
li.Text = dcf.HeaderText
li.Value = dcf.SortExpression
Items.Add(li)
End If
Next
End If
End Sub
If you check the value of gv.Columns.Count, you probably will see it's 0.
This is because the auto-generated columns of GridView is not added in the
Columns collection of GridView. Only explicitly specified columns like
below are added into the collection. For example, the following code will
create two columns. However, GridView1.Columns.Count is 1.
<asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1">
<Columns><asp:TemplateField HeaderText="Header">
<ItemTemplate>
<%#Container.DataItem %>
</ItemTemplate></asp:TemplateField></Columns>
</asp:GridView>
Another issue is the following two lines:
li.Text = dcf.HeaderText
li.Value = dcf.SortExpression
If you want to show some text in the ListBox, the dcf.HeaderText should not
be empty string. However, if you don't specify the HeaderText for, say, in
above code, TemplateField, you'll get empty string here. It's common
because developers can use HeaderTemplate to customize the Header.Therefore
you cannot see anything shown in the ListBox. The same to
dcf.SortExpression. If it's not specified, you'll get empty string.
So if you want to do so you probably need to change your code logic. For
instance you can try this way to get controls in GridView:
GridView.Controls(0) ------ Table
GridView.Controls(0).Controls(index) ------ GridViewRow
GridView.Controls(0).Controls(index).Controls(index) ------ TableCell. For
BoundField, we can use TableCell.Text to get the value.
GridView.Controls(0).Controls(index).Controls(index).Controls(index) ------
Controls in the TableCell
Regards,
Allen Chen
Microsoft Online Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).