How to refresh DataGrid? - c#

I am working on Silverlight 5 and RIA Services for database operations. DataGrid showing data perfectly.
But when I delete a record and submit changes to database, database updating successfully, but DataGrid showing still old data. It is not refreshing.
I search on google and surprise to see that there is no simple answer and I am also surprise to see that microsoft has not provided this basic functionality in Silverlight.

It is not a good way by anymeans, but I have gotten around this by setting the DataGrid to null and the re-adding the data.
System.Collections.IEnumerable temp = yourGrid.ItemsSource;
yourGrid.ItemsSource = null;
yourGrid.ItemsSource = temp;
I do not like doing it this way but it works and for my purposes I have not noticed any performance hits.
Edit: I guess this may not work with a delete though, but worth a try

Have you tried to use an PagedCollectionView as ItemSource?
Something like:
DataGridMainTopic.ItemsSource = new PagedCollectionView(m_context.EntitySet);
or even:
DataGridMainTopic.ItemsSource = new PagedCollectionView(DataGridMainTopic.ItemsSource);

Related

C# WPF Database Exercise How to update database column

Doing a learning exercise.
Trying to update the database date column for all entries to a new specific value or go through each entry and change date to a new specific value.
I tried using DataContext & Linq but it keeps telling me missing reference which its not so I have reverted to this.
var results = webDataSet.GreyList;
foreach (var elements in results)
{
elements.Date = 55;
}
webDataSet.AcceptChanges();
greyWebTableAdapter.Update(webDataSet.GreyList);
Even If i put Update in a try catch it says it is successful but it will never update the database.
Plus I'd like to thank the people who have nothing to say yet down vote my questions, its people like you who really bring the community together.
To anyone else who took advice to add .AcceptChanges(); by using this it sets the dataSets modified row value from true to false; so when you update the method looks for modified true row's however they are all false so nothing gets updated. Also the other factor to not seeing the database change is because Visual studio 2015 creates a copy of the database from your local folder and duplicates it in your bin folder, so looking for changes in your local folders database is a waste of time.

C# Refresh/Update databound control after executing SQL query

I have a databound datagridview for a simple application. I am using the built-in visual studio database, but am using SQL queries to manipulate the data as I prefer it.
The datagrid view is only databound because it is a requirement.
After executing any the datagridview does not update by itself, however even after much research and testing everything I could find, nothing seems to work. The data only updates after I restart the program.
This is what I currently have:
dgvEmployee.DataSource = null;
dgvEmployee.Rows.Clear();
this.employeeBindingSource.ResetBindings(true);
dgvEmployee.DataSource = this.employeeBindingSource;
I know it is not the fault of my queries since opening the database shows the added values. Thank you for your time. This is my first question on this site so please go easy on me ;)
Have you tried refreshing the main form UI right after? Something like this:
dgvEmployee.DataSource = null;
dgvEmployee.DataSource = this.employeeBindingSource;
this.Refresh();
this.Update();
I am using this code and it is working well, but not sure if it is a best practice:
List<MyEmployeeData> lstDataSource = query.ToList();
dgvEmployee.DataSource = new BindingListView<MyEmployeeData>(lstDataSource);
dgvEmployee.Refresh();
It uses BindingListView class available via NuGet.
In your code theese two lines
dgvEmployee.DataSource = null;
dgvEmployee.Rows.Clear();
are in my opinion not necessary.
But you are missing something like this:
this.employeeBindingSource.DataSource = tableWithNewData;

DataGridView: cannot add row or insert to table

