DataGrid的应用以及常用方法(Vb.Net版)

<HTML>
    <HEAD>
        <title>Info_Manage</title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content=" http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
        <LINK href="StyleSheet1.css" type="text/css" rel="stylesheet">
        <script language="javascript">
        var CurrentColor;
        Function CheckAll()function CheckAll(sel){
        var num;
        num=document.getElementsByName("CheckBox2").length;
         for(var i=0;i<num;i++)
           {document.getElementsByName("CheckBox2")[i].checked =sel;
            }
        }
        Function ItemOver()function ItemOver(Obj)
        {
         CurrentColor=Obj.style.backgroundColor;
         Obj.style.backgroundColor="#B9D1F3";
        }
        Function ItemOut()function ItemOut(Obj)
        {
         Obj.style.backgroundColor=CurrentColor;
        }
        
        Function Check_Set()function Check_Set(msg0,msg1)
        {
        var num=document.getElementsByName("CheckBox2").length;
         for(var i=0;i<num;i++)
           {
           if(document.getElementsByName("CheckBox2")[i].checked)
           {
           if(msg0!=null||msg0!="")
           {
           return confirm(msg0);}
           else{
           return true;
           
           }           
           }
            }
            alert(msg1);
            return false;
          }
          
          Function Select_Item()function Select_Item(sel)
        {
            if(sel == false)
            {
                document.getElementsByName("CkAllName")[0].checked = false;
            }
            else
            {
                var len = document.getElementsByName("CheckBox2").length;
                var num = 0;
                for(var i=0;i<len;i++)
                {
                    if(document.getElementsByName("CheckBox2")[i].checked)
                    {
                        num = num + 1;
                    }
                }
                if(num >= len)
                {
                    document.getElementsByName("CkAllName")[0].checked = true;
                }
            }
        }
        </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <FONT face="宋体">
                <TABLE id="Table1" style="Z-INDEX: 101; LEFT: 112px; WIDTH: 686px; POSITION: absolute; TOP: 40px; HEIGHT: 288px"
                    cellSpacing="1" cellPadding="1" align="center" border="0">
                    <TR>
                        <TD vAlign="bottom" align="center"><asp:label id="Label1" runat="server" Width="64px">搜索类别</asp:label><asp:dropdownlist id="DropDownList1" runat="server" Width="120px"></asp:dropdownlist> <asp:label id="Label2" runat="server" Width="80px">标题关键字</asp:label><asp:textbox id="TextBox1" runat="server"></asp:textbox><asp:button id="Button1" runat="server" Text="搜索"></asp:button>  
                            <asp:button id="Button8" runat="server" Text="重置"></asp:button>     
                            <asp:button id="Button7" runat="server" Width="72px" Text="删除所选"></asp:button></TD>
                    </TR>
                    <TR>
                        <TD align="center"><asp:datagrid id="DataGrid1" runat="server" Width="680px" DataKeyField="Id" HorizontalAlign="Center"
                                AllowPaging="True" PageSize="15" AutoGenerateColumns="False">
                                <SelectedItemStyle HorizontalAlign="Center"></SelectedItemStyle>
                                <AlternatingItemStyle HorizontalAlign="Center"></AlternatingItemStyle>
                                <ItemStyle HorizontalAlign="Center"></ItemStyle>
                                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                                <Columns>
                                    <asp:TemplateColumn HeaderText="序号">
                                        <HeaderStyle HorizontalAlign="Center" Width="60px"></HeaderStyle>
                                        <HeaderTemplate>
                                            <INPUT οnclick="CheckAll(this.checked)" type="checkbox" id="Ckall" name="CkAllName">全选
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <INPUT id="CheckBoxitem" οnclick=Select_Item(this.checked) type="checkbox"  value='<%#DataBinder.Eval(Container.DataItem,"Id")%>' name="CheckBox2">
                                        </ItemTemplate>
                                    </asp:TemplateColumn>
                                    <asp:BoundColumn DataField="Inf_Catalog_Name" HeaderText="类别">
                                        <HeaderStyle HorizontalAlign="Center" Width="100px"></HeaderStyle>
                                    </asp:BoundColumn>
                                    <asp:HyperLinkColumn Target="_self" DataTextField="Inf_Title" HeaderText="标题">
                                        <HeaderStyle HorizontalAlign="Center" Width="200px"></HeaderStyle>
                                    </asp:HyperLinkColumn>
                                    <asp:BoundColumn DataField="Inf_Author" HeaderText="作者">
                                        <HeaderStyle HorizontalAlign="Center" Width="60px"></HeaderStyle>
                                        <ItemStyle HorizontalAlign="Center"></ItemStyle>
                                        <FooterStyle HorizontalAlign="Center"></FooterStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="Inf_Dt" HeaderText="日期" DataFormatString="{0:d}">
                                        <HeaderStyle HorizontalAlign="Center" Width="80px"></HeaderStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="Inf_Uid" HeaderText="录入人">
                                        <HeaderStyle HorizontalAlign="Center" Width="60px"></HeaderStyle>
                                    </asp:BoundColumn>
                                </Columns>
                                <PagerStyle Visible="False"></PagerStyle>
                            </asp:datagrid></TD>
                    </TR>
                    <TR>
                        <TD vAlign="top" align="center"><asp:button id="Button2" runat="server" Text="首页"></asp:button> <asp:button id="Button3" runat="server" Text="上页"></asp:button><asp:label id="Msg" runat="server" Width="248px" CssClass="Center">Label</asp:label><asp:button id="Button4" runat="server" Text="下页"></asp:button> <asp:button id="Button5" runat="server" Text="末页"></asp:button>        
                            <asp:textbox id="Num" runat="server" Width="40px"></asp:textbox><asp:button id="Button6" runat="server" Text="跳转"></asp:button></TD>
                    </TR>
                </TABLE>
            </FONT>
        </form>
    </body>
