Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have this datatable dt3 which looks like:
ADminNo     PaperNo
111411H     3
111411H     18
172828z     3
172828z     18
111380Y     93
111938S     10


How to i get something like:

ConflictingPaper     Numberofstudents    AdminNo
3 : 8                2                   111411H
                                         172828z


so on and so forth.
To populate in a datagridview.
i tried something like:

VB
Dim curadminno As String = String.Empty
                        curadminno = dr3("AdminNo").ToString
                        Dim z, k As Integer
                        For z = 0 To dt3.Rows.Count
                            If dt3.Rows(z).Item(0) = curadminno Then
                                DataGridView3.Rows.Add(String.Format("{1}:{1}", _
                                                   dr3("PaperNo").ToString()))



its wrong though.

appreciate if anyone could help me out with this!
Thanks!
Posted
Updated 24-Jul-13 20:37pm
v2
Comments
Pheonyx 25-Jul-13 3:04am    
I think your question is a bit out of context, where did the 8 come from in the "ConflictingPaper" column? What defines a conflict? could you explain a bit more about the scenario that you are working with as, I personally, feel the question is unclear without a greater understanding.

Is this data from SQL? or just stored in datatables in memory?
12345_abcde 25-Jul-13 3:40am    
it 18 instead of 8, sorry. conflicting means, there is a common student in this 2 papers. so that later on the time table scheduler will know that these 2 papers cannot be scheduled at in the same time slot as there is common students which will cause the conflict.

data is stored in datatable memory. using with vb.net

Hope i had made the question clearer.

If you use SQL Server, then you can try something like this:
SQL
DECLARE @tmp TABLE (ADminNo VARCHAR(30), PaperNo INT)

INSERT INTO @tmp (AdminNo, PaperNo)
SELECT '111411H', 3
UNION ALL SELECT '111411H', 18
UNION ALL SELECT '172828z', 3
UNION ALL SELECT '172828z', 18
UNION ALL SELECT '111380Y', 93
UNION ALL SELECT '111938S', 10

SELECT t1.AdminNo, COUNT(t1.AdminNo) AS NumberOfStudents, STUFF( (SELECT ', ' + CONVERT(VARCHAR(5),[PaperNo]) AS 'text()' 
                  FROM @tmp AS t2
                  WHERE t2.AdminNo = t1.AdminNo
                  FOR XML PATH('')), 1, 1, '') AS [ConflictingPaper]
FROM @tmp AS t1
GROUP BY AdminNo
HAVING COUNT(AdminNo)>1


Result:
AdminNo    NumberOfStudents     ConflictingPaper
111411H    2                    3, 18
172828z    2                    3, 18



[EDIT]
12345_abcde wrote:
data is retrieved from access database and stored in datatable. using with vb.net


So... You can use below query:
SQL
SELECT t1.AdminNo, COUNT(t1.AdminNo) AS NumberOfStudents
FROM @tmp AS t1
GROUP BY AdminNo
HAVING COUNT(AdminNo)>1

It will return the same result as above, but without the ConflictingPaper column.

How to fetch data from MS Access database? Please, read this: Accessing Microsoft Office Data from .NET Applications[^]

[/EDIT]
 
Share this answer
 
v2
Comments
12345_abcde 25-Jul-13 3:36am    
Hi, im using vb.net 2008, oledb connection
and i cant name out the data as there are too many to name out.

is there a way to do it?
Maciej Los 25-Jul-13 3:53am    
I don't get you ;(
All you need to do is to execute OleDbCommand with command text as i suggest you in updated answer...

[EDIT]
You don't need to loop through the datatable collection of rows. My way is simplest and fastest ;)
[/EDIT]
12345_abcde 25-Jul-13 4:09am    
@tmp is table name? t1 is table?

i would need the conflicting paper ): could you help me with that?

Thanks!
12345_abcde 25-Jul-13 4:15am    
Sorry, this data is in datatable memory. not in access datatable.
i used the original data to manipulate this datatable out.
Kuthuparakkal 25-Jul-13 9:17am    
See my solution...

Thanks,

Kuthuparakkal
Try this:
Your required data will be stored into the datatable dt....
VB
Dim dt As New DataTable
  ' Create four typed columns in the DataTable.
  dt.Columns.Add("ConflictingPaper", GetType(String))
  dt.Columns.Add("Numberofstudents", GetType(String))
  dt.Columns.Add("AdminNo", GetType(String))

  Dim query1 = (From _a In dt3
          Group Convert.ToString(_a.Field(Of Int32)("PaperNo"))
          By AdminNo = _a.Field(Of String)("AdminNo")
          Into Group
          Select dt.LoadDataRow(New Object() {String.Join(":", Group.ToArray()), Group.Count(), AdminNo}, False)
          ).ToList().Count()


FYI The Imports:
VB
Imports System
Imports System.Data
Imports System.Math
Imports System.Linq
 
Share this answer
 
v5
Comments
12345_abcde 26-Jul-13 1:45am    
Hi thanks Kuthuparakkal .

hmm, but what is _a & _afield?

and i also having error stating: unable to cast object of type 'system.int32' to type 'system.string'
i think cause my paperNo is integer values. whe i changed it to (of integer) then i have got error here:
| Group.ToArray() |

value of 1 dimensional integer array cannot be converted into 1 dimensional string array, because integer is not derived from string.

How do i solve this?
Kuthuparakkal 26-Jul-13 2:50am    
Modified soln, please try!
Kuthuparakkal 26-Jul-13 3:14am    
_a is alias for dt3 in Linq query. I used Linq for solving this... You may look up MSDN for Linq(Language Integrated Query)

Thanks,

Kuthuparakkal
12345_abcde 26-Jul-13 3:20am    
Hi, i tried, but my form still sort of hang there, it doesnt load the data out. Please help!

Dim query1 = (From a In dt3 Group Convert.ToString(a.Field(Of Int32)("PaperNo")) By AdminNo = a.Field(Of String)("AdminNo") Into Group Select dt.LoadDataRow(New Object() {String.Join(":", Group.ToArray()), Group.Count(), AdminNo}, False)).ToList().Count()

DataGridView3.AutoGenerateColumns = True
Me.DataGridView3.DataSource = dt
Kuthuparakkal 26-Jul-13 3:31am    
Put a break point on "DataGridView3.AutoGenerateColumns = True" and view what's there in dt...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900