DataGridView Sample

 

 

GridView101.jpg

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            LoadGridView()

        End If

    End Sub

 

    Sub LoadGridView()

 

        Dim objConn As New SqlConnection("data source=aragorn;Integrated Security=SSPI;Database=smtscoring")

        Dim objCommand As New SqlCommand

        Dim objDa As New SqlDataAdapter

        Dim objDs As New DataSet

 

        objCommand.CommandText = "Select id_Client,ClientCode,ClientFullName From smt_Clients"

        objCommand.Connection = objConn

        objDa.SelectCommand = objCommand

        objDa.Fill(objDs, "Clients")

 

 

        GridView1.DataSource = objDs.Tables("Clients")

        GridView1.DataBind()

 

 

    End Sub

 

 

    Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

 

        Response.Write("the id_client value is: " & GridView1.Rows(e.CommandArgument).Cells(1).Text)

 

    End Sub

 

    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

 

            'Locate the Button in the first collumn...

            Dim b As Button = CType(e.Row.Cells(0).Controls(0), Button)

 

            'Set the Button's CommandArgument to the RowIndex

            b.CommandArgument = e.Row.RowIndex.ToString()

 

        End If

 

    End Sub