【问题标题】:How to create a Search button on a DataGrid?如何在 DataGrid 上创建搜索按钮?
【发布时间】:2011-05-16 15:53:35
【问题描述】:

我正在使用 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>

【问题讨论】:

  • 没有代码显示。另外,您到底在寻找什么?是列中的文本与搜索字符串匹配的行吗?
  • 对不起,我开始展示代码,但由于它超过 200 行,我认为最好只获取 URL/提示并自己浏览代码。但是,如果对您有帮助,我很乐意发布此代码!我正在搜索列中的文本与搜索字符串匹配的行。
  • 另外,我希望我可以使用 DataGridView,但我认为我的版本不支持这个。只是较旧的 DataGrid。
  • 好的,我刚刚添加了我的 ASPX 代码

标签: asp.net vb.net search datagrid


【解决方案1】:

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

使用类似的查询创建数据源

select * from search table where searchcolumn like @Searchstring  

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

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

【讨论】:

    猜你喜欢
    • 2020-02-27
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多