</HTML> 



Public Class Info_ManageClass Info_Manage
    Inherits System.Web.UI.Page
    Dim SqlStr As String = "select Information.*,Catalog.Catalog_Name As Inf_Catalog_Name  from Information Inner Join [Catalog] On Information.Inf_Catalog_Id=[Catalog].Id " '''用于记录Sql语句
    Dim AddStr As String = "" '''用于记录Sql条件语句
    Private Const ord As String = " order by Information.Id"
    Protected WithEvents Msg As System.Web.UI.WebControls.Label
    Protected WithEvents Num As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button8 As System.Web.UI.WebControls.Button
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Dim Db As New DataAccess
Web 窗体设计器生成的代码#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()

    End Sub
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    Protected WithEvents Button3 As System.Web.UI.WebControls.Button
    Protected WithEvents Button4 As System.Web.UI.WebControls.Button
    Protected WithEvents Button5 As System.Web.UI.WebControls.Button
    Protected WithEvents Button6 As System.Web.UI.WebControls.Button
    Protected WithEvents Button7 As System.Web.UI.WebControls.Button
    Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init()Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region
    '''加载分类信息
    Sub AddCatalog()Sub AddCatalog()

        Dim Dt As New DataTable
        DropDownList1.Items.Clear()
        Dt = Db.RunAdpSelect("Select * from Catalog order by Id")
        DropDownList1.Items.Insert(0, New ListItem("请选择信息类别", -1))
        For I As Int16 = 0 To Dt.Rows.Count - 1
            DropDownList1.Items.Add(New ListItem(Dt.Rows(I)("Catalog_Name"), Dt.Rows(I)("Id")))
        Next
        DropDownList1.DataBind()
    End Sub
    '''计算各种数据
    Sub CountInfo()Sub CountInfo()
        ViewState("PageSize") = 15
        ViewState("CurrentPageIndex") = 0
        ViewState("Count") = Db.RunExeScale("Select Count(*) From Information" & AddStr)
        If ViewState("Count") Mod ViewState("PageSize") = 0 Then
            ViewState("PageCount") = ViewState("Count") / ViewState("PageSize")
        Else
            ViewState("PageCount") = Fix(ViewState("Count") / ViewState("PageSize") + 1)
        End If
    End Sub

    '''定制信息
    Sub BindData()Sub BindData()
        Dim Dt As New DataTable
        Dt = Db.RunPageSelect(SqlStr & AddStr & ord, ViewState("PageSize") * ViewState("CurrentPageIndex"), ViewState("PageSize"), "Information")
        DataGrid1.DataSource = Dt
        DataGrid1.DataBind()
        Msg.Text = "<font Color=Red>信息:" & ViewState("Count") & "条---页:" & ViewState("CurrentPageIndex") + 1 & "/" & ViewState("PageCount") & "</font>"

        If ViewState("PageCount") = 1 Then
            Button2.Enabled = False
            Button3.Enabled = False
            Button4.Enabled = False
            Button5.Enabled = False
            Button6.Enabled = False
            Exit Sub
        End If
        If ViewState("CurrentPageIndex") = 0 Then
            Button2.Enabled = False
            Button3.Enabled = False
            Button4.Enabled = True
            Button5.Enabled = True
        ElseIf ViewState("CurrentPageIndex") = ViewState("PageCount") - 1 Then
            Button4.Enabled = False
            Button5.Enabled = False
            Button2.Enabled = True
            Button3.Enabled = True
        End If
    End Sub

    Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
        If Not Page.IsPostBack Then
            '    If Session("Uid") = "" orElse Session("Uid") Is Nothing Then
            '        Response.Write("<script language='javascript'>parent.parent.location.href='../WebForm1.aspx';</script>")
            '    Else
            Button7.Attributes.Add("onclick", "return Check_Set('确定删除选择项吗?将无法恢复!','请选择要删除的项!');")
            AddCatalog()
            CountInfo()
            BindData()
            '    End If
        End If
    End Sub

    '''首页
    Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ViewState("CurrentPageIndex") = 0
        BindData()
    End Sub

    '''末页
    Private Sub Button5_Click()Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        ViewState("CurrentPageIndex") = ViewState("PageCount") - 1
        BindData()
    End Sub

    '''上页
    Private Sub Button3_Click()Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If ViewState("CurrentPageIndex") > 0 Then
            ViewState("CurrentPageIndex") = ViewState("CurrentPageIndex") - 1
        End If
        BindData()
    End Sub

    '''下页
    Private Sub Button4_Click()Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If ViewState("CurrentPageIndex") < ViewState("PageCount") - 1 Then
            ViewState("CurrentPageIndex") = ViewState("CurrentPageIndex") + 1
        End If
        BindData()
    End Sub

    '''页面跳转
    Private Sub Button6_Click()Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If IsNumeric(Num.Text.Trim) = False orElse (CInt(Num.Text.Trim) > ViewState("PageCount")) orElse (CInt(Num.Text.Trim) < 1) Then
            Response.Write("<script language='javascript'>alert('所输入内容必须为总页数内的整数数字!');</script>")
        Else
            ViewState("CurrentPageIndex") = CInt(Num.Text.Trim) - 1
            BindData()
        End If
        Num.Text = ""
    End Sub

    '''搜索
    Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        AddStr = " Where Inf_Catalog_Id= " & DropDownList1.SelectedValue & " And Inf_Title like '%" & TextBox1.Text.Trim & "%' "
        CountInfo()
        BindData()
    End Sub

    '''重置
    Private Sub Button8_Click()Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        AddStr = ""
        CountInfo()
        BindData()
    End Sub

    '''删除选择的项
    Private Sub Button7_Click()Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim Ids As String = Request.Form("CheckBox2")
        Db.RunInsertUpdateDelete("Delete From Information Where Id in (" & Ids & ")")
        Response.Write("<script language='javascript'>alert('删除完毕!');</script>")
        CountInfo()
        BindData()
    End Sub

    '''添加鼠标属性
    Private Sub DataGrid1_ItemCreated()Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
        If e.Item.ItemIndex <> -1 Then
            e.Item.Attributes.Add("onmouseover", "ItemOver(this)")
            e.Item.Attributes.Add("onmouseout", "ItemOut(this)")
        End If
    End Sub
