我的一个ASP.net简单查询实现

    技术2022-05-11  84

    Imports System.DataImports System.Data.OleDb

    .....'Initialize the query stringDim strName, strYear, strSQL As StringDim rTmp As DataRow

    strYear = lstYear.SelectedValue

    If RadioTeam.Checked = True Then      strName = lstTeam.SelectedValue      strSQL = "SELECT * FROM QueryTeamWV WHERE TeamName = '" & strName & "' And WorkYear=" & strYearEnd 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") = SumJanrTmp("Feb") = SumFebrTmp("Mar") = SumMar rTmp("Apr") = SumAprrTmp("May") = SumMayrTmp("Jun") = SumJunrTmp("Jul") = SumJul  。。。。objDataset1.Tables(0).Rows.Add(rTmp)'Display resultgvTeam.DataSource = objDataset1.Tables(0).DefaultViewgvTeam.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,才能生效


    最新回复(0)