How To Save DataGridView Cell Style to database - vb.net

I am working with a DataGridView in vb.net and I am trying to work on saving changes that are made in the DataGridView to my Access Database. The problem is, The user can change Individual cell background colors by selecting whatever cells they want and then changing the color. I can't figure out how I can save the individual cell background color so that when the program is run again, the colors show up. When the user clicks on save the color format gets erased. I also don't think I can do this on the Access side of it by adding a color column because there will be multiple colors used in each row and column. Is there a way to save cell style formatting? Here is code...
This is the save button
Private Sub DsfToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DsfToolStripMenuItem.Click
BaggersTableAdapter.Update(RentalsDataSet.Tables(0))
RentalsDataSet.Tables(0).AcceptChanges()
End Sub
And The button that is clicked to change the color of the selected cells.
Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
For i As Integer = 0 To DataGridView1.SelectedCells.Count - 1
'Dim a As String = (DataGridView1.SelectedCells(i).RowIndex.ToString)
Dim colIndex As Integer = DataGridView1.SelectedCells(i).ColumnIndex
Dim rowIndex As Integer = DataGridView1.SelectedCells(i).RowIndex
DataGridView1.Item(colIndex, rowIndex).Style.BackColor = Color.LightGreen
Next
End Sub

I would create tables for saving design colors. Something like
grid_style_header (header_id, grid_id, user_id)
grid_style_cells (id, header_id, row, column, color)...
The problems can happen if you allow filtering of the grid. If that happens, your columns will probably remain, but rows may not or they may move up or down.
better solution if you can implement it: grid_style_cells (id, header_id, row_guid (whatever primary key in your database id), column_name, color)
This makes the row identifier absolute so you don't have any problem with filtering. Column names also get rid of column reording problem.
I'm not aware of any in-built solution for this.

Related

How to Fill ComboBox with DataTable Column Names in VB.NET?

I need to fill a combobox on a form with the column names from an existing data table. I don't want to manually enter the column names in case they change in the future. I also want to keep my code tidy, so I don't want a standard loop. How can I fill a combobox with the column names most efficiently?
Thanks to examples from here, here, and here, I was able to piece together the following LINQ query and set it directly to the data source on the combobox when the form loads.
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
comboBox.DataSource = (From dc In dataTable.Columns
Select dc.ColumnName).ToArray()
End Sub

Get the actual value of a BoundField element in a gridview VB.NET

The following image depicts the full gridview I have:
I'm able to get the values of the second column (project_ID) in the back end as follows:
Dim val As String = grdProjects.SelectedRow.Cells(1).Text.ToString
For instance if I press the Select of the third row I'll get back the value 3.
My problem appears when the gridview is updated and has less records as the one in the following image.
If I press the second record I get the value 2 instead of the value 4 that I'd like to get.
Any suggestions please on how to get the actual value of the cell instead of the number of the selected row?
I might be wrong but in VB.net datagrids SelectedRow does not exist, SelectedRows do exist.
Anyway, try this way and let me know if it work:
On the RowEnter or CellClick event use the E.RowIndex to get what you want. Do as follow:
Private Sub DataGrid1_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGrid1.RowEnter
Dim CellValue As String = DataGrid1.Rows(e.RowIndex).Cells("Project_ID").Value.ToString
End Sub
Notice that for the .cells() I use the name of the column, this is better because if in the future you add columns this code will not need to be changed. Easier code maintenance always pay! Also. Cell().Value instead Cell().Text
Nandostyle

DataGridView check all Rows vb.net

