-
Apr 27th, 2019, 02:23 AM
#1
Thread Starter
Lively Member
How do I change the BG color of a Single Cell & store the color scheme in Textfile?
Hello all,
This thread will be a 2 part question.
A) How do I change the BG color of a Single Cell on a Table?
B) How can I store the color scheme, so that the cell BG color will be the same when program is re-opened?
A)
1) User clicks a Cell on the table
2) Then clicks a button called "Color"
3) A color window pops up
4) User choose Color and clicks OK
5) Color window closes
6) The Cell BG color changes according to user's choice
Previously, I have this code that change the color of the whole row. Now I just want ONE cell.
I want to ask, how do you make the system know which Cell is selected?
Code:
Private Sub ProjectsDataGridView_CellFormatting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles ProjectsDataGridView.CellFormatting
If Me.ProjectsDataGridView.Columns(e.ColumnIndex).Name = "DataGridViewTextBoxColumnJobStatus" Then
If e.Value IsNot Nothing Then
If Not IsDBNull(e.Value) Then
If CType(e.Value, String) = "Confirmed" Then
ProjectsDataGridView.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.FromArgb(CInt(colorArray(0)))
ElseIf CType(e.Value, String) = "On Hold" Then
ProjectsDataGridView.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.FromArgb(CInt(colorArray(1)))
ElseIf CType(e.Value, String) = "Completed" Then
ProjectsDataGridView.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.FromArgb(CInt(colorArray(2)))
ElseIf CType(e.Value, String) = "Incomplete" Then
ProjectsDataGridView.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.FromArgb(CInt(colorArray(3)))
ElseIf CType(e.Value, String) = "Cancelled" Then
ProjectsDataGridView.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.FromArgb(CInt(colorArray(4)))
ElseIf CType(e.Value, String) = "Urgent" Then
ProjectsDataGridView.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.FromArgb(CInt(colorArray(5)))
End If
End If
End If
End If
End Sub
=================================================================
B) The BG color must be persistent, meaning it must be saved somewhere (I am considering Textfile)
The program will then read from textfile to populate the correct colors.
(I have a column with auto-generated number called ID)
TextFile
121,description=-65536
125,client=-1
This is the one that I am really unsure if it is doable. The issue is, user may add/delete/sort the table.
How will the system remember which Cell should hold the color if the table is modified.
I am considering using ID as X and Column Name as the Y, but I need advise.
-
Apr 27th, 2019, 06:14 AM
#2
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
To set one cell backcolor...
Code:
ProjectsDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).DefaultCellStyle.BackColor = Color.FromArgb(CInt(colorArray(5)))
To save a record, I'd use a text file, with one line per affected cell, i.e....
columnIndex,rowIndex,color.ToARGB
Then on Form_Load, read the text file line by line and change BackColor for any cells that should be changed
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 27th, 2019, 06:16 AM
#3
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
If the table is modified, you'll need to loop through your text file and edit any affected column or row indices
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 27th, 2019, 06:19 AM
#4
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Or, it might be easier, to just write your text file on Form_FormClosing, then you won't need to keep updating it when your user deletes rows or makes any other changes to the structure of the table
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 27th, 2019, 10:16 AM
#5
Thread Starter
Lively Member
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Originally Posted by .paul.
To set one cell backcolor...
Code:
ProjectsDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).DefaultCellStyle.BackColor = Color.FromArgb(CInt(colorArray(5)))
To save a record, I'd use a text file, with one line per affected cell, i.e....
columnIndex,rowIndex,color.ToARGB
Then on Form_Load, read the text file line by line and change BackColor for any cells that should be changed
But what is the code to check which cell is currently selected?
Like e.SelectedCell or something?
If user selects a cell and click “color” button.
-
Apr 27th, 2019, 10:24 AM
#6
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Just use the CurrentCell...
Code:
DataGridView1.CurrentCell.Style.BackColor = Color.Magenta
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 27th, 2019, 11:32 AM
#7
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Hi
here some ways to get the x y of the Cells, or like paul say's use CurrentCell
for the sample add the controls you see in the code
Code:
Public Class Form4
'https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.invalidatecell?view=netframework-4.8
'Force repaint when mouse enters Cell
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With DataGridView1
.ColumnCount = 5
.RowCount = 10
For i As Integer = 0 To .ColumnCount - 1
.Columns(i).HeaderText = "Column " & i.ToString
Next
For i As Integer = 0 To .Rows.Count - 1
For j As Integer = 0 To .ColumnCount - 1
.Rows(i).Cells(j).Value = "C " & i.ToString & "." & j.ToString
Next
Next
DataGridView1.Rows(2).Cells(3).Style.BackColor = Color.DarkOrange
.InvalidateCell(DataGridView1.CurrentCell)
End With
End Sub
Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
DataGridView1.InvalidateCell(DataGridView1.CurrentCell)
TextBox1.Text = DataGridView1.CurrentCellAddress.ToString & ": " & DataGridView1.CurrentCell.Style.BackColor.ToArgb
End Sub
Private Sub DataGridView1_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
DataGridView1.InvalidateCell(DataGridView1.CurrentCell)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'read Cell x y value
With DataGridView1
Dim s As String = .SelectedCells(0).RowIndex.ToString & ":" & _
.SelectedCells(0).ColumnIndex.ToString
MessageBox.Show(s)
End With
End Sub
End Class
hth
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Apr 27th, 2019, 12:15 PM
#8
Thread Starter
Lively Member
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Originally Posted by .paul.
Or, it might be easier, to just write your text file on Form_FormClosing, then you won't need to keep updating it when your user deletes rows or makes any other changes to the structure of the table
Hi Paul.
Can you explain more about how to use Form_FormClosing?
If let’s say user change color at row 2 column 3.
Then user add/delete/sort
Will the Cell color shift accordingly or stay as the NEW row 2 column 3?
-
Apr 27th, 2019, 12:18 PM
#9
Thread Starter
Lively Member
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Last edited by Volkof; Apr 27th, 2019 at 12:19 PM.
Reason: Double post
-
Apr 27th, 2019, 03:59 PM
#10
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
If you change a cell color, then wherever that cell moves to the backcolor will remain the same, so if it's column 0 row 2, and you delete row 1, the cell will now be column 0, row 1.
In the FormClosing event, loop through all cells in your dgv. If the color isn't standard, add a row to your textfile
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 27th, 2019, 10:37 PM
#11
Thread Starter
Lively Member
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Originally Posted by .paul.
If you change a cell color, then wherever that cell moves to the backcolor will remain the same, so if it's column 0 row 2, and you delete row 1, the cell will now be column 0, row 1.
In the FormClosing event, loop through all cells in your dgv. If the color isn't standard, add a row to your textfile
If the table is ever changing, how should I save the cell color in a text file?
User may close and reopen program.
-
Apr 28th, 2019, 01:31 AM
#12
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Originally Posted by Volkof
If the table is ever changing, how should I save the cell color in a text file?
User may close and reopen program.
As I said... Save colors on close, load colors on open.
That's why I said look at the FormClosing event for saving, and as I assumed you'd guess, the Load event for reloading. You just need nested loops to save and then loop through the text file on load, where you will have saved column index, row index, and color (integer)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 28th, 2019, 03:36 AM
#13
Thread Starter
Lively Member
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Ok, I can't figure out how to save the Row, Column, and Color Code.
Can someone please take a look?
rowIndex,columnIndex,color.ToARGB
Code:
Private Sub color_Btn_Click(sender As Object, e As EventArgs) Handles color_Btn.Click
Dim clrDialog As New ColorDialog
Dim ColorString As String
Dim rowIndex As Integer
Dim colIndex As Integer
If clrDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
ProjectsDataGridView.CurrentCell.Style.BackColor = clrDialog.Color
ColorString = clrDialog.Color.ToArgb.ToString 'Get Color Code
ProjectsDataGridView.CurrentRow = rowIndex 'Get Row Index
ProjectsDataGridView.Current??? = colIndex 'Get Col Index
End If
clrDialog.Dispose()
End Sub
-
Apr 28th, 2019, 03:45 AM
#14
Thread Starter
Lively Member
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Ok, I can't figure out how to save the Row, Column, and Color Code.
Can someone please take a look?
rowIndex,columnIndex,color.ToARGB
Code:
Private Sub color_Btn_Click(sender As Object, e As EventArgs) Handles color_Btn.Click
Dim clrDialog As New ColorDialog
Dim ColorString As String
Dim rowIndex As Integer
Dim colIndex As Integer
If clrDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
ProjectsDataGridView.CurrentCell.Style.BackColor = clrDialog.Color
ColorString = clrDialog.Color.ToArgb.ToString 'Get Color Code
rowIndex = ProjectsDataGridView.CurrentRow 'Get Row Index
colIndex = ProjectsDataGridView.CurrentCol 'Get Col Index
End If
clrDialog.Dispose()
End Sub
-
Apr 28th, 2019, 04:39 AM
#15
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
You have a Form which contains a DataGridView. What Form event would you expect to fire when your Form is closing??? Would it not make sense to loop through all of the rows of your DataGridView, and for each row loop through all of the cells in that row, then for each cell, check if the backcolor has changed, and if so, store that information in a text file???
And having a text file, wouldn't you wonder if there was a Form event that fires when the Form loads??? Then you could split each line in your text file and use it to restore the colors to your DataGridView...
Or you could just hang around here until someone writes every last word of it for you
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 28th, 2019, 05:23 AM
#16
Thread Starter
Lively Member
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Originally Posted by .paul.
You have a Form which contains a DataGridView. What Form event would you expect to fire when your Form is closing??? Would it not make sense to loop through all of the rows of your DataGridView, and for each row loop through all of the cells in that row, then for each cell, check if the backcolor has changed, and if so, store that information in a text file???
And having a text file, wouldn't you wonder if there was a Form event that fires when the Form loads??? Then you could split each line in your text file and use it to restore the colors to your DataGridView...
Or you could just hang around here until someone writes every last word of it for you
Sir, I understand the logic.
But my VB programming skill is not very good, I don't know the exact code to write.
which is why I am struggling.
So firstly, I am trying to figure out how to loop through the DataGridView
Can I get some help?
Code:
Private Sub color_Btn_Click(sender As Object, e As EventArgs) Handles color_Btn.Click
Dim clrDialog As New ColorDialog
Dim colorString As String
'Dim rowIndex As Integer
'Dim colIndex As Integer
If clrDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
ProjectsDataGridView.CurrentCell.Style.BackColor = clrDialog.Color
colorString = clrDialog.Color.ToArgb.ToString 'Get Color Code
'rowIndex = ProjectsDataGridView.CurrentRow 'Get Row Index
'colIndex = ProjectsDataGridView.CurrentCol 'Get Col Index
End If
clrDialog.Dispose()
'Loop through the DataGridView, find Cell where backcolor = changed, store rowIndex,columnIndex,color.ToARGB
"How should I code this?"
End Sub
Sorry if I'm slow...
Also, sorry for the update, but I need to save the rowIndex,columnIndex,color.ToARGB when user clicks the Color button, not when the Form Closes, cuz the same program is running on another computer, and the color will update there also. (This one I will figure it out myself, so it is ok)
-
Apr 28th, 2019, 06:20 AM
#17
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Originally Posted by Volkof
But my VB programming skill is not very good, I don't know the exact code to write.
We don't expect you to know already, but not knowing the exact code already doesn't mean you shouldn't try to work it out - based on the things you do know, the examples you have seen, and the information available (there are lots of tutorials and examples on the internet).
We are here to help you with things that are too hard to work out yourself, not to save you the effort of trying to do the things you could do... in this case you haven't even started writing a loop (which is something you shouldn't ask us for help with, thousand of tutorials cover loops, and part of post 7 above is useful), you've just asked us to write it all for you.
Being concerned that you can't do all of it doesn't mean you should avoid doing anything.
Do the bits you can, and put in some effort to find out how to do the other parts. If you get stuck then show us what you've come up with, and explain what you are stuck with.
-
Apr 28th, 2019, 09:58 AM
#18
Thread Starter
Lively Member
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
This is what I've got.
The bold part I could not figure out, how do you check if the Cell has Backcolor?
Code:
Private Sub color_Btn_Click(sender As Object, e As EventArgs) Handles color_Btn.Click
Dim clrDialog As New ColorDialog
Dim colorString As String
Dim rowIndex As Integer
Dim colIndex As Integer
If clrDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
ProjectsDataGridView.CurrentCell.Style.BackColor = clrDialog.Color
colorString = clrDialog.Color.ToArgb.ToString 'Get Color Code
End If
clrDialog.Dispose()
'Loop through the DataGridView, find Cell where backcolor = changed, store rowIndex,columnIndex,color.ToARGB
Dim i As Integer = 0 'Counter for Rows
Dim j As Integer = 0 'Counter for Columns
For i = 0 To ProjectsDataGridView.RowCount - 1
For j = 0 To ProjectsDataGridView.ColumnCount - 1
'Check if each cell has Backcolor
If ProjectsDataGridView.Rows(i).Cells(j).Style.BackColor = True Then
rowIndex = i 'Get Row Index
colIndex = j 'Get Col Index
End If
Next
Next
End Sub
-
Apr 28th, 2019, 12:16 PM
#19
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Hi
well you could check if something has changed but I would just write all the Data to a Text file
here a sample
Code:
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
SaveGrid(DataGridView1, "E:\Test\color.txt")
End Sub
Public Sub SaveGrid(ByVal Grid As DataGridView, ByVal FileFullName As String)
Dim Rows(Grid.RowCount - 1) As String
Dim Cols(Grid.ColumnCount - 1) As String
For i As Integer = 0 To Grid.RowCount - 1
For j As Integer = 0 To Grid.ColumnCount - 1
Cols(j) = Grid(j, i).Style.BackColor.ToArgb
Next
Rows(i) = Strings.Join(Cols, ";")
Next
System.IO.File.WriteAllText(FileFullName, Strings.Join(Rows, vbNewLine))
End Sub
my DGV as 5 rows and 5 columns
this is what my Test file looks like
the value 0 is white(Empty = no Color selected)
the other valus are Red and DarkOrange
Code:
-65536;0;0;0;0
0;0;0;0;0
0;0;0;-29696;0
0;0;0;-65536;0
0;0;0;0;0
Last edited by ChrisE; Apr 28th, 2019 at 12:25 PM.
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Apr 29th, 2019, 02:45 AM
#20
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Apr 29th, 2019, 02:56 AM
#21
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
@ChrisE... I wouldn't bother saving the colors after each change. Simpler to save on Form FormClosing, and reload on Form Load
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 29th, 2019, 05:31 AM
#22
Thread Starter
Lively Member
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
Originally Posted by ChrisE
Hi
well you could check if something has changed but I would just write all the Data to a Text file
here a sample
Code:
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
SaveGrid(DataGridView1, "E:\Test\color.txt")
End Sub
Public Sub SaveGrid(ByVal Grid As DataGridView, ByVal FileFullName As String)
Dim Rows(Grid.RowCount - 1) As String
Dim Cols(Grid.ColumnCount - 1) As String
For i As Integer = 0 To Grid.RowCount - 1
For j As Integer = 0 To Grid.ColumnCount - 1
Cols(j) = Grid(j, i).Style.BackColor.ToArgb
Next
Rows(i) = Strings.Join(Cols, ";")
Next
System.IO.File.WriteAllText(FileFullName, Strings.Join(Rows, vbNewLine))
End Sub
my DGV as 5 rows and 5 columns
this is what my Test file looks like
the value 0 is white(Empty = no Color selected)
the other valus are Red and DarkOrange
Code:
-65536;0;0;0;0
0;0;0;0;0
0;0;0;-29696;0
0;0;0;-65536;0
0;0;0;0;0
Hi ChrisE, the issue is that the user may ADD/DELETE/SORT the DataGridView, then the texfile won't tally anymore.
@paul, I can't use FormClosing because I have another computer running the same program and the changes need to be real-time (sort-of, I have an auto-refresh function).
So I am checking if Color is not WHITE
I am thinking of only saving the ProjectsDataGridView.row(ID).value, ColumnIndex, Color
Cuz if user ADD/DELETE/SORT, ID will stay the same.
Also I don't need to store for the entire DataGridView
So in this example, Row ID 122 has color changed
TextFile
122, 0, Red
122, 1, Red
.
.
.
Then whenever the program refresh, it will read from textfile, Loop the DataGridView and check Column 0 where Value = 122, set the Cell Color
That's the idea.
I may have to come back and ask more questions
-
Apr 29th, 2019, 11:12 AM
#23
Re: How do I change the BG color of a Single Cell & store the color scheme in Textfil
I think you are heading in the wrong directon with wanting to save the colors to a text file
or I got it wrong.
here a sample of coloring cells baseed on another cell (status changed in a row)
Code:
Dim tb As DataTable = New DataTable
Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tb.Columns.Add("ID")
tb.Columns.Add("QtyOut")
tb.Columns.Add("QtyIn")
tb.Columns.Add("Stock")
tb.Columns.Add("Balance")
tb.Rows.Add("1", "20", "230", "400", "Confirmed")
tb.Rows.Add("2", "6", "210", "300", "On Hold")
tb.Rows.Add("3", "5", "220", "25", "Incomplete")
tb.Rows.Add("4", "4", "126", "1233", "Urgent")
DataGridView1.DataSource = tb
End Sub
Private Sub ApplyFormatting()
For Each row As DataGridViewRow In DataGridView1.Rows
Select Case CStr(row.Cells(4).Value) 'based on Cell 4
Case Is = "Confirmed"
row.Cells(0).Style.BackColor = Color.Aquamarine
'or any other Cell = 0 1 2 or 3 in this case
Case Is = "On Hold"
row.Cells(2).Style.BackColor = Color.Pink
Case Is = "Incomplete"
row.Cells(3).Style.BackColor = Color.Bisque
Case Is = "Urgent"
row.Cells(2).Style.BackColor = Color.Red
End Select
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ApplyFormatting()
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
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
|