How to load only newly added record into datagridview? - vb.net

I am developing the windows application in VS 2010
I am binding the data to grid using generic collection.
Here is the code
Private sub LoadAllCustomer()
Dim oCustomerCollection as new Customers
oCustomers.LoadAll() //Loads all the collection of customers
gvCustomerGrid.DataSource = oCustomerCollection
End sub
Above code work fine...
I have details form which saved the data into customer table in DB. When I save the record , I just want to add new row into grid contains recently added record.
(Previously, I was using Devexpress's grid's RefeshDataSource property.
but now I am using Visual Studio's grid which don't have this property.)
Is there any way for it ? Or I have to load entire data into grid again to show recently added record ?

Related

GridView does not update DataTable

I am using a DevExpress XtraGrid/View for windows forms. The datasource of the grid is a binding source, which is connected to a dataset. My problem is, this table adapter does not update the datatable. I can insert new rows without problems, but I can't update. No error message is thrown; when I save changes, the row value just reverts. This happens for every column in the row. Here is my code for saving and then reloading data:
' Class variable
Private _invoiceDetailsAdapter As dsInvoiceDetailsTableAdapters.inv_InvoiceDetailsTableAdapter = New dsInvoiceDetailsTableAdapters.inv_InvoiceDetailsTableAdapter()
'Save Data
InvInvoiceLineBindingSource.EndEdit()
_invoiceDetailsAdapter.Update(DsInvoiceDetails.inv_InvoiceDetails)
'Load
DsInvoiceDetails.inv_InvoiceDetails.Clear()
If Me._invoiceId > 0 Then
_invoiceDetailsAdapter.Fill(DsInvoiceDetails.inv_InvoiceDetails, _invoiceId)
InvInvoiceLineBindingSource.Sort = "LineNum"
End If
I've figured out that it must be the dataset itself, because I've tried using a regular DataGridView with the dataset, to no avail. I generated the dataset through the wizard and had to add ColumnName and SourceColumn properties in the parameters for Insert & Update. The parameters for Insert & Update look identical as far as properties are concerned.
I've also tried creating new datasets, datatables, binding sources and tableadapters. I've even tried a DataAdapter but there was no difference. I have literally spent 2 weeks now looking through the properties and debugging, trying to find a cause.
Can someone please offer some advice?
The problem was DevExpress' XtraGrid functionality.
The XtraGrid does not immediately save an edit value to the linked dataset. The modified row is usually posted to the data object when focus is moved to another grid row. You can programmatically force the update by calling the UpdateCurrentRow method. In your case you should only call the CloseEditor and UpdateCurrentRow methods before updating the dataset via the DB adapter object.
So if a user only updates only one row, those changes go nowhere. Read on for a code snippet...
http://www.devexpress.com/Support/Center/Question/Details/A327

Updating a control on another form with the results of a dialog box

I made a windows form which contains a listbox (named VenueList). The listbox is pulling it's values from a list provided by a binding source VenuesBindingSource which is pulling in a ID for the value and a name for the text.
There is a button which is causing a DialogBox to appear which is asking for values to store in the database for a NEW venue. Once the values are filled and insert the database, what's supposed to happen is that the dialog box closes and goes back to the original form which invoked it.
However, instead of updating the list. The list stays the same. But if you close the form and reopen it, you see that a new value was added.
TournamentSettings.VenuesTableAdapter.InsertVenueQuery(Trim(VenueNameTxt.Text), Trim(VenueAddress1Txt.Text), Trim(VenueAddress2Txt.Text), Trim(VenueCityTxt.Text), Trim(VenueProvinceTxt.Text), Trim(VenueZipTxt.Text), Trim(CountryBox.SelectedValue), Trim(VenuePhoneNo.Text), VenueType.SelectedText, VenueWebAddress)
TournamentSettings.VenuesTableAdapter.Fill(TournamentSettings.VenueNameList.Venues)
In the above code, InsertVenueQuery is the name of a query from the designer which is invoked to add the values onto the tableadapter VenuesTableAdapter which is used to fill the combo box on load. I also sent the Fill command to refill the table with the new value.
So the question is, should I go about doing this another way, rather than feeding the Table adapter and sending a fill command on to the datatable? Or is there something that I'm not doing here which I should to force that value into the list. I'm tempted to redo everything outside of the designer but that's a lot of code since I have to essentially run two commands (one to insert the data, and another to get the ##IDENTITY value since this is run on an access database.)
Okay. This one I had to think about for a moment.
Instead of me creating a block of done on the load event, I instead created a sub function called "FillVenueList".
I used the following block of code:
Public Sub FillVenueList()
' Adding values from database to a datatable.
' From there will add to the list box.
Dim VenueConnection As New OleDb.OleDbConnection(DBconnection)
VenueConnection.Open()
Dim VenueConnectionQuery As New OleDb.OleDbCommand("SELECT VenueID, VenueName FROM Venues", VenueConnection)
Dim VenueDataAdapter As New OleDb.OleDbDataAdapter(VenueConnectionQuery)
Dim VenueDataSet As New DataSet
VenueDataAdapter.Fill(VenueDataSet, "Venues")
TrnVenueLst.DataSource = VenueDataSet.Tables("Venues")
TrnVenueLst.DisplayMember = "VenueName"
TrnVenueLst.ValueMember = "VenueID"
VenueConnection.Close()
End Sub
From there I called THIS sub on both the form AND the Add Venue window and I can safely see that this works. SO THAT is how you get a new value onto the form, don't use it as a part of the Load Event but rather call it from the load event block, then call it when you want to add to the list.

