How to make columns invisible and still be able to reference the data in the column…
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
'Why this works? Because the event is called after the data is bound to the grid... This ensures that the column has been databound and then it is hidden.
e.Row.Cells(1).Visible = False 'id_bank
e.Row.Cells(3).Visible = False 'id_status
End Sub
How
to adjust the header column alignment dynamically…
Protected Sub GridView1_RowCreated(ByVal
sender As Object,
ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowCreated
If
e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(5).HorizontalAlign =
HorizontalAlign.Left 'passage title
End If
End Sub
How to adjust the column widths dynamically…
<asp:GridView… Width="1400px"
GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word")
How to
display html in a cell…
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
e.Row.Cells(8).Text = Server.HtmlDecode(e.Row.Cells(8).Text)
End Sub
How to disable editing a bound field…
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
e.Row.Cells(4).Enabled = False
End Sub
1. Drop the gridview on your aspx page
2. Add a “ButtonField” and change the CommandName to cmdAssign
3. Assign it’s Command Argument like this….
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowIndex > 0 Then
Dim btnAssign As Button = DirectCast(e.Row.FindControl("cmdAssign"), Button)
If Not btnAssign Is Nothing Then
btnAssign.CommandArgument = e.Row.RowIndex.ToString
End If
End If
End Sub
4. Use the feature like this…
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Response.Write("id_passage=" & GridView1.Rows(e.CommandArgument).Cells(2).Text)
End Sub
The “Select” button is much more straight forward….
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Response.Redirect("PassageEdit.aspx?id_passage=" & GridView1.SelectedRow.Cells(2).Text & "&Passage=" & GridView1.SelectedRow.Cells(4).Text)
End Sub
How to reference some text in a particular cell…
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If IsDate(e.Row.Cells(4).Text) Then
e.Row.Cells(4).Text = CType(e.Row.Cells(4).Text, Date).ToShortDateString
End If
End If
End Sub
How to change the header….
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(0).Text = "Exam Name"
e.Row.Cells(1).Text = "First"
e.Row.Cells(2).Text = "Last"
e.Row.Cells(3).Text = "Exam Date"
End If
End Sub
Assign a CommandArgument…
1. Drop a GridView on your webform
2. Through the GridView’s Gui, add a template , then add a button to the template column. Name the button “cmdMyButton”
Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Response.Write(e.CommandName & " " & e.CommandArgument)
End Sub
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim b As Button
b = e.Row.Cells(0).FindControl("cmdMyButton")
b.CommandArgument = e.Row.RowIndex
b.CommandName = "MyButton"
End If
End Sub
Delete Confirm…
'For the example below,
the GridView's first cell, (cell 0), has 3 linkbutton's in it, 'Edit', 'Delete'
and 'Select'
'To reference
the 'Delete' linkbutton, we have to specify Controls(2). Control 0 is the 'Edit' linkbutton
'Control 1 is
a literal, and Control 2 is the 'Delete' linkbutton
If
e.Row.RowType = DataControlRowType.DataRow Then
'
reference the Delete LinkButton. This
would be the first column, (column '0'), and the third control in it.
Dim
lb As LinkButton = CType(e.Row.Cells(0).Controls(2),
LinkButton)
lb.OnClientClick = "javascript:return confirm('Are you certain you want
to delete this record') "
End If