GridView with columns and rows swapped - c#

I am using a GridView. My requirement is like -
Name C1 C2 C3 C4
Age 24 25 26 27
Gender M F M F
I didn't get any solution. Please suggest correct solution.
Do I need to create a new UserControl for this?

Try this
http://www.aspdotnet-suresh.com/2012/09/how-to-convert-rows-to-columns-in-sql.html
Or try to create a view to bind data with ur gridview .

Related

Dynamic Convert Row to Column in Linq

I want to convert Row to Column, actually I can implement a Static row to column as below, but it only works if I know the row position beforehand, so does anyone know how to dynamically convert a Row to Column in Linq?
This is the below table
ItemCode LOC001 LOC002 LOC003
AAA 10 11 12
BBB 13 31 14
CCC 15 18 0
This is the Static Code:
var table=(from x in Mst_LocationItems
group x by x.ItemCode into gr
select new
{
ItemCode=gr.Key,
LOC001 = gr.Where(x=>x.LocationID == "LOC001").Sum(x=>x.Reorder),
LOC002 = gr.Where(x=>x.LocationID == "LOC002").Sum(x=>x.Reorder),
LOC003 = gr.Where(x=>x.LocationID == "LOC003").Sum(x=>x.Reorder)
}).ToList();
table.Dump();

Select distinct items with a specific field in an array and display in tabular form

I have a stored procedure which is returning 3 columns/fields and i'm saving the result set in an arrayList. Each row contains three fields ID,name,description.I want to get all distinct category from this array into separate array or some other objects.
For example, if my output had 100 rows returned by sproc, there might be 10 rows with category1, 20 rows with category2, 35 rows with category3 and so.
Now i need to display like below, i.e display all ID comes under each category.
category1
ID Name
1 A
19 B
32 C
category2
ID Name
10 D
11 T
54 D
and so on...
I can use gridview or Repeater or table to display this.
Sample code:
Dim a As ArrayList
a = //values from sproc
'we need to implement some logic here display like above.
Please let me know how to display this properly. Thanks in advance!
Some nested linq queries should get you that result...I've put together an example of the principle, but of course it will need to be adapted to your program (can't tell what language you're using from your tags/post, so this is in vb.net):
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lstAry As New List(Of String())
lstAry.Add({"1", "a", "d1"})
lstAry.Add({"1", "b", "d3"})
lstAry.Add({"2", "c", "d2"})
lstAry.Add({"3", "a", "d4"})
lstAry.Add({"3", "a", "d5"})
Dim distinctCats = lstAry.Select(Function(x) x(0)).Distinct
If distinctCats IsNot Nothing Then
For Each distinctcat As String In distinctCats
Debug.Print("")
Debug.Print(distinctcat)
Dim catmembers = lstAry.Where(Function(x) (x(0) = distinctcat)).Distinct
If catmembers IsNot Nothing Then
For Each catmember As String() In catmembers
Debug.Print(catmember(1) & " " & catmember(2))
Next
End If
Next
End If
End Sub
This outputs:
1
a d1
b d3
2
c d2
3
a d4
a d5
So if you run this sub's logic on your array list you should get results formatted pretty closely to what you're looking for. First I'm getting the distinct categories, and then getting the distinct arrays for each category and just printing each group.

Getting ID (PK) from Table based on Selected Columns in ComboBox

I have a table of STATUS and it contains these columns:
ID ColA ColB ColC
and I want these 3 columns to be shown in ComboBox of a WPF Form. Fine! I will do it by getting Distinct ColA,ColB,ColC respectively (Kindly correct me if I am wrong)
Now I want to insert an entry in another table based on these comboboxes.
This table, STATUS_HISTORY has following structure (not related to question I think):
ID EmpID (FK from Emp) StatusID (FK from Status) UpdationDate
Now how would I get the ID of status based on the selected ComboBoxes? Should I use a query like this:
SELECT ID from STATUS WHERE ColA LIKE '#SelectedColAValue' AND ColB LIKE '#SelectedColBValue' AND ColC LIKE '#SelectedColCValue'
It sounds a very trivial way to me. Is there any better way to do it?
UPDATE
STATUS Table has rows like this:
ID ColA ColB ColC
1 A1 B1 C1
2 A1 B1 C2
3 A1 B2 C1
4 A1 B2 C2
5 A1 B3 C1
6 A1 B3 C2
7 A2 B1 C1
8 A2 B1 C2
Now if I select A1 in ComboBox1, it corresponds to multiple rows (and so on..)
I hope it explains it further
The easier way to do is create Status .NET object and bind it to the comboboxes.
You could bind the same object to all the comboboxes and display which-ever value you want to display in the combo-box.
Check DisplayMemberPath , SelectedItem ,SelectedValue and SelectedValuePath
And Just pass the SelectedItem ID to the database.
Make the ID column of STATUS a String and generate it like ColA + ColB + ColC. It must be unique for all rows. So then user select values from ComboBoxes make the same concatination of ComboBoxA.Value + ComboBoxB.Value + ComboBoxC.Value. That will be PK to insert in STATUS_HISTORY.

Add, Edit DataGrid in WPF

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

How to get records with the same specific column content from a DataTable using LINQ

I have a table with personal data which looks something like this:
Identifier Name Phone Address
.............................
1 aa 23 abc
2 bb 22 abd
2 cc 11 aaa
3 dd 44 amd
4 fa 33 agd
2 ds 14 dad
3 as 55 fgg
I want to get the records with the same identifier, using LINQ, to get something like this
Identifier Name Phone Address
.............................
2 bb 22 abd
2 cc 11 aaa
2 ds 14 dad
3 dd 44 amd
3 as 55 fgg
I could order by Identifier and copy to a new DataTable, then parse it and get the records with the same identifier, but that would be expensive i guess. Is there a shorter way ?
Thank you !
Something like the code below would filter and extract the duplicates to a new DataTable with the same schema. Code assumes Identifier is an int. Replace with the appropriate names and types, as applicable.
var extractedDuplicates = (from row in table.AsEnumerable()
group row by row.Field<int>("Identifier") into rows
where rows.Count() > 1
from row in rows
select row).CopyToDataTable();
Give it a try and see how far that gets you. If there is any chance there aren't any duplicates, you will want to split this into multiple statements, as CopyToDataTable() will throw if there are no rows to copy.
var duplicateRows = from row in table.AsEnumerable()
group row by row.Field<int>("Identifier") into rows
where rows.Count() > 1
from row in rows
select row;
DataTable extractedDuplicates;
if (duplicateRows.Any())
extractedDuplicates = duplicateRows.CopyToDataTable();
else
extractedDuplicates = table.Clone();
And, of course, if you do not need a new DataTable, omit the second portion of this code entirely and just work with duplicateRows.
select * from YourTable a where a.Identifier in
(
select aa.Identifier from YourTable aa group by aa.Identifier
having (count(aa.Identifier ) > 1)
)

Categories