Question How do I save changes to an unbound datagridview?

mayajewe1

New member
Joined
Jan 25, 2011
Messages
2
Programming Experience
1-3
Hi,

I fill my datagridview using the code shown but I don't know how to approach saving any changes or new rows that I make to the datagrid. I think I need to get hold of the changes somehow and then add these to a data adaptor and then use dataadaptor.update? Any help would be gratefully received. Here is the code:

VB.NET:
Private Sub frmLabMat_Load(.....)etc...

conn.Open()

Dim sqlString As String = "SELECT * FROM Timesheets WHERE JobID ='" & cbxJob.SelectedValue & "' AND (WorkDate BETWEEN @StartDate AND @EndDate)"

dgvLabour.DataSource = fillDataTable(sqlString, mcSelectDate.SelectionStart)
  
End sub 
 
 
Function fillDataTable(ByVal sqlString As String, ByVal StartDate As DateTime) As DataTable


        cmd = New SqlCeCommand(sqlString, conn)


        Dim EndDate As DateTime
        EndDate = StartDate.AddDays(6)

        cmd.Parameters.Add(New SqlCeParameter("@StartDate", StartDate))
        cmd.Parameters.Add(New SqlCeParameter("@EndDate", EndDate))

        dr = cmd.ExecuteReader()

        dt = New DataTable()
        dt.Load(dr)


        Return dt

End Function

Thank you :)

Note:
I am using VB 2008 Express Edition and .NET framework 3.5 SP1, and SQL server compact 3.5.
 
Also found another way to populate my datagrid...

I found another way to populate my datagridview, I don't know if is is any easier to save it when it has been populated using this method. (I have tried it this way as I might like to add comboboxes to some of the columns.)

VB.NET:
Private Sub tryToFillTheGrid()

        dgvLabour.Rows.Clear()

        Dim i As Integer = 0
        Dim sqlString As String
        Dim StartDate As String
        Dim EndDate As DateTime

        Try
            sqlString = "SELECT * FROM Timesheets WHERE JobID ='" & cbxJob.SelectedValue & "' AND (WorkDate BETWEEN @StartDate AND @EndDate)"
            StartDate = mcSelectDate.SelectionStart
            EndDate = mcSelectDate.SelectionStart.AddDays(6)


            cmd = New SqlCeCommand(sqlString, conn)
            cmd.Parameters.Add(New SqlCeParameter("@StartDate", StartDate))
            cmd.Parameters.Add(New SqlCeParameter("@EndDate", EndDate))

            dr = cmd.ExecuteReader

            '******* Add columns to the dgv *******
            dgvLabour.Columns.Add("EmployeeID", "Employee")
            dgvLabour.Columns.Add("JobID", "Job ID")
            dgvLabour.Columns.Add("HoursWorked", "Hours")
            dgvLabour.Columns.Add("Notes", "Notes")
            dgvLabour.Columns.Add("TimesheetID", "ID")
            dgvLabour.Columns.Add("PayScaleType", "Pay Type")
            dgvLabour.Columns.Add("WorkDate", "Date")
            '**************************************

            While dr.Read
                'MessageBox.Show("in dr.read")
                dgvLabour.Rows.Add()
                dgvLabour.Rows(i).Cells(0).Value = dr.GetString(dr.GetOrdinal("EmployeeID"))
                dgvLabour.Rows(i).Cells(1).Value = dr.GetString(dr.GetOrdinal("JobID"))
                dgvLabour.Rows(i).Cells(2).Value = dr.GetInt32(dr.GetOrdinal("HoursWorked"))
                dgvLabour.Rows(i).Cells(3).Value = dr.GetString(dr.GetOrdinal("Notes"))
                dgvLabour.Rows(i).Cells(4).Value = dr.GetInt32(dr.GetOrdinal("TimesheetID"))
                dgvLabour.Rows(i).Cells(5).Value = dr.GetString(dr.GetOrdinal("PayScaleType"))
                dgvLabour.Rows(i).Cells(6).Value = dr.GetDateTime(dr.GetOrdinal("WorkDate"))
                i = i + 1
            End While

            dr.Close()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        End Try
end sub
 
Back
Top