如何在DataGrid上创建搜索按钮?

时间:2011-05-16 15:53:36

标签: asp.net vb.net search datagrid

我正在使用Visual Studio 2008 Shell和SQL Server 2008.目前我能够显示DataGrid。但是现在我有一个新的要求,我必须在其中一个列中启用文本搜索。我该如何实现呢?我尝试实现我在网上找到的解决方案,但我仍然遇到错误。请温柔;我是VB.Net程序员的新手!

由于这是Shell版本,并且源数据位于我无法直接访问的其他计算机上,因此我无法进行调试。

以下是我的所有代码。请注意,目前我甚至无法将其视为ASPX页面,因为仍然存在一些错误。你可以给我一个URL或一些简单的提示,让我在开发时遵循它吗?谢谢!

以下是我的代码:

<%@ Page Language="VB" Debug="true" Src="../Global.vb"%>
<%@ Import Namespace="ChartDirector" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
<%@ Import Namespace="System.Math" %>
<%@ Import Namespace="system.data.SqlClient" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Register TagPrefix="chart" Namespace="ChartDirector" Assembly="netchartdir" %>

<html>
    <SCRIPT LANGUAGE="VB" RUNAT="Server">
        Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

            If Not IsPostBack Then
                Main()
            Else
                If part_transfer.Value.Trim() <> "" Then
                    'ShowChart()
                    Panel1.Visible = False
                    Panel2.Visible = True
                End If
            End If


        End Sub

        Dim DSTableData As New System.Data.DataSet


        Sub Main()          
            '------------------------- Query database and get arrays for the chart and bind query results to datagrid ----------------------------------------                                                              

            If check1.Checked Then
                DSTableData = GlobalFunctions.GlobalF.FillSparePartsTable(1)
            Else
                DSTableData = GlobalFunctions.GlobalF.FillSparePartsTable(0)
            End If

            Me.Button1 = New System.Web.UI.Control
            Me.TextBox1 = New System.Web.UI.Control

            dgTable.DataSource = DSTableData
            dgTable.DataBind()

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = 0 To DSTableData.Tables(0).Rows.Count - 1
                If dgTable.Items(i).ToString = TextBox1.Text Then
                    Me.dgTable.SelectedItem(i)
                    Me.dgTable.SelectedIndex(i)
                    Exit Sub
                End If
            Next
        End Sub

        Sub ScrollToRow(ByVal row As Integer)
            If Not Me.DataBind(row) Is Nothing Then
                Me.GridVScrolled(Me, New ScrollEventArgs(ScrollEventType.LargeIncrement, row))
            End If
        End Sub

        Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
            If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
                Dim thepriority As Integer

                If e.Item.Cells(7).Text = "&nbsp;" Then
                    thepriority = 3
                Else
                    thepriority = e.Item.Cells(7).Text
                End If

                Dim thecolor As String

                If thepriority = 1 Then
                    thecolor = "Red"
                Else
                    If thepriority = 2 Then
                        thecolor = "Yellow"
                    Else
                        thecolor = "Green"
                    End If
                End If
                Dim i As Int16

                If thepriority = 1 Then
                    For i = 0 To 6
                        e.Item.Cells(i).BackColor = System.Drawing.Color.Red
                    Next
                Else
                    If thepriority = 2 Then
                        For i = 0 To 6
                            e.Item.Cells(i).BackColor = System.Drawing.Color.Yellow
                        Next
                    Else
                        For i = 0 To 6
                            e.Item.Cells(i).BackColor = Drawing.Color.LightGreen 'System.Drawing.Color.Green
                        Next
                    End If
                End If


                Dim itemType As System.Web.UI.WebControls.ListItemType
                Dim thepart As String = e.Item.Cells(0).Text
                Dim thepartdesc As String = e.Item.Cells(1).Text
                itemType = CType(e.Item.ItemType, System.Web.UI.WebControls.ListItemType)
                If (itemType <> System.Web.UI.WebControls.ListItemType.AlternatingItem) Then
                    e.Item.Attributes.Add("OnMouseOver", "this.style.backgroundColor = '" & thecolor & "';")
                    e.Item.Attributes.Add("OnMouseOut", "this.style.backgroundColor = 'white';")
                    e.Item.Attributes.Add("OnClick", "GetChart('" & thepart & "','" & thepartdesc & "','" & thecolor & "',this);")
                Else
                    e.Item.Attributes.Add("OnMouseOver", "this.style.backgroundColor = '" & thecolor & "';")
                    e.Item.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#eeeeee';")
                    e.Item.Attributes.Add("OnClick", "GetChart('" & thepart & "','" & thepartdesc & "','" & thecolor & "',this);")
                End If
            End If
        End Sub

        Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
            GlobalFunctions.GlobalF.DataGridToExcel(dgTable, Response)
        End Sub

        Sub ReRun_Main(ByVal sender As Object, ByVal e As EventArgs)
            Main()
        End Sub

    </SCRIPT>
    <script type="text/javascript" language="javascript">
        function GetChart(thepart, thepartdesc, thecolor, row)
        {
            Form1.part_transfer.value = thepart;
            Form1.part_desc_transfer.value = thepartdesc;
            Form1.submit();
        }
    </script>
    <head> 
        <title></title>
        <style type="text/css">
            .DataGridFixedHeader {background-color: white; position:relative; top:expression(this.offsetParent.scrollTop);}
        </style>
    </head>
    <body>  
        <form runat="Server" method="post" id="Form1">
            <div style="font-size:18pt; font-family:verdana; font-weight:bold; color:#336699">
               Parts Watch List
            </div>
            <hr style="font-weight: bold; font-size: 20pt; color:#000080;" />
            <div style="height: 380px; text-align: center; position: static;">              
                <input id="part_transfer" type="hidden" runat="server"/>
                <input id="part_desc_transfer" type="hidden" runat="server"/>
                <asp:Panel id="Panel1" runat="server" HorizontalAlign = "Center">
                       <span style="font-weight: bold; text-align: left; font-size: 15pt;">
                            Calculation:</span><br />
                        Reliability Rate &nbsp;=&nbsp; 1 - ( number of failed parts in the last 6 months / Part Multiplier * Average instrument Census in the last 6 months),<br />
                        Where
                        <br />
                        Number of failed parts - Summation of failures of a part for a 6 month period<br />
                        Part Multiplier - Number that represents how many times a part is used on the intrument<br />
                        Average instrument census -&nbsp; Average of the instrument census for the same
                        6 month timeframe as failed parts<br />
                        <br />
                        &nbsp; &nbsp; &nbsp; 
                        Please choose one of the parts below to view the control charts.</p>
                </asp:Panel>
                <asp:Panel id="Panel2" runat="server" Visible="false">
                    <chart:WebChartViewer id="WebChartViewer1" runat="server" HorizontalAlign="Center" />
                </asp:Panel>
            </div>
            <hr style="width: 90%; position: static;" />

            <div style="text-align: center; position: static; ">
                <asp:Label id="CensusLastUpdate" runat="server"/><br />
                <asp:Button id="btnExport" runat="server" Text="Export to Excel"></asp:Button>
                <asp:CheckBox id="check1" Text="Display Only Parts Below Threshold" TextAlign="Right" AutoPostBack="True" OnCheckedChanged="ReRun_Main" runat="server" />
                <asp:TextBox ID="TextBox1" Text="Enter Part Number" runat="server" />
                <asp:Button ID="Button1" Text="Search" runat="server" />
            </div> 

            <% Dim scrollPosURL As String = "../includes/ScrollPos.htc" %>

            <input id="saveScrollPos" type="hidden" runat="server" name="saveScrollPos"/>   
            <table border="0" cellspacing="0" align="center" style="">  
                <tr>  
                    <td> 
                        <div persistID="<%= saveScrollPos.UniqueID %>" scrollPOS="<%= saveScrollPos.value %>" style="OVERFLOW: auto; 
                            HEIGHT: 400px; BEHAVIOR: url(<%= ResolveURL(scrollPosURL)%>);">  
                            <ASP:DATAGRID ID="dgTable" HorizontalAlign="Center" runat="server" AutoGenerateColumns="False" ShowHeader="True" OnItemDataBound="DataGrid1_ItemDataBound">
                                <AlternatingItemStyle BackColor = "#eeeeee" />
                                <HEADERSTYLE CssClass="ms-formlabel DataGridFixedHeader" BackColor = "#336699" ForeColor = "#ffffff" Font-Bold = "true" />  
                                <COLUMNS>
                                    <ASP:BOUNDCOLUMN HEADERTEXT="Part No." DATAFIELD="PN" READONLY="true" ItemStyle-Width="130px" ItemStyle-Font-Size="8"/>
                                    <ASP:BOUNDCOLUMN HEADERTEXT="Part Desc" DATAFIELD="PART_DESC" READONLY="true" ItemStyle-Width="150px" ItemStyle-Font-Size="8"/>
                                    <ASP:BOUNDCOLUMN HEADERTEXT="Number of Failed Parts (last 6 months)" DATAFIELD="numFailed" READONLY="true" ItemStyle-Width="200px" ItemStyle-Font-Size="8"/>
                                    <ASP:BOUNDCOLUMN HEADERTEXT="Average Census (last 6 months)" DATAFIELD="AvgCensus" READONLY="true" ItemStyle-Width="215px" ItemStyle-Font-Size="8"/>
                                    <ASP:BOUNDCOLUMN HEADERTEXT= "Part Multiplier" DATAFIELD="PartMultiplier" READONLY="true" ItemStyle-Width="100px" ItemStyle-Font-Size="8"/>
                                    <ASP:BOUNDCOLUMN HEADERTEXT= "Criticality" DATAFIELD="Criticality" READONLY="true" ItemStyle-Width="100px" ItemStyle-Font-Size="8"/>
                                    <ASP:BOUNDCOLUMN HEADERTEXT="Reliabilty Rate" DATAFIELD="ReliabilityRate" READONLY="true" ItemStyle-Width="105px" ItemStyle-Font-Size="8"/>
                                    <ASP:BOUNDCOLUMN HEADERTEXT="Priority" DATAFIELD="PRIORITY" READONLY="true" Visible="False" ItemStyle-Width="0px" ItemStyle-Font-Size="8"/>
                                </COLUMNS>              
                            </ASP:DATAGRID>
                        </div>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您可能需要为GlobalFunctions.GlobalF.FillSparePartsTable(1, SearchString)创建一个函数重载,它将接受搜索字符串作为参数。

使用类似

的查询创建数据源
select * from search table where searchcolumn like @Searchstring  

并将百分比(%)sybmol附加到参数的任一侧。

然后在你的搜索按钮onclick事件中让它调用新函数并绑定结果。