CSDN博客

img huanghui22

代码管理(1)

发表于2004/9/25 20:49:00  841人阅读

1--------------从数据库中得到所有注册用户的数据,放在dataSet1中,接着从Application中得到在线的用户名,从dataSet1中过滤出在线用户的记录,与DataGrid绑定
 
'从application中取得所有登录人员的appAllUserId
        userArray = CType(Application("appAllUserId"), ArrayList)

        Dim i As Integer
        Private strOnlinePs As String
'把userArray转换成string,用,分隔
        For i = 0 To usercount - 1
            strOnlinePs = strOnlinePs + "','" + userArray(i)
        Next

'过滤出记录
        Me.dtView = dtUser.DefaultView

        Me.dtView.RowFilter = "user_id in ('" & strOnlinePs & "')"
        Me.dtgTask.DataSource = Me.dtView
'数据绑定        
        Me.dtgTask.DataBind()


2----------------------精确查询和模糊查询

   '如果checkbox被选中则要模糊查询
            If Me.chkSrc.Checked = True Then
                If Me.txtUserID.Text.Trim <> "" Then
                    str = "user_id like '%" & Me.txtUserID.Text.Trim & "%'"
                End If
                If Me.txtUserName.Text.Trim <> "" Then
                    If str <> "" Then
                        str = str + "and c_name like '%" + Me.txtUserName.Text.Trim + "%'"
                    Else
                        str = "c_name like '%" + Me.txtUserName.Text.Trim + "%'"
                    End If
                End If
            Else
                '如果checkbox未被选中则精确查询
                If Me.txtUserID.Text.Trim <> "" Then
                    str = "user_id='" & Me.txtUserID.Text.Trim & "'"
                End If
                If Me.txtUserName.Text.Trim <> "" Then
                    If str <> "" Then
                        str = str + "and c_name='" + Me.txtUserName.Text.Trim + "'"
                    Else
                        str = "c_name='" + Me.txtUserName.Text.Trim + "'"
                    End If
                End If
            End If
            '如果str不为空,先过滤一下
            If str <> "" Then
                Me.dtView.RowFilter = "user_id in ('" & strOnlinePs & "') and " & str
            End If

 
3---------------------时间控件的用法

   ' 当使用时间控件查询是必须3个textBox都不为空,否则报错
            If timeFrom = "" Then
                Return
            End If
            If timeTo = "" Then
                Return
            End If

            '2个时间控件都不为空
            If Me.UCTime1.GetSetTime <> DateTime.MinValue And Me.UCTime2.GetSetTime <> DateTime.MinValue Then
                'dtUser.DefaultView表中记录过滤
                i = Me.dtView.Count
                While i <> 0
                    login_dt = dtView.Item(i - 1).Item("lst_login_dt")

                    temp = login_dt.Substring(0, 10)
                    timeFrom = temp + timeFrom
                    timeTo = temp + timeTo
                    '如果登陆时间在timeFrom和timeTo之间
                    If DateTime.Parse(timeFrom) <= DateTime.Parse(login_dt) And DateTime.Parse(timeTo) >= DateTime.Parse(login_dt) Then
                    Else
                        '如果不在的话删掉这一行
                        dtView.Item(i - 1).Delete()
                    End If
                    timeFrom = timeFrom.Substring(10)
                    timeTo = timeTo.Substring(10)
                    i = i - 1
                End While
            End If

            'UcTime2为空的话
            If Me.UCTime1.GetSetTime <> DateTime.MinValue And Me.UCTime2.GetSetTime = DateTime.MinValue Then
                'dtUser.DefaultView表中记录过滤
                i = Me.dtView.Count
                While i <> 0
                    login_dt = dtView.Item(i - 1).Item("lst_login_dt")

                    temp = login_dt.Substring(0, 10)
                    timeFrom = temp + timeFrom

                    '如果登陆时间大于timeFrom
                    If DateTime.Parse(timeFrom) <= DateTime.Parse(login_dt) Then
                    Else
                        '如果不大于的话删掉这一行
                        dtView.Item(i - 1).Delete()
                    End If
                    timeFrom = timeFrom.Substring(10)
                    i = i - 1
                End While
            End If

            'UcTime1为空的话
            If Me.UCTime1.GetSetTime = DateTime.MinValue And Me.UCTime2.GetSetTime <> DateTime.MinValue Then
                'dtUser.DefaultView表中记录过滤
                ' For i = 0 To dtUser.DefaultView.Count - 1
                i = Me.dtView.Count
                While i <> 0
                    login_dt = Me.dtView.Item(i - 1).Item("lst_login_dt")

                    temp = login_dt.Substring(0, 10)
                    timeTo = temp + timeTo

                    '如果登陆时间小于timeTo
                    If DateTime.Parse(timeTo) >= DateTime.Parse(login_dt) Then
                    Else
                        '如果不小于的话删掉这一行
                        Me.dtView.Item(i - 1).Delete()
                    End If
                    timeTo = timeTo.Substring(10)
                    i = i - 1
                End While
            End If

            'Me.dtgTask.DataSource = Me.dtView
            Me.dtgTask.DataBind()
            'Me.lblPerNum.Text = Me.dtView.Count
        End If


4-----------------给DataGrid添加自动增长列

 <Columns>
              <asp:TemplateColumn>
               <ItemTemplate>
               <!-- 这里是关键,给DataGrid添加自动增长列-->
                <SPAN>
                   <%# Container.ItemIndex+1 %>
                </SPAN>
               </ItemTemplate>
              </asp:TemplateColumn>
              
              
              <asp:BoundColumn DataField="user_id" SortExpression="user_id desc"></asp:BoundColumn>
              <asp:BoundColumn DataField="c_name"></asp:BoundColumn>
              <asp:BoundColumn DataField="lst_lng_ip"></asp:BoundColumn>
              <asp:BoundColumn DataField="lst_login_dt"></asp:BoundColumn>
              <asp:BoundColumn DataField="login_num"></asp:BoundColumn>
              
             </Columns><%# Container.ItemIndex+1 %><%# Container.ItemIndex+1 %><%@ Register TagPrefix="uc1" TagName="UCTimeControl" Src="../controls/UCTime.ascx" %>

阅读全文
0 0

相关文章推荐

img
取 消
img