如何从基于部门名称的数据表获取所有依赖行的值 [英] How Do I Get The All Depending Row Values From Datatable Based On dept Name
问题描述
大家好,
这里我从数据库中获取所有值作为数据表,在这里我想根据大学名称划分学生的详细信息,甚至我不知道数据表中的那些大学,我也必须将它绑定在单独的网格上
colege,dept
-----,----
abcd,mech
abcd,IT
abcd,cse
abcd,mech
abcd,mech
abcd,cse
abcd,cse
abcd,IT
abcd,cse
abcd,cse
abcd,IT
这是我的专栏名称(大学,部门),在这里我想将mech dept和大学名称绑定在一个网格中,同样我想绑定所有部门,但我不知道如何在表格中有多少个表格
有人可以帮我吗
提前付款
velsamy
'我认为试试吧..
Conn1 .Open()
Dim objReader As SqlDataReader
Dim PreDept As string =
Dim corDept As string =
Cmd.CommandText =select school,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
而objReader.Read
corDept = objReader.GetValue(0)
ActionDatView.Rows.Add()
如果corDept<> PreDept然后
ActionDatView.Rows( i).Cells(0).Value =------------
ActionDatView.Rows(i).Cells(1).Value =------- -----
ActionDatView.Rows.Add()
i = i + 1
结束如果
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()
你不是eed group by,distinct应该足够了。考虑将该代码与任何其他数据库查询/操作一起放入存储过程。
Dim ds As DataSet = 没有
尝试
Conn.Open()
Cmd.CommandText = 选择DISTINCT college,部门来自Hrm_Mst ORDER BY college
Cmd = 新 SqlCommand(Cmd.CommandText,Conn)
ds = Cmd.ExecuteDataset
如果 ds isnot 没有 andalso ds.tables.count> 0
your_grid.DataSource = ds.Tables( 0 )
else
your_grid.DataSource = 没有
end if
your_grid.DataBind()
catch ex as exception
MsgBox(ex.Message)
最后
Cmd.Dispose
Conn.Dispose
结束 尝试
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
'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()
You don't need group by, distinct should suffice. Consider putting that code into stored procedure along with any other database queries / operations.
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
这篇关于如何从基于部门名称的数据表获取所有依赖行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!