I have a DataGridView. It uses a BindingSource, a DataTable in a DataSet, and a TableAdapter to add/change/delete data in a table. It worked OK, but stopped working when I added a field/column, and I can't figure out what I did or how fix it.
The user can add a new row at the bottom of the DataGridView, but when he goes to save, the row disappears and is not saved. In addition, if he tries to type a second new row, the first new row disappears.
Existing Rows can be changed and saved back to the database successfully.
I've been asked for code. OK, here is code. (I've eliminated some error checking done by scanning dtDep) The point that after the third line is executed, there are no rows in dtDep even though a new row had been entered into the DataGridView. If a row had been retrieved, it would be in dtDep and the database table updated by the last statement.
this.Validate();
bsBelkDep.EndEdit();
DataTable dtDep = dsBelk.Tables["belk_elig_dep"];
int n = belk_elig_depTableAdapter.Update(this.dsBelk.belk_elig_dep);
It was a problem with the DataGridView, but I don't know what. I started deleting and re-creating the various object, and after the I recreated the DataGridView, it worked OK. Which was a pain because I have to do significant reformatting, but at least it works.
This is a very old question and I have no way of knowing if it was the OP's original problem, but I had the exact same scenario and this is how I resolved it.
For background: I have a WinForms application built using datasets and an Access database. I migrated that to use Sqlite and anything but datasets. To avoid destroying the application completely, first I copied the strongly typed data tables out, tweaked them to account for changes in the schema and then used PetaPoco to perform the data operations. That worked fine for a single test conversion.
The trouble arose when I wanted to move on and convert all data tables - I wasn't happy manually writing the logic for converting to and from typed data rows and POCOs, so I fell back to writing old school T4 templates to generate typed DataTable, DataRow classes and the necessary remapping code.
Worked a treat - for editing or removing data. But new rows disappeared on "creation", the binding navigator count didn't increment, and of course, when saving, I didn't detect any rows with the RowState of DataRowState.Added. The grid at start up was subtly different - a blank value in all columns instead of a negative number in the ID column. In hindsight, that should have been a big clue.
On reverting the behaviour back to the manually extracted typed class the grid started working again so it was clearly an error in the new code.
End of background; tldr;
The cause of the issue, in my case, was that the my Id column didn't have the AutoIncrement property set. As soon as I configured that to be true (along with setting AutoIncrementSeed and AutoIncrementStep to -1, although neither are required) new rows started being correctly added to the table.

Using a BindingSource to link a DataSet to a DataGridView, but there's no data

This is my first time working with DataSets and BindingSources, so please be gentle on me.
As part of a more complicated reporting system (I've distilled it down to a basic incarnation, but it still won't run correctly), I'm trying to pull data from a database using a DataSet problematically (that is, not set up via the designer). Here is the code I have so far:
// pull the data set
var dsReportData = new Reports2.ReportTest2();
dsReportData.BeginInit();
dsReportData.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema;
// bind tha data set
BindingSource bsReportBinding = new BindingSource();
((ISupportInitialize)bsReportBinding).BeginInit();
bsReportBinding.DataMember = dsReportData.Tables[0].TableName;
bsReportBinding.DataSource = dsReportData;
bsReportBinding.ResetBindings(true);
// test this stuff
dgvTester.DataSource = bsReportBinding;
dsReportData.EndInit();
((ISupportInitialize)bsReportBinding).EndInit();
I based this on the code I saw in a .designer.cs file after setting up binding through the designer. dgvTester is just a DataGridView with the default properties created in the designer.
The ReportTest2 dataset has just one TableAdapter in it, added via designer.
Now, if I went to Data -> Preview Data in VS and previewed the ReportTest2.payments.Fill,GetData () object, it returns data just fine, same as if I ran the query I used to crate the TableAdapter in SQL Server Management Studio.
However, running the actual code results in the DataGridView getting the column names from the query result, but not the actual data. The debugger reveals that dsReportData.payments.Rows.Count == 0 (and that, yes, dsReportData.Tables[0] is payments).
I ultimately intend to use the BindingSource to provide data to a ReportViewer, but first things first is making sure there's no problems with retrieving the data before going onto debug the report.
Hopefully I'm missing something obvious here. I hope so...
Figured it out after some trial and error. This is the part I was missing:
var TableAdapter = new Reports2.ReportTest2TableAdapters.paymentsTableAdapter();
TableAdapter.Fill(dsReportData.payments);
I didn't see it in the code I was referencing because the designer snuck it into the .cs file instead of the .designer.cs file. This made the data appear.

CRM 2011 - Display all entities in option set

I just started working with Dynamics CRM 2011 and need to do some tasks but I simply don't know how. Really hope you can help me with this.
I need to set the values for a picklist(option set) through code behind, it should display all published entities in the solution everytime the form of my custom entity is loaded.
I've searched the web and all I found was the next piece of code, but I absolutely have no idea what to do with is (yeah, I'm newbie, didn't you notice):
RetrieveAllEntitiesRequest req = new RetrieveAllEntitiesRequest();
req.EntityFilters = EntityFilters.Privileges;
req.RetrieveAsIfPublished = true;
RetrieveAllEntitiesResponse resp = (RetrieveAllEntitiesResponse)service.Execute(req);
I tried to do the same with some JScript samples I found on the SDK but I still couldn't manage to insert the values into the picklist. Please give a hint or something. Thanks.
In that case, I think, you should create an entity and put a lookup on form instead.
I've never seen using a picklist for dynamic data, I use them only for static.
They're like global enums.
But, they say about addOption at MSDN Xrm.Page.ui Control methods web page.
Have you tried this one?

Categories