I have a problem with binding Listview to my database using Entity Framwork.
This code only shows the first row of the table but the records do not show:
var item = (from p in db.tbl_film
select p).FirstOrDefault();
string[] items = {item.flm_id.ToString(),item.flm_name,item.flm_description,item.flm_category };
foreach (var itemlist in items)
{
ListViewItem lvi = new ListViewItem(items);
listView1.Items.Add(lvi);
}
I have a table that has several records. Now I want to show it in the ListView.
Table: flm_film
Fields: flm_id, flm_name, flm_category
I want to see data with Entity Framework in ListView in details mode.
Consider this code :
var items = (from p in db.tbl_film select p).ToList();
foreach (var item in items)
{
// Create your ListViewItem here
// Then add it to your listView here.
}
If you want to retrieve multiple records, you shouldn't have to use FirstOrDefault() because, as it says, you get only the first record.
Related
I have a question regarding use of "Tag" :
I have a ListBox, or ListView, in which I have the name of my objects, I addes a "Tag" property to find its corresponding object :
foreach(Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
ListViewItem item = new ListViewItem(op_ass.Nom);
item.Tag = op_ass;
listBoxAss1.Items.Add(op_ass.Nom);
}
Now what I would like, is when I select an item in my list(or several), make an action on corresponding objects. But how can I find them back?
For example I want to remove selected objects from a List, or get the list of Operation ID (without displaying ID in my list).
Looks like you are adding the property, op_ass.Nom into the listbox instead of the ListViewItem, item. Modify your code as follows:
foreach (Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
ListViewItem item = new ListViewItem(op_ass.Nom);
item.Tag = op_ass;
// Add the list view item instead of op_ass.Nom
listBoxAss1.Items.Add(item);
}
Now you should be able to retrieve the tag from selected item/items as follows:
var operation = ((listBox1.SelectedItem as ListViewItem).Tag) as Operation;
Alternatively, you could think of using data binding as follows:
foreach (Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
}
listBoxAss1.DataSource = ListOpAss1;
listBoxAss1.DisplayMember = "Nom";
And access the data bound object as follows:
var operation = listBox1.SelectedItem as Operation;
using foreach is kind of deprecated you can look into implemented functions in list of objects
ListOpAss1.ForEach(x=>
{
x.getNom(Properties.Settings.Default.Langue);
var item = new ListViewItem(x.Nom);
item.Tag = x;
listBoxAss1.Items.Add(x.Nom);
});
in order to select an item in a list you can use SingleOrDefalt() or Skip(count) take (count) for multiple files or you can run native querys with conditions to search the list like this
var items = collection.Where(x=> x.City == "Burgas").ToList(); //You can use select if you want only certain properties of the object to be selected
///then you can use that new item list to remove the objects from the collection list like this
items.ForEach(x=>
{
collection.Remove(x);
});
I have a project that I am using to learn about EF6. I started the project and setup a database first model. There are a couple of existing tables and views. The result includes "classes" that represent the table / view db structures. My eventual goal is to populate Excel workbook sheets with the results.
I can access and sort, etc, the DbSet data that is returned a la:
using (var db = new TestDbEntities()) {
var list = db.TestTable1s
.OrderBy(m => m.machine.ToUpper())
.ThenBy(m => m.netid.ToUpper())
.ToList<TestTable1>();
var columnNames =
(from t in typeof(TestTable1).GetProperties()
select t.Name)
.ToList<string>();
}
However, I am stuck when it comes to getting the column data values. For example, if I add a loop at the bottom of the using above:
var row = 1;
foreach (var line in list) {
var col = 1;
if (row == 1) {
//
// print table headers
foreach (var item in columnNames) {
cells[row, col++].Value2(item);
}
col = 1;
}
//
// print table "body"
foreach (var item in line) {
cells[row, col++].Value2(item);
}
row++;
}
In the "body" loop item is an instance of type TestTable1. I am told by visual studio that TestTable1 is not enumerable.
So my question is, how do I access the members of class TestTable1 in a list-like way? Should I just access each known member as item.memberName?
The first list created in your code is the collection of the entities retrieved from the db. And you can loop thru it to create table like structure in the ui.
In fact you can bind this collection to datagridview or datagrid to show the data in tabular format. You just need to know how to use those controls.
If columns in your table are not going to change very frequently then this is the approach you should take.
I have a group item. Then, each group in group item, i put it into a listview
var Groups = query.GroupBy(query => query.Name);
foreach (var group in Groups)
{
if (group.records.Count() > 2)
{
ListView listview = new ListView();
var itemsource = new ObservableCollection<FileProperties>();
var header = "";
foreach (var item in group.records)
{
header = item.Name;
itemsource.Add(new FileProperties(item.Name, item.Size, item.DateModified, item.Hash, item.Path, item.IsOrigin));
}
listview.ItemsSource = itemsource;
var itemsTemplate = (DataTemplate)this.Resources["Show"];
listview.ItemTemplate = itemsTemplate;
//test is mother listview
test.Items.Add(listview);
}
}
Now, i have a question, how can i update listview UI if i change value in group items without reset mother listview
The default ListView can be grouped by using CollectionViewSource. There is no need to create a child ListView for each group of the parent ListView.
My answer here shows how to create a grouped ListView, you may take a look. Or there are a bunch of demos on internet, you can googling them.
But the most important point here is that by using the default grouped ListView, we can simply create one data collection for the whole ListView, modify the items source collection to update the grouped children automatically, we don't need to create ObservableCollections for each group any more.
I am new to using datagridviews and wanted to know how to populate certain fields.
I have created a form and added a datagridview (named GridSellProducts) with Visual Studio designer and added 8 columns with the first named Item.
I have also changed the column type for Item to DataGridViewComboBoxColumn also in design view.
I have the following data (product names) that I want to populate the combobox with for each row that may be added:
// get products
productsURL = "https://eko-app.com/Products/list_products/sessionId:" + sessionID + ".json";
var products = Products.GetProducts(productsURL);
List<string> productNames = new List<string>(); <-----the data to add to the combobox
foreach (var p in products)
{
var x = p.Product;
foreach (var pn in x)
{
productNames.Add(pn.name);
}
}
How do I add the above data to the column combobox Item that will let a user be able to type a product name and also have an autocomplete feature. I am using WinForms.
To populate your list with Items property in DataGridViewComboBoxColumn use this:
public DataGridViewComboBoxColumn cbColumn; // outside method for further use of inserting into DataBase
cbColumn = new DataGridViewComboBoxColumn();
bColumn.DataSource = productNames;
MSDN DataGridViewComboBoxColumn.DataSource Property
I have a table in the database that I'm retrieving using LINQ to SQL, and as a part of my processing I want to add to this list, then update the database with the new items + any changes I've made.
What I thought I could do was this:
var list = (from item in db.Table
select item).ToList();
[do processing where I modify items & add to the list]
list = list.Distinct();
db.SubmitChanges();
What happens is that the modifications happed (ie. SQL updates) but any new items I add to the list don't get added.
Obviously I'm doing this wrong, what is the correct way to modify & add to a list of DB entities, then commit all the updates & inserts?
The List is meaningless. It's just happens to hold objects that the DataContext knows about. We need to make sure that the DataContext knows about the new ones. The important thing is that they don't have to be complete when we alert the DataContext to them:
Item item;
if (needNewOne)
{
item = new Item();
db.InsertOnSubmit(item);
}
else
{
item = list[i];
}
/// build new or modify existing item
/// :
db.SubmitChanges();
You can create an extension method for it:
static void EnsureInsertedOnSubmit<TEntity>( this Table<TEntity> table
,IEnumerable<TEntity> entities)
{ foreach(var entity in entities)
{ if ( table.GetModifiedMembers(entity).Length == 0
&& table.GetOriginalEntityState(entity) == default(TEntity))
{ table.InsertOnSubmit(entity);
}
}
}
And then you can do this:
var list = db.Table1.ToList();
list.Add(new Item());
db.Table1.EnsureInsertedOnSubmit(list);
db.SubmitChanges();
You have to add the new items via InsertOnSubmit.
You've got to tell LINQ to insert the new row on submit, using InsertOnSubmit:
db.InsertOnSubmit(newrow);
db.SubmitChanges();
You do not need a new DataContext, you can use the one you are using for updates.
Same for delete DeleteOnSubmit(row). Modifications will be tracked, though.