Add, Edit DataGrid in WPF - c#

I am new to WPF. I know how to use the DataGridView control well in Windows Forms. But I see that the DataGrid is the alternative in WPF. I want a good example for adding and editing data in a DataGrid in WPF.
For example I have the following table in database:
Channel Point Value
_________________________
A 1 w
B 1 x
A 2 y
B 2 z
The above table has to be displayed in this format:
Point A B
_______________
1 w x
2 y z
The channels are not fixed, i.e, Channels C and D can also be present.

if you have created a database or a list, u can retrieve data and print on the datagrid..
database db = new database (); //database
employees emp = new employees (); //list or table in db
var result1 = (from t in db.employees
select t).ToList();
datagrid1.Itemssource=result1;
it prints the data on datagrid

Related

Linq to Dictionary with key and a list

My data is as under in two tables
Master Columns
ID MyDateTime
1 07 Sept
2 08 Sept
MyDatetime column in above has unique index
Detail Columns
ID Data1 Data2 Data3
1 a b c
1 x y z
1 f g h
2 a b c
I want to populate this in a dictionary. I have tried
Dictionary<DateTime, List<Detail>> result = (
from master in db.master
from details in db.detail
where (master.ID == detail.ID)
select new
{
master.MyDateTime,
details
}).Distinct().ToDictionary(key => key.MyDateTime, value => new List<Detail> { value.details });
I expect two rows in the dictionary
1, List of 3 rows as details
2, List of 1 row as details
I get an error where it complains about the key of the dictionary entered twice. The key would be the datetime which is unique in the master
This is precisely what lookups are for - so use ToLookup instead of ToDictionary:
ILookup<DateTime, Detail> result =
(from master in db.master
join details in db.detail on master.ID equals detail.ID
select new { master.MyDateTime, details })
.ToLookup(pair => pair.MyDateTime, pair => pair.details);
(You shouldn't need to use Distinct, and note the use of a join instead of a second from and a where clause.)

Linq List Contains Method

Im newbie to linq and im using linq query to retrieve data from the table.My idea is to list all the cashsafes corresponding to a particular user and show it in dropdownlist.
The table structure is shown below
Table 1
cashsafeid cashsafename
1 cashsafe1
2 cashsafe2
3 cashsafe3
Table 2
Id UserId Cashsafeid
1 100 1,2,3
2 101 1,3
I've to get the cashsafename of a particular user say 100.How can i achieve it
The below code is the one i've tried but am stuck
List<Cashsafe> cashsafes=(from c in db.Table 1
where c.CashsafeId contains() )--Cannot go further
You store User's Cachsafeid column in very inefficient way - it doesn't allow to generate efficient SQL for LINQ provider. So the following solution has bad performance - if you care about that - change your table structure.
var user = db.Table2.Single(u => u.UserId == 100);
var cachfeIds = user.Cashsafeid.Split(',').Select(int.Parse).ToArray();
var cachefes = db.Table1.Where(c => cachfeIds.Contains(c.Id)).ToList();
Basically you need to join to tables, but foreign key is "virtual" - it is only in your mind. To retrieve foreign key values we must split the Cachsafeid column's value of every user to retrieve linked cachefes. And only then retrieve the cachefes with separate request (I think LINQ will retrieve all values from table and the execute Where part in C# code).
if you have no idea of join you can use
int x = 0;
List<int> Users = db.table2.FirstOrDefault(m => m.UserId == 100).Cashsafeid.Split(',').ToList().Where(str => int.TryParse(str, out x)).Select(str => x).ToList(); ;
var content = db.table1.Where(m => Users.Contains(m.cashsafeid)).ToList();

Linq to Add column values in datagrid based on the value of another column in c# windows

I have a datagrid with many columns. Below are two of those columns. I need to add up the count values for P and F values of P/F column seperately and compare them.for P the sum is 3 and for F it is 7. I need to display the sum with greater value. Is there any way i can achieve dis.
P/F | Count
P | 2
P | 1
F | 5
F | 2
Using Linq
var p_sum = from p_col in dataGridView1 // am getting error here(group by not found)
group p_col by p_col.Status into g
select g.Sum(p => p.weightagepercent) ;
Maybe this one could help:
var p_sum = (from DataGridViewRow row in dataGridView1.Rows
group row by row.Cells["Status"].Value.ToString() into g
select g.Sum(p => (int)p.Cells["Count"].Value)
).Max();
It starts by reading the row collection of the datagridview instead of the whole datagridview, then sum the "Count" grouping by the value of "Status" cell, finally select the greatest sum.

Repeat a row multiple times in a gridview

I have a Gridview with a column called Quantity. Each product has it own quantity. I need to show as many rows as the quantity says.
For example, if I have
Car | 4 | $ 20
I need to show 4 rows like
Car | 1 | 20
I can't find a way of doing this. I am using linq to retrieve the data from the database and that is the DataSource of my Gridview.
You can do it using the linq Range method. Assuming you have a linq source like this (trivial example)
var result =
from r in db.SourceTable
select new { r.Name, r.Quantity, r.Price };
Add this to the query
var result =
from r in db.SourceTable
from s in Enumerable.Range(1, record.Quantity)
select new { r.Name, 1, r.Price };
However, this is probably not supported by entity framework (and probably not in any other ORM) so you may need to call .ToList() or .AsEnumerable() first.

Populating an ObservableCollection of ObservableCollection, delimited by two database columns

My database is as follows :
ID Date Number NumberIWishToRecord
What I wish to do is use a Linq-to-SQL query to populate an ObservableCollection<ObservableCollection<CustomClass>>.
What I want is select only the rows were Number == a given parameter.
ID refers to a person, what I want to do is get all the information about a person and store it in an ObservableCollection, so I will have an ObservableCollection<CustomClass>, with each CustomClass holding information about only one row, and each ObservableCollection<CustomClass> holding information about only one person (recorded on different days).
I then wish to select an ObservableCollection of the ObservableCollection<CustomClass> which will hold information on all people!
So, some sample data :
ID Date Number NumberIWishToRecord
1 27-06-2012 0.1933 25
1 28-06-2012 0.1933 27
1 29-06-2012 0.1933 29
2 14-06-2012 0.1933 412
2 15-06-2012 0.1741 321
So when I run my method, I want to return only the Numbers of the given parameter, in my case I will choose 0.1933.
I then want both rows where ID = 1 to be saved in an ObservableCollection<CustomClass>, and the single row where ID == 2 to be saved in another ObservableCollection<CustomClass>. Then, both of these ObservableCollections will be held in their own ObservableCollection! To illustrate :
ObservableCollection<ObservableCollection<CustomClass>>
ObservableCollection<CustomClass>
1 27-06-2012 0.1933 25
1 28-06-2012 0.1933 27
1 29-06-2012 0.1933 29
ObservableCollection<CustomClass>
2 14-06-2012 0.1933 412
How would I write a query in linq to sql that would do this ?
I'll just write a standard query syntax Linq expression to achieve this, you adapt it for your tables.
var rowsById = new ObservableCollection<ObservableCollection<row>>(
from r in _rows
where r.number == 1.2
group r by r.ID into rowIdGroup
select new ObservableCollection<row>(rowIdGroup));
If you need to convert data from the row into the CustomClass:
var rowsById = new ObservableCollection<ObservableCollection<CustomClass>>(
from r in _rows
where r.number == 1.2
group r by r.ID into rowIdGroup
select new ObservableCollection<CustomClass>(
rowIdGroup.Select(r => new CustomClass
{
ID = r.ID,
Number = r.number // add more
})));
Or if you prefer query syntax in all the expression:
var rowsById = new ObservableCollection<ObservableCollection<CustomClass>>(
from r in _rows
where r.number == 1.2
group r by r.ID into rowIdGroup
select new ObservableCollection<CustomClass>(
from gr in rowIdGroup select new CustomClass
{
ID = gr.ID,
Number = gr.number
}));

Categories