我的一个ASP.net简单查询实现
2007-02-08 15:43
766 查看
Imports System.Data
Imports System.Data.OleDb
.....
'Initialize the query string
Dim strName, strYear, strSQL As String
Dim rTmp As DataRow
strYear = lstYear.SelectedValue
If RadioTeam.Checked = True Then
strName = lstTeam.SelectedValue
strSQL = "SELECT * FROM QueryTeamWV WHERE TeamName = '" & strName & "' And WorkYear=" & strYear
End If
....
'Create connection to one access database, and get data
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Server.MapPath("App_Data/WV.mdb")
Dim objConn As New OleDbConnection(sConnectionString)
objConn.Open()
Dim objCmdSelect As New OleDbCommand(strSQL, objConn)
Dim objAdapter1 As New OleDbDataAdapter()
objAdapter1.SelectCommand = objCmdSelect
Dim objDataset1 As New DataSet()
objAdapter1.Fill(objDataset1, "wvData")
'how to add one additional row to temp table
rTmp = objDataset1.Tables(0).NewRow
rTmp("TeamName") = "Total"
rTmp("Jan") = SumJan
rTmp("Feb") = SumFeb
rTmp("Mar") = SumMar
rTmp("Apr") = SumApr
rTmp("May") = SumMay
rTmp("Jun") = SumJun
rTmp("Jul") = SumJul
。。。。
objDataset1.Tables(0).Rows.Add(rTmp)
'Display result
gvTeam.DataSource = objDataset1.Tables(0).DefaultView
gvTeam.DataBind()
'Follow procedure are called by RowDataBound event to format the data.
'display diff with red when negative
Private Sub NumNegative(ByVal gvTmp As GridView)
Dim gvr As GridViewRow
For Each gvr In gvTmp.Rows
If Not String.IsNullOrEmpty(gvr.Cells(gvr.Cells.Count - 1).Text) Then
If CInt(gvr.Cells(gvr.Cells.Count - 1).Text) < 0 Then
gvr.Cells(gvr.Cells.Count - 1).ForeColor = Drawing.Color.Red
End If
End If
Next
End Sub
Private Sub GrouGV(ByVal gvTmp As GridView, ByVal e As System.EventArgs)
If radioDept.Checked = True Then
GroupRows(gvTmp, e, "Team", 1)
GroupRows(gvTmp, e, "Dept", 0)
Else
GroupRows(gvTmp, e, "Name", 0)
End If
End Sub
'将指定列分组合并
Private Sub GroupRows(ByVal gvTmp As GridView, ByVal e As System.EventArgs, ByVal strType As String, ByVal intColumn As Integer)
Dim text As String
text = ""
Dim count As Integer
count = 0
Dim ht As Hashtable
ht = New Hashtable
' loop through all rows to get row counts
Dim gvr As GridViewRow
For Each gvr In gvTmp.Rows
If (gvr.RowType = DataControlRowType.DataRow) Then
If (gvr.Cells(intColumn).Text = text) Then
count = count + 1
Else
If (count > 0) Then
ht.Add(strType + text, count)
End If
text = gvr.Cells(intColumn).Text
count = 1
End If
End If
Next
If (count > 1) Then ht.Add(strType + text, count)
'loop through all rows again to set rowspan
text = ""
For Each gvr In gvTmp.Rows
If (gvr.RowType = DataControlRowType.DataRow) Then
If gvr.Cells(intColumn).Text = text Then
gvr.Cells.Remove(gvr.Cells(intColumn))
Else
text = gvr.Cells(intColumn).Text
gvr.Cells(intColumn).RowSpan = Convert.ToInt32(ht(strType + text))
End If
End If
Next
End Sub
'Two small tips:
1. 如何让系统自动为Radio Button生成后台的事件代码需要将其属性:AutoPostBack=True
2. 定义grid view中数据格式,例如显示一位小数的数字:{0:N1},需要将列属性中的HtmlEncode=False,才能生效
Imports System.Data.OleDb
.....
'Initialize the query string
Dim strName, strYear, strSQL As String
Dim rTmp As DataRow
strYear = lstYear.SelectedValue
If RadioTeam.Checked = True Then
strName = lstTeam.SelectedValue
strSQL = "SELECT * FROM QueryTeamWV WHERE TeamName = '" & strName & "' And WorkYear=" & strYear
End If
....
'Create connection to one access database, and get data
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Server.MapPath("App_Data/WV.mdb")
Dim objConn As New OleDbConnection(sConnectionString)
objConn.Open()
Dim objCmdSelect As New OleDbCommand(strSQL, objConn)
Dim objAdapter1 As New OleDbDataAdapter()
objAdapter1.SelectCommand = objCmdSelect
Dim objDataset1 As New DataSet()
objAdapter1.Fill(objDataset1, "wvData")
'how to add one additional row to temp table
rTmp = objDataset1.Tables(0).NewRow
rTmp("TeamName") = "Total"
rTmp("Jan") = SumJan
rTmp("Feb") = SumFeb
rTmp("Mar") = SumMar
rTmp("Apr") = SumApr
rTmp("May") = SumMay
rTmp("Jun") = SumJun
rTmp("Jul") = SumJul
。。。。
objDataset1.Tables(0).Rows.Add(rTmp)
'Display result
gvTeam.DataSource = objDataset1.Tables(0).DefaultView
gvTeam.DataBind()
'Follow procedure are called by RowDataBound event to format the data.
'display diff with red when negative
Private Sub NumNegative(ByVal gvTmp As GridView)
Dim gvr As GridViewRow
For Each gvr In gvTmp.Rows
If Not String.IsNullOrEmpty(gvr.Cells(gvr.Cells.Count - 1).Text) Then
If CInt(gvr.Cells(gvr.Cells.Count - 1).Text) < 0 Then
gvr.Cells(gvr.Cells.Count - 1).ForeColor = Drawing.Color.Red
End If
End If
Next
End Sub
Private Sub GrouGV(ByVal gvTmp As GridView, ByVal e As System.EventArgs)
If radioDept.Checked = True Then
GroupRows(gvTmp, e, "Team", 1)
GroupRows(gvTmp, e, "Dept", 0)
Else
GroupRows(gvTmp, e, "Name", 0)
End If
End Sub
'将指定列分组合并
Private Sub GroupRows(ByVal gvTmp As GridView, ByVal e As System.EventArgs, ByVal strType As String, ByVal intColumn As Integer)
Dim text As String
text = ""
Dim count As Integer
count = 0
Dim ht As Hashtable
ht = New Hashtable
' loop through all rows to get row counts
Dim gvr As GridViewRow
For Each gvr In gvTmp.Rows
If (gvr.RowType = DataControlRowType.DataRow) Then
If (gvr.Cells(intColumn).Text = text) Then
count = count + 1
Else
If (count > 0) Then
ht.Add(strType + text, count)
End If
text = gvr.Cells(intColumn).Text
count = 1
End If
End If
Next
If (count > 1) Then ht.Add(strType + text, count)
'loop through all rows again to set rowspan
text = ""
For Each gvr In gvTmp.Rows
If (gvr.RowType = DataControlRowType.DataRow) Then
If gvr.Cells(intColumn).Text = text Then
gvr.Cells.Remove(gvr.Cells(intColumn))
Else
text = gvr.Cells(intColumn).Text
gvr.Cells(intColumn).RowSpan = Convert.ToInt32(ht(strType + text))
End If
End If
Next
End Sub
'Two small tips:
1. 如何让系统自动为Radio Button生成后台的事件代码需要将其属性:AutoPostBack=True
2. 定义grid view中数据格式,例如显示一位小数的数字:{0:N1},需要将列属性中的HtmlEncode=False,才能生效
相关文章推荐
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- 在ASP.NET Core中通过EF Core实现一个简单的全局过滤查询
- asp.net core 实现一个简单的仓储
- asp.net core 实现一个简单的仓储
- 一个简单的asp.net 单点登录实现
- ASP.NET : 一个网站繁体转换的简单实现
- ASP.NET Signalr 2.0 实现一个简单的聊天室
- asp.net core 实现一个简单的仓储
- 【网络转载】Asp.net使用XPath实现一个简单的RSS阅读器
- Asp.Net MVC3 简单入门第一季(五) 通过Asp.Net MVC的区域功能实现将多个MVC项目部署到一个站点
- 一个实现查询功能的asp.net控件
- ASP.NET三层架构的一个简单实现
- 一个简单的AJAX实现,基于C#的ASP.Net,包括服务器端的程序代码
- 用ASP.NET实现一个简单的计算器(适合入门者)
- asp.net core 实现一个简单的仓储
- 用ASP.NET实现一个简单的计算器(适合入门者)
- 一个简单的asp.net 单点登录实现
- asp.net core 实现一个简单的仓储
- Asp.net中创建程序集以及一个简单的IHttpHandler 实现
- 怎样用asp.net实现简单的查询功能