Click Here for More GridView Snippets…

 

Sample Page of Select, Edit, Update & Delete…

 

To see another example, click here….

 

 

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 column widths dynamically…

1.       Ensure the Gridview  width style attribute set on the html side to the total of the cells widths…

<asp:GridView… Width="1400px"

2.       Add this code to the RowDataBound event for all of your comments…

a.          e.Row.Cells(0).Width = 100

b.          e.Row.Cells(1).Width = 500

c.          e.Row.Cells(2).Width = 100

d.          e.Row.Cells(3).Width = 100

e.          e.Row.Cells(4).Width = 200

f.          e.Row.Cells(5).Width = 200

g.          e.Row.Cells(6).Width = 100

h.          e.Row.Cells(7).Width = 100

 

3.       Add this code to the page_Load event….

GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word")

 

 

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

 

 

 

 

 

GridViewSample.jpg

 

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

 

More Sample how to’s…

 

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

 

Another How to…

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