Changes in DataTable not seen from another form - vb.net

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! (:

Related

Open data bound form on specific record

I am struggling to find an answer on this;
I have Form1 which contains a databound DataGridView table from a dataSet from a SQL server. Upon double click event of the row, I need to open Form2 to that databound specific ID. Now I understand that I can get each of the columns and loop through all the textboxes and add them in, but these seems very cumbersome and open to mistakes when there could be a large number of individual textboxes.
Instead I just want to open that form to the specific selected record. Something similar to how you could do with MS access...
DoCmd.OpenForm "Form2", , , "ID = " & recordID
Thank you very much for your help #Jimi. I used your solution by creating a new tableAdpter and adding a parameter which is passed to the new form, works perfectly.

How do I edit data in a data table that is bound to a combo box without using a datagridview?

I am using a combo box that is bound to a data table by the primary key. By selecting a value from the dropdown I am able to populate several textboxes in a form. I can easily add a new record to the data table. Occasionally I need to edit an existing record but I cannot discover the "index" of the row corresponding to the selection in the combo box even though I can populate the textboxes. Are there any methods to use with "datarow" to determine what record the "Pointer" is pointing to? If so perhaps then I could reference that datarow and write new data to the datatable, then update using a dataadapter... Any help would be greatly appreciated.
You're trying to solve a problem that doesn't exist. You don't need to know the index of the row or the row for that matter. What you should be doing is:
Binding your DataTable to a BindingSource, which you add in the designer.
Binding your BindingSource to your ComboBox AND your TextBoxes.
Select records in the ComboBox and edit in the TextBoxes as you like.
When it's time to save, call EndEdit on the BindingSource to ensure any pending edit is committed to the DataTable.
Use the same data adapter to save the changes from your DataTable as you used to populate it in the first place.
The binding would look something like this:
myDataAdapter.Fill(myDataTable)
myBindingSource.DataSource = myDataTable
With myComboBox
.DisplayMember = "DisplayColumn"
.ValueMember = "PKColumn"
.DataSource = myBindingSource
End With
myTextBox.DataBindings.Add("Text", myBindingSource, "EditColumn")
The saving would look something like this:
myBindingSource.EndEdit()
myDataAdapter.Update(myDataTable)
At no point do you have to care what rows were edited and where they are in the DataTable because Update will just save all changes.

Vb.net Freezing column in 2 different datagridviews

Googling this gave me only references to the scroll bars, but that is not my issue.
I have a Tab control on my form, it has 2 tabs. On each tab there is a datagridview. The data is retrieved from my database and bound to the datagrid. The two separate tabs hold slightly different data, but with mostly the same columns. I want to freeze the first two columns and I have the code for it and it works fine, except on the initial loading of the form. When the form is loaded for the first time it access the same information that I would get if I clicked the button to import the data into the datagrid. Except when it hits the function to freeze the columns in the first tab the column is frozen, but on the second one it basically dumps all data that was in the datagrid. Which doesn't make sense because the data is still there it just isn't showing when I click on the tab.
Anyone have any idea why it would do this? and how to fix it?
Believe me I have walked through it like 6 times and had someone here look at it and it doesn't make sense. But ok I guess I can put the code in here not that it is going to do any good because the code that breaks is the second call to the frozen column.
Dim DT as New DataTable
Dim DT2 as New DataTable
DT = object.getDataTable 'this is where the sql is being pulled from
Me.DataGridView1.DataSource = DT
DT2 = object.getDataTable 'this is where the sql is being pulled from
Me.DataGridView2.DataSource = DT2
'Frozen function
IF Me.DataGridView1.RowCount > 0 Then
Me.DataGridView1.Columns("ThisColumn").Frozen = True
End If
IF Me.DataGridView2.RowCount > 0 Then
Me.DataGridView2.Columns("ThisColumn").Frozen = True
End If
that's it, there are other functions but they don't break it I have commented all of them out and the only one that breaks it is the Frozen function as defined above.

Turning datagrid values that dont match datasource to datatable

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.

Can I page through grouped data in DataGridView?

I have a simple VB.NET 2008 app that helps users to edit fields in the database. Simple enough as a first "real" project in .NET, right?
For one table, I am currently using a DataGridView so it can be edited straight up. However, instead of offering the user the entire table, I'd like to group the data by the 'CompanyNumber' column and use a navigator to page through. In other words, I'd like the DataGridView to show me all the lines related to one company, then click the "next" arrow to show the next company, etc.
(I know I could do this with Xceed DataGrid, but I'm using Windows Forms not WPF, and I'd really prefer to do this with "pure" ADO.NET for this project.)
Update 2009-09-28:
So I have created a ComboBox filled from the same BindingSource, and configured its SelectedIndexChanged to change the Filter value on the DataGridView.
But still, filling the ComboBox--which should be easy!--continues to be a problem. I can either:
(a) fill it from the BindingSource, in which case I see multiples of each 'CompanyNumber' and I can't figure out a way to show only distinct values, or
(b) create another TableAdapter in the data source which is just a "Select DISTINCT CompanyNumber..." query, which mostly works, except that that first value of the list changes when I change the selection (e.g. if the ComboBox shows "100, 101, 102, 103" and I pick "102", then the list will show as "102, 101, 102, 103").
Any recommendations?
(Also, bonus if you can suggest how to make the BindingNavigator's arrows page through the 'CompanyNumber' filters instead of the items in the DataGridView... which is what I'd really like to see.)
What you could do is just force the DataGridView to sort CompanyName, this way all rows with the same company name are next to each other and the user can navigate the data grids with the paging that comes with it.
Alternatly, you could follow through with your combobox/DropDownList idea, which would be best. From what I understand when you select an item in the combobox everything in it changes?
Another way is to create two separate buttons, "Previous" "Next", that when clicked, will change the DataGridView's binding source to only show a certain company. You would need to store an array of company names, then store what the current DataGridView's binding source is displaying.
I ended up figuring it out myself, and the solution is clean and simple. Here are the basic steps:
create a DataView off of the table out of the DataSet
use the DataView.ToTable() method to create a new table filtered to only distinct values from the needed column ('CompanyNumber')
create a BindingSource which uses the new DataTable as its DataSource
bind the ComboBox to the new BindingSource
bind the BindingNavigator to the new BindingSource
Because the ComboBox and the BindingNavigator use the same BindingSource, they will update each other with the changes automagically.
Here's the rough code:
Private Sub CoNumsComboxBox_LoadData()
Dim dvCoNums As DataView, dtCoNums As DataTable
dvCoNums = New DataView(Me.ODBCDataSet.Tables("CompanyFundProfile"))
dvCoNums.Sort = "CompanyNumber ASC, FundNumber ASC"
dtCoNums = dvCoNums.ToTable("CompanyFundProfile", True, "CompanyNumber")
CoNums_BindingSource.DataSource = dtCoNums
CoNumsComboBox.DataSource = CoNums_BindingSource
CoNumsComboBox.DisplayMember = "CompanyNumber"
CoNumsComboBox.ValueMember = "CompanyNumber"
'attach handler which changes DataGridView1's filter when this changes
AddHandler ToolStripComboBox1.SelectedIndexChanged, AddressOf CoNumsComboBox_SelectedIndexChanged
CompanyFundProfile_BindingNavigator.BindingSource = CoNums_BindingSource
End Sub