Click here to Skip to main content
15,875,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

Here I am getting the all values form my DB as a datatable , here I want to divide the students details based on college name but even I don't know the whatever colleges r there in datatable and also I have to bind this on separate grid


colege, dept
----- ,----
abcd ,mech
abcd ,IT
abcd ,cse
abcd ,mech
abcd ,mech
abcd ,cse
abcd ,cse
abcd ,IT
abcd ,cse
abcd ,cse
abcd ,IT

this is my column names(college , dept), here i want to bind the mech dept along with college name in one grid similarly i want to bind the all dept , but i dont know howmany depts are there in table

could anyone help me

thanks in advance
velsamy
Posted
Comments
Sinisa Hajnal 7-Oct-14 6:27am    
What have you tried before?

Google Master-detail datagrid. Use two selects distinct, one for college, another for departments...

Please provide expected output based on the example above: that is, what do you expect to see from the data above?
avelsamy 7-Oct-14 7:50am    
before i have tried with StringBuilder, but o should use paging, and row events too. that is y i want it in grid
Sinisa Hajnal 7-Oct-14 7:56am    
You didn't answer the question: what do you expect as output from the data above?
avelsamy 7-Oct-14 8:33am    
exactly this

college, dept
-------, ----
abcd, mech
abcd, mech

college, dept
-------, ----
abcd, cse
abcd, cse

college, dept
-------, ----
abcd, IT
abcd, IT




'I think try it..

 Conn1.Open()
 Dim objReader As SqlDataReader
 Dim PreDept As string=""
 Dim corDept As string=""
 Cmd.CommandText = "select college, dept from Hrm_Mst group by college, dept"
 Cmd = New SqlCommand(Cmd.CommandText, Conn1)
 objReader = Cmd.ExecuteReader
 ActionDatView.Rows.Clear()
 Dim i As Integer = 0
 While objReader.Read
     corDept =objReader.GetValue(0)
     ActionDatView.Rows.Add()
     if corDept<>PreDept then
         ActionDatView.Rows(i).Cells(0).Value = "------------"
         ActionDatView.Rows(i).Cells(1).Value = "------------"
         ActionDatView.Rows.Add()
         i = i + 1
     End If
     ActionDatView.Rows(i).Cells(0).Value = objReader.GetValue(0)
     ActionDatView.Rows(i).Cells(1).Value = objReader.GetValue(1)
     PreDept=corDept
     i = i + 1
 End While
 objReader.Close()
 Conn1.Close()
 
Share this answer
 
v3
You don't need group by, distinct should suffice. Consider putting that code into stored procedure along with any other database queries / operations.

VB
Dim ds As DataSet = nothing
try
     Conn.Open()

     Cmd.CommandText = "select DISTINCT college, dept from Hrm_Mst ORDER BY college"
     Cmd = New SqlCommand(Cmd.CommandText, Conn)
     ds  = Cmd.ExecuteDataset
     if ds isnot nothing andalso ds.tables.count > 0 
         your_grid.DataSource = ds.Tables(0)
     else
         your_grid.DataSource = nothing
     end if

     your_grid.DataBind()
 catch ex as exception
MsgBox(ex.Message)
 Finally
     Cmd.Dispose
     Conn.Dispose
 End Try
 
Share this answer
 

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