
Originally Posted by
wes4dbt
kevin,
In my project I've added a datasource, which is a typed dataset, with several datatables and tableadapters.
1. I added a dgv to my form
2. I selected the "Lots" datatable from under "other data sources" in the popup window as my datasource.
3. the designer added the "lotsbindingsource" and "LotsTableadapter" to my form and assigned the "LotsBindingsource" as my datasource.
The data displays fine. Just no color change when editing.
I'd like to get this to work, I think it's a cool concept.
Thanks
I whipped up a simple demo using NortWind MS-Access database.
http://kevininstructor.home.comcast....hangeWatch.zip
This example watchs the CompanyName column where the DataGridView Column name is CompanyNameDataGridViewTextBoxColumn.
AddHandler for VS2008
Code:
AddHandler BindingSource1.DataTable("Customer").ColumnChanging, _
New DataColumnChangeEventHandler(AddressOf Column_Changing)
AddHandler for VS2005
Code:
AddHandler BindingSourceExtensions.DataTable(BindingSource1, "Customer").ColumnChanging, _
New DataColumnChangeEventHandler(AddressOf Column_Changing)
Note we need to watch for the column CompanyNameDataGridViewTextBoxColumn and look at the field CompanyName.
Code:
Private Sub Column_Changing(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs)
If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("CompanyNameDataGridViewTextBoxColumn").Index Then
If e.Row("CompanyName").ToString <> e.ProposedValue.ToString Then
With DataGridView1.CurrentRow.Cells("CompanyNameDataGridViewTextBoxColumn").Style
.ForeColor = Color.White
.BackColor = Color.Red
End With
End If
End If
End Sub
Now if you want to do this via the designer you can too which adds another element to the mix, if the code above hits e.ProposedValue where it is empty we will throw an exception so in the designer code (which can apply to the code above) we check to see if e.ProposedValue has a value or not and act accordingly which means that the code above should be doing the same thing which you would need to change to check to see if the Proposed value is nothing/null.
Code:
Partial Class Database1DataSet
Partial Class CustomerDataTable
Private Sub CustomerDataTable_ColumnChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanging
If e.Column.ColumnName = Me.columnCompanyName.ColumnName Then
If e.ProposedValue IsNot Nothing Then
If e.Row("CompanyName").ToString <> e.ProposedValue.ToString Then
With Form1.DataGridView1.CurrentRow.Cells("CompanyNameDataGridViewTextBoxColumn").Style
.ForeColor = Color.White
.BackColor = Color.Red
End With
End If
Else
With Form1.DataGridView1.CurrentRow.Cells("CompanyNameDataGridViewTextBoxColumn").Style
.ForeColor = Color.White
.BackColor = Color.Black
End With
End If
End If
End Sub
End Class
End Class