Click here to Skip to main content
15,030,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all
I want to write code to display data in gridview control, and select any record to get the selected row contents, editing data and update.
I have a gridview in my page, and my code is:

*********************************************************************
Imports System.Configuration.ConfigurationManager
Imports System.Data
Imports System.Data.SqlClient

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Public ds As New DataSet
    Public paramArragy As New ArrayList

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            'If Not Me.Page.IsPostBack Then
            LoadGrid()
            'End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub LoadGrid()
        GridView1.Columns.Clear()
        Dim col As New BoundField
        col.DataField = "Emp_no"
        col.HeaderText = "No."
        GridView1.Columns.Add(col)

        col = New BoundField
        col.DataField = "Emp_Name"
        col.HeaderText = "Name"
        GridView1.Columns.Add(col)

        col = New BoundField
        col.DataField = "DOB"
        col.HeaderText = "Date of Birth"
        GridView1.Columns.Add(col)

        col = New BoundField
        col.DataField = "Salary"
        col.HeaderText = "Salary"
        GridView1.Columns.Add(col)

        col = New BoundField
        col.DataField = "D_No"
        col.HeaderText = "Department No."
        GridView1.Columns.Add(col)

        Dim colf As CommandField = New CommandField
        colf.EditText = "Edit"
        colf.UpdateText = "Update"
        colf.CancelText = "Cancel"
        colf.ShowCancelButton = True
        colf.ShowEditButton = True
        colf.ShowSelectButton = True

        GridView1.Columns.Add(colf)

        Dim con As New SqlConnection(ConnectionStrings("Test").ToString)
        If con.State = ConnectionState.Closed Then con.Open()
        Dim com As New SqlCommand("select * from tb_emp", con)
        Dim da As New SqlDataAdapter(com)
        da.Fill(ds, "emp")

        ds.Tables("emp").PrimaryKey = New DataColumn() {ds.Tables("emp").Columns("emp_no")}
        GridView1.DataMember = "emp"
        GridView1.DataSource = ds
        GridView1.DataBind()

    End Sub
End Class
*********************************************************************

Now, how can I select, edit and update my data?
Posted
Updated 7-Nov-10 3:10am
v2

1 solution

hello;
check that Link[^] and you will get all what you need for that ctonrol...regards..
   
v2
Comments
fadi77_net 8-Nov-10 4:38am
   
Thank you tamerqatar
I am asking about GridView not DataGridView, I am using Web Application not Windows Application, and the link is for C#, but my code is in vb
thank you for your help anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900