Reputation: 21
I'm working with a GridView in ASP.NET (ASPX) with VB.NET, where the data is loaded from a DataTable. Contains the fields: Title, Category, Status, ID and LinkAUX.
Dim artifactTable As New DataTable()
artifactTable.Columns.Add("Title")
artifactTable.Columns.Add("Category")
artifactTable.Columns.Add("Status")
artifactTable.Columns.Add("ID")
artifactTable.Columns.Add("LinkAUX")
artifactTable.Rows.Add(title, Category, hwStatusFinal, artifactId, artifactLink)
gridViewArtifacts.DataSource = artifactTable
gridViewArtifacts.DataBind()
In ASPX, i added a new column called Identifier, which displays a Hyperlink. The URL of this hyperlink is taken from the LinkAUX column, while its text corresponds to the value of the ID column.
<asp:GridView ID="gridViewArtifacts"
runat="server"
CssClass="grid-margin"
Style="margin-top: 4px; text-align: Justify; max-height: 200px;"
Width="100%"
BackColor="White"
BorderColor="#CCCCCC"
BorderStyle="None"
BorderWidth="1px"
CellPadding="3"
OnRowDataBound="gridViewArtifacts_RowDataBound"
OnRowCreated="gridViewArtifacts_RowCreated"
OnRowCommand="gridViewArtifacts_RowCommand">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="gridViewArtifacts_CheckedChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Identifier">
<ItemTemplate>
<asp:HyperLink ID="hyperLinkID" runat="server"
NavigateUrl='<%# Eval("LinkAux") %>'
Text='<%# Eval("ID") %>'
Target="_blank" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="Black" />
<HeaderStyle BackColor="#005AFF" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Justify" />
<RowStyle ForeColor="Black" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
In the interface want to show only the Identifier, Title, Category and Status columns, but currently the ID and LinkAUX columns are also visible. To hide them i'm trying to do it in the RowDataBound event, but the event doesn't seem to fire when the GridView is loaded.
Protected Sub gridViewArtifacts_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gridViewArtifacts.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim chk As CheckBox = CType(e.Row.FindControl("chkSelect"), CheckBox)
If chk IsNot Nothing Then
chk.Attributes.Add("onclick", "checkBoxClicked()")
End If
End If
If e.Row.RowType = DataControlRowType.DataRow Then
' Encontre o HyperLink na linha atual
Dim hyperLinkID As HyperLink = CType(e.Row.FindControl("hyperLinkID"), HyperLink)
If hyperLinkID IsNot Nothing Then
hyperLinkID.NavigateUrl = DataBinder.Eval(e.Row.DataItem, "LinkURL").ToString()
hyperLinkID.Text = DataBinder.Eval(e.Row.DataItem, "ID").ToString()
End If
End If
gridViewArtifacts.EnablePersistedSelection = True
End Sub
Has anyone experienced this situation or have suggestions for solving this problem? Thank you in advance for your help!
Upvotes: 0
Views: 31
Reputation: 49264
Ok, so you have a number of issues here.
First up, you have two ways of adding events to the GridView.
You can bring up the property sheet, and click on the particular event you want. This will wire up the event, but DOES NOT place the event name in the markup. (you see a "handles" in the code behind to wire up that event).
Hence, with the property sheet, you can double click in the event you want to create the code stub:
Or, you can while in markup have VS create the event for you. In THIS case, you WILL see the event name in markup:
Like this:
Do NOT do this both ways!!!!
So, in your sample code, you have "Handles" on the row data bound event, and that means by your messing around, you created and setup the event to run two times - you don't want that.
So, either use property sheet, and double click on the event you want to create the event. Or while editing in the markup, use the 2nd example above in which after you type in the event, and hit "=", then note how the editor displays a option to create the event.
So, above shows the two ways of creating the event, and using the property sheet approach to create that event means that the GridView will NOT show nor have the event name specifed in the markup. You have both, so either remove the event name you specified in the markup, or remove the "handles" from your code behind.
Next up:
The GridView when given a datasource will automatic generate all columns in the data source for you, and display those columns. However, if you want (such as your example) to have custom columns, then you need to disable the automatic genreation of columns, and thus ONLY your columns in the template will show.
Hence, you need to add AutoGenerateColumns = "false" in your markup (or you can use the proptery sheet - it will place such settings in the markup for you.
Hence, this:
Hence, with above, and this code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadData
End If
End Sub
Sub LoadData()
Dim artifactTable As New DataTable()
artifactTable.Columns.Add("Title")
artifactTable.Columns.Add("Category")
artifactTable.Columns.Add("Status")
artifactTable.Columns.Add("ID")
artifactTable.Columns.Add("LinkAUX")
For i = 1 To 8
artifactTable.Rows.Add("Title " & i,
"Category " & i,
"hwStatusFinal " & i,
i, "link " & i)
Next
gridViewArtifacts.DataSource = artifactTable
gridViewArtifacts.DataBind()
End Sub
Then the result is thus this:
So, this suggests that you don't really need to hide any columns, but set AutoGenerateColumns = "false"
Upvotes: 0
Reputation: 61
What you exactly meant by "row databound" event fired? In your case, when you put the debugger it will not hit during deugging? Also, your Row_databound event not having any code for "Hiding" the columns of ID and LinxUx. So in order to hide those include the below lines under the Row_databound event handler like below:
Protected Sub gridViewArtifacts_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gridViewArtifacts.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(5).Visible = False
End If
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(5).Visible = False
End If
If e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(6).Visible = False
End If
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(6).Visible = False
End If
End Sub
Hope this helps!
Upvotes: 0