简体   繁体   中英

How to create a Search button on a DataGrid?

I am using Visual Studio 2008 Shell and SQL Server 2008. Currently I am able to display DataGrid. But now I have a new requirement where I have to enable a text search in one of the columns. How can I implement this? I tried implementing a solution I found online, but I'm still getting errors. Please be gentle; I am a novice VB.Net programmer!

And since this is Shell edition and source data is on a different machine which I do not have direct access to, I am not able to debug at all.

Below is all of my code. Note that currently I cannot even view it as an ASPX page since there are still a couple of errors. Can you either give me a URL or some simple tips for me to follow as I develop this? Thanks!

Here is my code below:

<%@ 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>

You will probably need to create a function overload for GlobalFunctions.GlobalF.FillSparePartsTable(1, SearchString) that will accept the search string as an argument.

Create the datasource with a query like

select * from search table where searchcolumn like @Searchstring  

and append a Percent(%) sybmol to either side of the parameter.

Then in your search button onclick event have it call the new function and bind the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM