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;
Related
I know that this question was asked before, but there is no relevant answer for it. The problem is that my rdlc Report shown in a reportViewer C# does not reflect the changes from the SQL Server database made at run-time (inserts, updates, deletes). It shows only the oldest data. If I close the program and run it again, it works but this is not the solution.
I found the solution . It seems that before the two lines of code written by me, had to be something like this:
String sql = "select * from TRANZACTIONS";
this.TRANZACTIONSTableAdapter.ClearBeforeFill = true;
this.TRANZACTIONSTableAdapter.Connection = new
SqlConnection(#"ConnectionString");
this.TRANZACTIONSTableAdapter.Connection.CreateCommand().CommandText = sql;
and after that calling the fill method and refresh the reportViewer.
I am not sure if it is the correct way to solve the problem, but it works fine for me.
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);
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.
Currently I'm writing an ASP.net webforms app, which works with a table containing 15 fields.
I'm using LINQ to SQL which is very good to use and everything, but when i need to bind the fields with the text boxes (15 of em), whats the best way currently I'm doing this
...
...
var stud = db.Students.SingleOrdefault(d => d.ApplicationNo == 32)
textbox1.Text = stud.Name;
...
...
I feel like I'm "missing the point". Please advise on the best way to do this.
Also what should i do to implement
the Move next and move previous
functionality like a recordset
i mean how can i move to the next/previous "student" in db.Students collection.
You can bind to an ASP.NET DetailsView control; this control supports displaying one record at a time, and can page through the results, so you don't have to do all that extra work. You could also consider a FormView too, but you have to control that.
I think you have the right approach; you could also use ElementAt() to retrieve by an index; that can have some performance implications though.
HTH.
You can use the Skip and Take method.
Update after comment:
You don't have to iterate through the different properties of the retrieved Student object. They're simply there. So you could use it like this:
txtName.Text = stud.Name;
txtAge.Text = stud.Age.ToString();
...
If you want to iterate to another record you can use the Skip and Take methods.
Grz, Kris.
I'm working on a Silverlight project trying to access a database using LINQ To DataSet and then sending data over to Silverlight via .ASMX web service.
I've defined my DataSet using the Server Explorer tool (dragging and dropping all the different tables that I'm interested in). The DataSet is able to access the server and database with no issues.
Below is code from one of my Web Methods:
public List<ClassSpecification> getSpecifications()
{
DataSet2TableAdapters.SpecificationTableAdapter Sta = new DataSet2TableAdapters.SpecificationTableAdapter();
return (from Spec in Sta.GetData().AsEnumerable()
select new ClassSpecification()
{
Specification = Spec.Field<String>("Specification"),
SpecificationType = Spec.Field<string>("SpecificationType"),
StatusChange = Spec.Field<DateTime>("StatusChange"),
Spec = Spec.Field<int>("Spec")
}).ToList<ClassSpecification>();
}
I created a "ClassSpecification" data class which is going to contain my data and it has all the table fields as properties.
My question is, is there a quicker way of doing the assignment than what is shown here? There are actually about 10 more fields, and I would imagine that since my DataSet knows my table definition, that I would have a quicker way of doing the assignment than going field by field. I tried just "select new ClassSpecification()).ToList
Any help would be greatly appreciated.
First, I'll recommend testing out different possibilities using LINQPad, which is free and awesome.
I can't quite remember what you can do from the table adapter, but you should be able to use the DataSet to get at the data you want, e.g.
string spec = myDataSet.MyTable.Rows[0] // or FindBy... or however you are choosing a row
.Specification;
So you might be able to do
foreach(var row in myDataSet.MyTable.Rows) {
string spec = row.Specification;
...
}
Or
return (from row in myDataSet.Specification
select new ClassSpecification()
{
Specification = row.Specification,
SpecificationType = row.SpecificationType,
StatusChange = row.StatusChange,
Spec = row.Spec,
}).ToList<ClassSpecification>();
Or even
return myDataSet.Specification.Cast<ClassSpecification>()
Not sure if the last one will work, but you can see that there are several ways to get what you want. Also, in my tests the row is strongly typed, so you shouldn't need to create a new class in which to put the data - you should just be able to use the existing "SpecificationRow" class. (In fact, I believe that this is the anemic domain model anti-pattern.)
So are you using a dataset for lack of a Linq provider to the database? That is the only reason I would consider this move. For instance, with Linq to Sql you can drag the table out and drop it. Then you have instant objects with the shape you want for it.