Posted by : Sudhir Chekuri Saturday, 5 October 2013

SQL queries to create a registration details table and inserting data to display in datagridview control

create database db_datagridview
use db_datagridview
create table tbl_register(sno int identity,sname varchar(20),gender varchar(20),hobbies varchar(30),qualification varchar(20))
insert into tbl_register values('sudhir','Male','Cricket,Chess','B-Tech')
insert into tbl_register values('sujatha','Female','Cricket,Carroms','M-Tech')
insert into tbl_register values('sita','Female','Chess','BCA')
insert into tbl_register values('ram','Male','Cricket','BSc')
select * from tbl_register 


VB.NET Code to fill datagridview and display selected row data in controls to edit and update

Imports System.Data.SqlClient
Imports System.Data

Public Class Form1
    Dim con As New SqlConnection("Data Source=SUDEER;Initial Catalog=db_datagridview;Integrated Security=True")
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        getdata()
    End Sub

    Public Sub getdata()


        Dim cmd As New SqlCommand("select * from tbl_register", con)
        Dim ds As New DataSet()
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)

    End Sub

    Public Sub ClearFields()
        Txtname.Text = ""
        RBMale.Checked = False
        RBFemale.Checked = False
        CBLHobbies.SetItemChecked(0, False)
        CBLHobbies.SetItemChecked(1, False)
        CBLHobbies.SetItemChecked(2, False)
        CBQualification.SelectedIndex = -1


    End Sub

    Private Sub DataGridView1_CurrentCellChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellChanged
        ClearFields()
        'Used to check wheather gridrow is selected or not
        ' if not exit from sub without filling the controls
        If DataGridView1.CurrentRow Is Nothing OrElse DataGridView1.CurrentRow.Index < 0 Then
            Exit Sub
        End If



        Dim s As String = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value
        'command to bring data where itemid matches with the selected cell row itemid
        Dim cmd As New SqlCommand("select [sno],[sname],[gender],[hobbies],[qualification] from tbl_register where sno=" + s + "", con)
        Dim ds As New DataSet()
        Dim da As New SqlDataAdapter(cmd)
        ds.Clear()
        da.Fill(ds)
        'for textbox
        Txtname.Text = ds.Tables(0).Rows(0)("sname").ToString()
        'for radiobuttons
        If (ds.Tables(0).Rows(0)("gender").ToString = "Male") Then
            RBMale.Checked = True
        Else
            RBFemale.Checked = True
        End If
        'for checkbox list
        Dim h As String = ds.Tables(0).Rows(0)("hobbies").ToString()

        Dim hobbies As String() = h.Split(New Char() {","c})
        Dim word As String
        For Each word In hobbies
            If (word = "Cricket") Then
                CBLHobbies.SetItemChecked(0, True)
            ElseIf (word = "Chess") Then
                CBLHobbies.SetItemChecked(1, True)
            ElseIf (word = "Carroms") Then
                CBLHobbies.SetItemChecked(2, True)
            End If

        Next
        'dropdown list
       
        Dim qual As String = ds.Tables(0).Rows(0)("qualification").ToString()
        Select Case qual
            Case "B-Tech"
                CBQualification.SelectedIndex = 0
            Case "M-Tech"
                CBQualification.SelectedIndex = 1
            Case "BCA"
                CBQualification.SelectedIndex = 2
            Case "BSc"
                CBQualification.SelectedIndex = 3
        End Select

    End Sub

End Class


Leave a Reply

Subscribe to Posts | Subscribe to Comments

Followers

Total Pageviews

Powered by Blogger.

Blog Archive

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -