-
Sep 6th, 2011, 03:15 PM
#1
Thread Starter
Lively Member
[RESOLVED] adding colors in datagrid view
Hi,
I have a datagridview and there are several columns in it whose datatype is in date format. I want it to where if a person changes the date in a cell which is ALREADY colored, I want that color to change to white as soon as they click off of the cell.
I have something like:
Code:
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Me.DataGridView1.CurrentRow.Cells("DateDataGridView1").Style.BackColor = Color.White
End Sub
Last edited by c3p0; Sep 6th, 2011 at 04:20 PM.
-
Sep 6th, 2011, 08:17 PM
#2
Re: adding colors in datagrid view
What about putting the code in the "CellEndEdit" event,
Code:
If e.ColumnIndex = 1 Then
Me.LOTDataGridView.CurrentCell.Style.BackColor = Color.Aqua
End If
-
Sep 7th, 2011, 09:35 AM
#3
Thread Starter
Lively Member
Re: adding colors in datagrid view
Thanks for the reply. It works, but it doesn't work. It changes the color regardless of if the value is changed or not. So I ended up putting this in the cellvaluechanged event. But it still doesn't change the color.....hrmmm
-
Sep 7th, 2011, 10:28 AM
#4
Re: adding colors in datagrid view
If you are data bound say to a DataTable (or a DataTable in a DataSet) then monitor ColumnChanging event of the DataTable
In the example below a BindingSource DataSource is a DataTable in a DataSet which assigns an event to monitor ColumnChanging event. Highlighted in red is a language extension
Code:
AddHandler bsMaster.DataTable(0).ColumnChanging, _
New DataColumnChangeEventHandler( _
AddressOf Column_Changing)
Language extension
Code:
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function DataTable( _
ByVal sender As BindingSource, _
ByVal TableIndex As Int32) As DataTable
Return DirectCast(sender.DataSource, DataSet).Tables(TableIndex)
End Function
ColumnChanging event
If the column CompanyName changes the background color becomes red, did not makes sense to make the background color white as this is the current color.
Code:
Private Sub Column_Changing(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs)
If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("CompanyName").Index Then
If e.Row("CompanyName").ToString <> e.ProposedValue.ToString Then
DataGridView1.CurrentRow.Cells("CompanyName").Style.BackColor = Color.Red
End If
End If
End Sub
The background color will revert back to the normal background color upon executing AcceptChanges for the DataTable.
-
Sep 7th, 2011, 12:27 PM
#5
Re: adding colors in datagrid view
kevins code looks like a good solution, but just in case, this is what I do.
Code:
Dim saveCellValue As String = String.Empty
Private Sub LOTDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles LOTDataGridView.CellBeginEdit
Me.saveCellValue = Me.LOTDataGridView.CurrentCell.Value.ToString
End Sub
Private Sub LOTDataGridView_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles LOTDataGridView.CellEndEdit
If e.ColumnIndex = 1 Then
If Me.saveCellValue <> Me.LOTDataGridView.CurrentCell.Value.ToString Then
Me.LOTDataGridView.CurrentCell.Style.BackColor = Color.Aqua
End If
End If
End Sub
I think I'll try Kevins code and see how it works, it looks good.
-
Sep 7th, 2011, 09:07 PM
#6
Re: adding colors in datagrid view
kevin, Don't mean to hijack the thread but I could get your code to work,
Code:
Private Sub frmUsers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
'TODO: This line of code loads data into the 'WaterTablesDataSet.lots' table. You can move, or remove it, as needed.
Me.LotsTableAdapter.Fill(Me.WaterTablesDataSet.lots)
'TODO: This line of code loads data into the 'WaterTablesDataSet.GroupLotsForUsers' table. You can move, or remove it, as needed.
Me.GroupLotsForUsersTableAdapter.Fill(Me.WaterTablesDataSet.GroupLotsForUsers)
'TODO: This line of code loads data into the 'WaterTablesDataSet.users' table. You can move, or remove it, as needed.
Me.UsersTableAdapter.Fill(Me.WaterTablesDataSet.users)
LoadUserLookUp()
AddHandler Me.LotsBindingSource.DataTable(0).ColumnChanging, _
New DataColumnChangeEventHandler( _
AddressOf Column_Changing)
blnIsAddNew = False
strUserId = ""
Me.Show()
Me.ToolStripId.Focus()
End Sub
Private Sub Column_Changing(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs)
Debug.Print("at col chg")
If Me.DataGridView1.CurrentCell.ColumnIndex = Me.DataGridView1.Columns("LotId").Index Then
Debug.Print("inside index")
If e.Row("LotId").ToString <> e.ProposedValue.ToString Then
Debug.Print("inside chg")
Me.DataGridView1.CurrentRow.Cells("LotId").Style.BackColor = Color.Red
End If
End If
End Sub
In a Module I put this,
Code:
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function DataTable( _
ByVal sender As BindingSource, _
ByVal TableIndex As Int32) As DataTable
Return DirectCast(sender.DataSource, DataSet).Tables(TableIndex)
End Function
column_changing is never called when I edit the dgv cell.
Me.Lotsbindingsource is bound to a datatable in a dataset. I must be missing something.
BTW - This code throws an error in 2005 but works in 2010
Code:
System.Runtime.CompilerServices.Extension()
It says the Type is not defined
Last edited by wes4dbt; Sep 7th, 2011 at 09:15 PM.
-
Sep 8th, 2011, 06:38 AM
#7
Re: adding colors in datagrid view
Where is LotsBindingSource DataSource being assigned?
The extension method shown here will work like a normal function for VS2005 as extension methods are not supported below VS2008.
Code:
Public Function DataTable(ByVal sender As BindingSource, ByVal TableIndex As Int32) As DataTable
Return DirectCast(sender.DataSource, DataSet).Tables(TableIndex)
End Function
-
Sep 8th, 2011, 10:36 AM
#8
Re: adding colors in datagrid view
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
-
Sep 8th, 2011, 12:27 PM
#9
Re: adding colors in datagrid view
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
-
Sep 8th, 2011, 03:21 PM
#10
Re: adding colors in datagrid view
Kevin,
Thanks for everything, I have it working now. Still not sure what I was doing wrong.
I noticed that in your example app that the dgv datasource was "CustomersBindingSource" but in the addhandler you reference "BindingSource1" which you had added to the form. That confused me but that not unusual.
I did notice this, in my app both of these statements seem to have the same result,
Code:
AddHandler Me.BindingSource1.DataTable("Lots").ColumnChanging, _
New DataColumnChangeEventHandler( _
AddressOf Column_Changing)
and
Code:
AddHandler Me.LotsBindingSource.DataTable("Lots").ColumnChanging, _
New DataColumnChangeEventHandler( _
AddressOf Column_Changing)
but in your app only this one worked
Code:
AddHandler Me.BindingSource1.DataTable("Customer").ColumnChanging, New DataColumnChangeEventHandler(AddressOf Column_Changing)
Thanks for all your help, I'm sure I'll be using this code in the future.
-
Sep 8th, 2011, 04:30 PM
#11
Re: adding colors in datagrid view
Sorry about the confusion with the two BindingSource, this was the first time since working with Visual Studio (since VS2003) that I used this style of coding so I added BindingSource1 and the other BindingSource was generated which I did not expect.
-
Sep 8th, 2011, 05:43 PM
#12
Thread Starter
Lively Member
Re: adding colors in datagrid view
Hey I fixed it. Sorry...forgot to update thread to resolved. The issue I was having is that in the "CellValueChanged" event, before changing the color, I was trying to check if the cell was empty, but it didn't show it as empty in the "CellValueChanged" event. It showed the value of the cell AFTER the value was changed.
Anyways thanks for replies!
Tags for this Thread
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
|