End Class
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vb.net操作DataGridView控件的用法的集合,包括: 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选中行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView行列不表示和删除 DataGridView控件用法合集(二) 7. DataGridView行列宽度高度设置为不能编辑 8. DataGridView行高列幅自动调整 9. DataGridView指定行列冻结 10. DataGridView列顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、列、单元格取得 DataGridView控件用法合集(三) 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字列设定 16. DataGridView选择的部分拷贝至剪贴板 17.DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) DataGridView控件用法合集(四) 19. DataGridView中的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加列 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 DataGridView中输入错误数据的处理(五) 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 DataGridView控件用法合集(六) 29. DataGridView行排序(点击列表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定列指定排序 DataGridView控件用法合集(七) 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字列换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定 39. DataGridView单元格文字字体设置 40. DataGridView根据单元格值设定单元格样式 DataGridView控件用法合集(八) 41. DataGridView设置单元格背景颜色 42. DataGridView行样式描画 43. DataGridView显示行号 44. DataGridView焦点所在单元格焦点框不显示的设定 DataGridView控件用法合集(九) 45. DataGridView中显示选择框CheckBox 46. DataGridView中显示下拉框ComboBox 47. DataGridView单击打开下拉框 48. DataGridView中显示按钮 49. DataGridView中显示链接 50. DataGridView中显示图像 DataGridView控件用法合集(十) 51. DataGridView编辑中单元格控件取得 52. DataGridView输入自动完成 53. DataGridView单元格编辑时键盘KEY事件取得 54. DataGridView下拉框(ComboBox)单元格编辑时事件取得 55. DataGridView下拉框(ComboBox)单元格允许文字输入设定 DataGridView控件用法合集(十一) 56. DataGridView根据值不同在另一列中显示相应图片 57. DataGridView中显示进度条(ProgressBar) 58. DataGridView中添加MaskedTextBox DataGridView控件用法合集(十二) 59. DataGridView中Enter键按下焦点移至旁边的单元格 60. DataGridView行集合化(Group)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值