Private Sub comboBoxStudentID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBoxStudentID.SelectedIndexChanged
For Each dr As DataGridViewRow In Me.dataGridViewStudent.Rows
If dr.Cells(0).Value.ToString.Contains(comboBoxStudentID.Text) Then dr.Visible = True Else dr.Visible = False
Next
End Sub
I've created this method to check and display a row that contains the same Student ID as selected from the comboBoxStudentID the problem is it only checks the first row of the DataGridView. How can I make it check all of the rows if there is match of Student ID?
Without knowing how your data is organized, you could do this by binding the DataGridView and the ComboBox to the same data source. Since this is not specified and looking at the snippet of code (which appears to work) it appears to loop through the rows and if the currently selected combo box text is contained in the first column cell, then make that row visible. Otherwise make the row invisible.
The posted code appears to work however there is one scenario you may want to consider. If the combo box contains the values that are in column one of the DataGridView, THEN, when the user selects a value in the combo box, the DataGridView will display ONLY those values that match the current combo box selection. This appears to be what you are trying to do.
The problem is how do you re-display all the rows?
Once the combo box selection has changed to some value, the user can delete the selection but all the rows will not display unless you handle another event. Also this strategy is not very user intuitive. One possible solution (below) simply adds a top item of “Reset” to the top of the combo boxes items. Then when the user selects “Reset” all row will become visible. Hope this helps.
To reiterate: you should consider setting this up with a DataSource and possibly a BindingSource where this is done for you automatically. Just a thought.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex > 0 Then
For Each dr As DataGridViewRow In DataGridView1.Rows
If (Not dr.IsNewRow) Then
If dr.Cells(0).Value.ToString.Contains(ComboBox1.Text) Then
dr.Visible = True
Else
dr.Visible = False
End If
End If
Next
Else
For Each row As DataGridViewRow In DataGridView1.Rows
row.Visible = True
Next
End If
End Sub

All elements of an array cannot be accessed from a for loop in monthCalendar Control - Visual Basic

I have a bit of an issue here with the monthCalendar Control in Visual Basic. What is happening in the program is I have a calendar and a text box. The user selects a date on the calendar, then types in information into the textbox component, then presses the add button. From there the add button sets up a parallel array that contains the selected date and the added string from the text box as a way to add planner information for each date. Then the date is bolded for each time an addition like this has happened.
From there you are suppose to be able to click each bolded date and view the information you added. The issue is this only works on the last added date. For some reason it will only read the last element added to the array. I have the for loop down below. I do not understand why I cannot select any bolded date and view the text added. I have option strict on so I know no data loss is occurring. Also I know the date is being selected and placed into the string each time a date is selected on the calendar because I am displaying it onto a label each time. What is the problem. Is it some logical error I am not catching or is it something else?
Public Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonthCalendar1.DateChanged
For index As Integer = 0 To intSizeOfSelectedDatesAry - 1
strSelectedDay = CStr(MonthCalendar1.SelectionRange.Start) ' place the selected date into a string
lblError.Text = strSelectedDay
lblError.Visible = True
Dim strAryDate As String = arySelectedDates(index)
If (arySelectedDates(index) = strSelectedDay) Then
lblInstructions.Text = strSelectedDay
lblSelectedDate.Text = aryTextForDates(index)
index = intSizeOfSelectedDatesAry - 1
End If
Next
End Sub

While in edit mode, advance to next cell and stay in edit mode for VB.NET DataGridView?

I have a DataGridView in which one column has data that the user needs to align by adding spaces. For example, the first two rows might contain:
kumbu
kuimbiu
And the user needs to be able to line up the letters that match by adding spaces. Something like this:
ku mb u
kuimbiu
Now in order to do that with the DataGridView, the user must enter edit mode in the top cell, add spaces, hit enter, re-enter edit mode in the bottom cell, and then add spaces. Our users would like to be able to, while in edit mode in the top cell, hit the down arrow and advance to the second cell while staying in edit mode, saving clicks or F2 hits.
Is there a good way to do this? I have tried trapping the down arrow key press, leaving edit mode, advancing a cell, and then entering edit mode with the grid's BeginEdit method, but this does not do what I want.
Any ideas?
When leaving the cell capture the edit status in a class variable. When the user presses down or enter, the next cell will begin edit mode, but only if the previous cell was in edit mode. You can add additional logic if you want it to be based upon columns.
Private blnEditMode As Boolean = False
Private Sub dgv_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEnter
If blnEditMode Then
dgv.BeginEdit(False)
End If
End Sub
Private Sub dgv_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellLeave
blnEditMode = dgv.IsCurrentCellInEditMode
End Sub