creating a drill through rdlc report in vb.net in Visual studio 2010

I have a parent and child report that I created in visual studio 2010 using winforms in vb.net. I have setup one text box in the parent report to go to the child report with the respective value as the paramter. However, when I click on the link I get the error:
a data source instance has not been supplied for the data source 'dataset1'
I realize that the dataset1 has to be filled, however I cannot figure out how to fill dataset1. I have researched the topic cand came with the following links:
http://www.developerfusion.com/t/vb.net/
http://msdn.microsoft.com/en-us/library/microsoft.reporting.winforms.reportviewer.drillthrough.aspx
On the winform where the reportviewer controller is located, I have the report drill through handler, but I do not understand how to set the dataset.
I have the following, and I am stuck on how to move on:
Private Sub ReportViewer1_Drillthrough(ByVal sender As System.Object, ByVal e As Microsoft.Reporting.WinForms.DrillthroughEventArgs) Handles ReportViewer1.Drillthrough
Dim localreport = e.Report
Me.Sp_get_testaccountsTableAdapter1.Fill(Me.Retreival.sp_get_testaccounts)
Dim od As New RetreivalTableAdapters.sp_get_testaccountsTableAdapter
End Sub
As you can see, I am filling the table adapter that dataset 1 is supposed to be connected to, but the dataset1 is still not filling... Please help!
Do I need to set a new sqlconnection?? Why do I need to do this? Why can I not just use the sqlconnection string that my tableadapter is using??
Thank you for your help!!
Based upon the example you provided (the second link), the code for accomplishing what you want to do is as follows:
void DemoDrillthroughEventHandler(object sender,
DrillthroughEventArgs e)
{
LocalReport localReport = (LocalReport)e.Report;
localReport.DataSources.Add(new ReportDataSource("Employees",
LoadEmployeesData()));
}
This shows how to assign a new DataSource based upon the drill-through functionality. Just follow the example on the website you gave and you will be fine.
As for why you need to do it, you have to load new data based upon the user drilling into your data.
Your question about filling the table adapter that the dataset is connected to seems a bit confusing. A dataset holds a set of tables. The DataSource for your report uses one table out of a dataset. Therefore, it looks like you might be trying to work backwards (filling a table to fill a dataset).

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

Problem with datagridview and AddNew

I've created a VB 2008 program to track work requests. It all works perfectly on a VISTA box, but I am having an issue with the program on an XP environment with adding new records.
Basically I've got 2 tabs: TAB 1 holds a datagridview with limited info and a calendar. Selecting dates on the calendar change the info in the datagridview. TAB 2 holds all the available info for that record in text/combo boxes. Both the datagridview and text boxes use the same Binding Source, so they are always in sync whenever the user selects a row from the datagridview. When you select the NEW button, TAB 2 appears with all the text boxes empty so the user can add data. If you look back on TAB 1, you see an empty, new row added to the datagridview (user can not directly add a row in the datagridview as AllowUserToAdd is set to false). If you let the app stay in the AddNew record state on VISTA, you remain on that new record until you select SAVE or CANCEL. On XP, however, after 1 minute time lapse, all the empty fields will eventually fill in with an existing record for that particular calendar day. When you look back on TAB 1, you no longer see the new empty row, you only see existing records previously saved.
Any ideas on how to resolve?? Thanks for any assistance.
Here is the code for adding new records:
Private Sub cmdNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNew.Click
'Focus on Work tab
TabControl1.SelectedTab = tabWork
'Change the files from read-only
bEditMode = True
ChangeEditMode()
'Clear the current information stored in the fields
Try
Me.BindingContext(WorkRequestBindingSource).AddNew()
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
'Hidden text boxes populate with current selected calendar
'Used to populate TimeIn and DateNeed because if never clicked on, will populate as NULL on save
dtpDateNeed.Text = txtDate.Text
dtpTimeIn.Text = txtTime.Text
End Sub
This is definitely an environmental issue. To solve the problem I would need to know which browsers you are using on each machine and some of the settings on each.
It sounds like the XP machine is refreshing the page after a timeout period and therefore munging the new record. I have seen that happen before and it stinks.
You might need to consider saving some more state information in the viewstate to catch that kind of thing.
If the code is exactly the same I wonder if it is an environment issue e.g. something like different international options or version of framework?