|
-
Jan 12th, 2010, 07:20 AM
#1
Thread Starter
PowerPoster
what gives here? datagridview,datatable problem
Hi Guys,
I am populating my datagridview like this:
Code:
PeopleTable = GetNewGameSheet(classid)
dgvgame.Columns.Clear()
Me.PeopleBindingSource.DataSource = PeopleTable
dgvgame.DataSource = Me.PeopleBindingSource
PeopleBindingNavigator.BindingSource = PeopleBindingSource
With Depositbtn
.HeaderText = "Deposit"
.UseColumnTextForButtonValue = True
.Text = "Deposit"
.Name = "Deposit"
End With
With Withdrawalbtn
.HeaderText = "Withdrawal"
.UseColumnTextForButtonValue = True
.Text = "Withdrawal"
.Name = "Withdrawal"
End With
With comboboxcolumn
.DataPropertyName = "jobid"
.DataSource = RetrieveAllJobs()
.ValueMember = "JobID"
.DisplayMember = "Description"
.HeaderText = "Job"
.DisplayIndex = 4
'.Name = "JobID"
'.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
'.ReadOnly = True
End With
With dgvgame
.Columns("PeopleId").Visible = False
.Columns("ClassId").Visible = False
.Columns("AccountId").Visible = False
.Columns("JobId").Visible = False
.Columns("surname").HeaderText = "Surname"
.Columns("firstname").HeaderText = "First Name"
.Columns("balance").HeaderText = "Balance"
.Columns("Day1").HeaderText = "Day 1"
.Columns("Day1").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Day2").HeaderText = "Day 2"
.Columns("Day2").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Day3").HeaderText = "Day 3"
.Columns("Day3").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Day4").HeaderText = "Day 4"
.Columns("Day4").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Day5").HeaderText = "Day 5"
.Columns("Day5").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns.Add(Depositbtn)
.Columns.Add(Withdrawalbtn)
.Columns.Add(comboboxcolumn)
End With
Code:
Private Function GetNewGameSheet(ByVal classid As Integer) As DataTable
Using con As New SqlConnection(dbpath)
Dim command As New SqlCommand("SELECT peopleid,surname,firstname,balance,accountid,classid,cast(6 as bigint) as jobid,CAST(0 as bit) Day1,CAST(0 as bit) Day2, CAST(0 as bit) Day3, CAST(0 as bit) Day4, CAST(0 as bit) Day5 FROM v_new_gamesheet WHERE (ClassId=@ClassId) ORDER BY surname", con)
command.Parameters.AddWithValue("@ClassId", classid)
PeopleAdapter.SelectCommand = command
PeopleTable = New DataTable
PeopleAdapter.AcceptChangesDuringFill = False
PeopleAdapter.Fill(PeopleTable)
PeopleTable.Columns.Add("Wage")
Return PeopleTable
End Using
End Function
then on the cellvaluechanged event of the datagridview I have thsi code:
what this does is, if a job is selected in the jobid combobox i get the wage attached to that job and for each checked column i add the wage up.
Code:
Private Sub dgvgame_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvgame.CellValueChanged
Dim balance As Decimal = 0
Dim wage As Decimal = 0
Using con As New SqlConnection(dbpath)
If (dgvgame.Columns(e.ColumnIndex).Name = "Day1") Or (dgvgame.Columns(e.ColumnIndex).Name = "Day2") Or (dgvgame.Columns(e.ColumnIndex).Name = "Day3") Or (dgvgame.Columns(e.ColumnIndex).Name = "Day4") Or (dgvgame.Columns(e.ColumnIndex).Name = "Day5") Then
If (e.RowIndex >= 0) Then
Dim jobID As Integer = CInt(If(IsDBNull(dgvgame.Rows(e.RowIndex).Cells("JobID").Value), 0, dgvgame.Rows(e.RowIndex).Cells("JobID").Value))
If (jobID > 0) Then
Dim wagecmd As New SqlCommand("SELECT Wage FROM Jobs WHERE JobID = @JobID;", con)
wagecmd.Parameters.AddWithValue("@JobID", jobID)
con.Open()
wage = CDec(wagecmd.ExecuteScalar())
con.Close()
End If
If (CBool(dgvgame.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = True) Then
dgvgame.Rows(e.RowIndex).Cells("Wage").Value = CDec(If(IsDBNull(dgvgame.Rows(e.RowIndex).Cells("Wage").Value), 0, dgvgame.Rows(e.RowIndex).Cells("Wage").Value)) + wage
Else
If (CDec(dgvgame.Rows(e.RowIndex).Cells("Wage").Value) > 0) Then
dgvgame.Rows(e.RowIndex).Cells("Wage").Value = CDec(dgvgame.Rows(e.RowIndex).Cells("Wage").Value) - wage
End If
End If
End If
End If
End Using
End Sub
now this all works fine when I run my save code which i am posting in the next thread the value in my datatable for the Day1 column etc is true if I check it
-
Jan 12th, 2010, 07:23 AM
#2
Thread Starter
PowerPoster
Re: what gives here? datagridview,datatable problem
Code:
Dim testforchanges As DataTable
Me.Validate()
'get new recs
testforchanges = PeopleTable.GetChanges(DataRowState.Added)
If testforchanges IsNot Nothing Then
Dim j As Integer = 0
Dim dt As New Date
dt = Me.MonthCalendar1.SelectionStart
Dim weeknumber As Integer = DatePart(DateInterval.WeekOfYear, dt)
Dim dayofweek As FirstDayOfWeek = FirstDayOfWeek.Monday
'get last date of week. Friday
Dim tdate As Date = dt.AddDays(5 - CInt(dt.DayOfWeek))
For j = 0 To testforchanges.Rows.Count - 1
Dim peopleid As Long = CLng(testforchanges.Rows(j).Item("PeopleId"))
Dim jobid As Long = CLng(If(IsDBNull(testforchanges.Rows(j).Item("JobID")), 0, testforchanges.Rows(j).Item("JobID")))
Dim accountid As Long = CLng(testforchanges.Rows(j).Item("accountid"))
Dim d1 = testforchanges.Rows(j).Item("Day1")
Dim d2 = testforchanges.Rows(j).Item("Day2")
Dim d3 = testforchanges.Rows(j).Item("Day3")
Dim d4 = testforchanges.Rows(j).Item("Day4")
Dim d5 = testforchanges.Rows(j).Item("Day5")
as i said above the values in the datatable for these columns are true if i check them.
then I have this code under a button that I check all the day columns:
Code:
Private Sub btnCheckAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckAll.Click
For Each row As DataGridViewRow In Me.dgvgame.Rows
row.Cells("Day1").Value = (row.Cells("JobID").Value IsNot DBNull.Value AndAlso row.Cells("JobID").Value IsNot Nothing)
row.Cells("Day2").Value = (row.Cells("JobID").Value IsNot DBNull.Value AndAlso row.Cells("JobID").Value IsNot Nothing)
row.Cells("Day3").Value = (row.Cells("JobID").Value IsNot DBNull.Value AndAlso row.Cells("JobID").Value IsNot Nothing)
row.Cells("Day4").Value = (row.Cells("JobID").Value IsNot DBNull.Value AndAlso row.Cells("JobID").Value IsNot Nothing)
row.Cells("Day5").Value = (row.Cells("JobID").Value IsNot DBNull.Value AndAlso row.Cells("JobID").Value IsNot Nothing)
Next
End Sub
it checks all the checkboxes fine but when I save, even if the checkbox is checked i get false returned.
Please tell me what i am doing wrong
-
Jan 12th, 2010, 08:40 AM
#3
Thread Starter
PowerPoster
Re: what gives here? datagridview,datatable problem
ok guys, ignore all my code. i know theres alot there. but this code specifically is the problem:
why is it when I tick each checkbox with the mouse and save the changes push through to my datatable but when I check them using the below code the changes don't push through to the datatable?
Code:
Private Sub btnCheckAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckAll.Click
For Each row As DataGridViewRow In Me.dgvgame.Rows
row.Cells("Day1").Value = (row.Cells("JobID").Value IsNot DBNull.Value AndAlso row.Cells("JobID").Value IsNot Nothing)
row.Cells("Day2").Value = (row.Cells("JobID").Value IsNot DBNull.Value AndAlso row.Cells("JobID").Value IsNot Nothing)
row.Cells("Day3").Value = (row.Cells("JobID").Value IsNot DBNull.Value AndAlso row.Cells("JobID").Value IsNot Nothing)
row.Cells("Day4").Value = (row.Cells("JobID").Value IsNot DBNull.Value AndAlso row.Cells("JobID").Value IsNot Nothing)
row.Cells("Day5").Value = (row.Cells("JobID").Value IsNot DBNull.Value AndAlso row.Cells("JobID").Value IsNot Nothing)
Next
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|