Turning datagrid values that dont match datasource to datatable - vb.net

I have a datagrid that gets an extra column with checkboxes added that is not in its source. I am trying to convert the grid that the user sees to a datatable when a save button is clicked. What I am trying to do is run it through a for loop and check if the rows checkbox is checked and the best solution seems to be converting it to a datatable and checking from there. Most of what I have seen to do the conversion is Dim dt As DataTable = DirectCast(DirectCast(dtgrd, DataGrid).DataSource, DataTable) which wont work for me since the checkbox values that I am checking for are not in the datasource. I am trying to avoid a million postbacks so I am not updating anything when the checkbox changes.

Related

Changes in DataTable not seen from another form

I have a DataTable in my DataSet which is not bound to any table in the SQL Database since I need it only inside 2 forms. When I update it in Form1, it works just fine, the table is populated and I can see the data inside a DataGridView (here is the code I use for updating it):
Dim newProcesRow As DataRow = Me.SampleDataSet.Tables("TableSample").NewRow()
newProcesRow("Col1") = val1.ToString()
newProcesRow("Col2") = val2.ToString()
newProcesRow("Col3") = val3.ToString()
Me.Sample_DataSet.Tables("TableSample").Rows.Add(newProcesRow)
After doing this, the DataGridView in Form1 shows the entire table with no problems. After the user clicks a button, Form2 is shown, where there is a DataGridView as well with the same DataSource as the grid in the first form, the unbound table from the DataSet. I also tryed putting a MessageBox in Form2 to return the number of rows from the DataTable and the result was 0.
Is there any way I could make Form2 see the rows in the DataTable without binding it to the database? (I don't want to do unnecessary changes to the DataBase...) Or are there any saves/changes/updates I am not doing to the DataTable?
If there is anything else I should mention in order to make it easier to find a solution, tell me and I will add. Thanks in advance for any piece of advice, have a nice day! (:

VB stretch DataGridViewImageColumn images programmatically

What I am doing
I am developing a VisualBasic application where the GUI is separated from the data manipulation (frontend and backend). This particular piece of code keeps track of the Serial Numbers already measured and displays them in the Form as a DGV with the serial number and an image or not.
My code
In a class that the GUI Form instantiates, my data is stored in a DataTable with a BindingSource, with the second column displaying an Image when I tell it to in the program (Nothing in the beginning):
Public SerialNumbersBindingSource As New BindingSource
Public SerialNumbersDataTable As New DataTable
(...)
SerialNumbersBindingSource.DataSource = SerialNumbersDataTable
SerialNumbersDataTable.Columns.Add("Serial", Type.GetType("System.String"))
SerialNumbersDataTable.Columns.Add("Pass", MyImage.GetType)
In my GUI Form code, a DataGridView has its DataSource set to the former DataTable:
DataGridViewSerialNumber.DataSource = MyObject.SerialNumbersDataTable
By just updating the DataTable in my class, the DataGridView updates automatically to reflect the current state of it. I do it like:
SerialNumbersDataTable.Add(CurrentSerial, MyImage)
The results I get
The code works, so I can modify and access the DataTable and the DataGridView autoupdates. But the images are not stretching, so I can only see a small part of them.
What I need
I need the second column named "Pass" in DataGridView to stretch the images.
What have I tried
If I access the column, it is treated like a DGVColumn and not a DGVImageColumn, so the Layout operation fails.
DataGridViewSerialNumber.Columns("Pass").DataGridViewImageCellLayout.Stretch
Microsoft's Docs page tells me to do this, which treats the columns like DGVImageColumn as I need "Pass" to. It fails because the first column is a Text one, not image.
For Each column As DataGridViewImageColumn In DataGridViewSerialNumber.Columns("Pass")
column.ImageLayout = DataGridViewImageCellLayout.Stretch
Next
Also I have tried creating a local DGVImageColumn, modify it and write it onto the original column, but it is read-only.
Dim imageColumn As DataGridViewImageColumn
imageColumn = DataGridViewSerialNumber.Columns("Pass")
imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch
DataGridViewSerialNumber.Columns("Pass") = imageColumn
I have also tried to do it from the designer. If I click the DGV, arrow to the right and 'Edit Column', I can create the two columns and setup Pass as ImageColumn with Stretched Layout. But when I set up DGVSerialNumbers.Datasource to my DataTable, it adds the DataTable's columns to the DGV's.
Failed DGV with columns added in designer
It's time for you to learn how to cast. If you want to access one column then don't use a loop. Simply access the column you want and then cast it as the type you want to use it as. You also need to actually assign the appropriate value to the appropriate property.
DirectCast(DataGridViewSerialNumber.Columns("Pass"), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch
If you want to break that up for clarity:
Dim imageColumn = DirectCast(DataGridViewSerialNumber.Columns("Pass"), DataGridViewImageColumn)
imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch
Also, be aware that Stretch will not retain the original aspect ratio, so you might want to use Zoom instead.

Datagridview Invisible Columns - accessing data

Having set up a datagridview, bound to a bindingsource, I have to use the cellformatting to paint the cells various colours.
Since you cannot access a field or record via bindingsource on a cellformatting event, the only way I could access the data, is through a column such as the primary ID for example. (this is only for cellformatting, i have no problem accessing the ID through the bindignsource.current object).
So, once I have finished designing and testing my datagridview, I then switch various columns (ID) for this example, to visible=false to make the column invisible.
I now cannot access the column data through cellformatting. I have read on a number of pages listed by google that since datagridview doesnt render the columns, the values are unobtainable aside from a couple of alternatives such as :
.TAG
and datakeynames
I cannot find datakeynames under datagridview for winforms for some reason.
.tag doesnt seem to do anything as far as I can tell.
Since trying to use this from the cell formatting, I am unable to use
Select Case .Columns(e.ColumnIndex).DataPropertyName.ToLower
or
If Datagridview1.columns(e.columnindex).datapropertyname.tolower='id' Then
as the column is invisible...
Shame I cannot access the bindingsource through cellformatting, but there must be a way and there must be others who have had this issue.
Use DataBoundItem:
Dim drw As DataRow = DirectCast(DataGridView1.Rows(i).DataBoundItem, DataRowView).Row
(Assuming you've bound to a DataTable)

Datagridview should not clear on datasource = nothing

I am loading my datagridview through databinding. After that I would want to allow the user to add more rows to the datagridview.
This is only possible if I make the datasource of the datagridview to nothing.
When I do that , the datagridview clears when I say rows.add command.
How can I add a new row without clearing the data?
If you are using a Data-Bound DataGridView control, you cannot just simply add new row by using the cell property of the control.
A DataGridView that is bound using a DataSet can be access only using the DataSet properties. Forcing it to use the cell property can cause an error "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."
Adding new row to a DataGridView control programmatically is useful when you want to pull some data from another table to a Bound DataSet with another table.
These links may help you :
http://social.msdn.microsoft.com/Forums/windows/en-US/c291d580-5a52-422a-b798-fbfb5f799b6a/cannot-add-new-rows-to-a-databound-datagridview-programmatically
http://www.codeproject.com/Questions/411452/Add-Rows-To-Databound-DatagridView

How to keep the data after removing the datasouce from a datagridview in vb.net?

I am populating a DataGridView (grid1) either by DataTable or DataSet.
I can remove a set of selected rows then add them to another unbound DataGridView (grid2).
The problem arises when I take a row from grid2 then add it to grid1.
Rows cannot be programmatically added to the DataGridView's rows
collection when the control is data-bound.
This occurs because
grid1.DataSource = myDataTable
or
grid1.DataSource = myDataSet
but when I do
grid1.DataSource = Nothing
all the rows from grid1 is removed.
Is there anyway to detach a datagridview from its datasource but keep the rows?
I can think of a solution wherein I would add another unbound DataGridView (grid3),
copy the content of the bound datagridview (grid1) to grid3 programmatically
then manipulate it from there.
I haven't tried this, but have a look at the Copy method:
grid1.DataSource = myDataTable.Copy
The MSDN says "Copies both the structure and data for this DataTable."
It may be that copying it also un-binds it.
If you have a databound grid, you should probably not be playing with the rows in the grid at all. Alter the table that you set as the datasource, then the grid will reflect the new rows.
dim row as datarow = myDataTable.newrow()
row.item("Column1") = "new value"
myDataTable.rows.add(row)
now the new row will show up on the databound grid. You can remove rows from the grid by removing it from the datasource datatable as well.