2011-05-16 38 views
0

我使用的是Visual Studio 2008 Shell和SQL Server 2008.目前我能夠顯示DataGrid。但現在我有一個新的要求,我必須在其中一列啓用文本搜索。我怎樣才能實現這個?我嘗試實施我在網上找到的解決方案,但我仍然收到錯誤。請溫柔;我是新手VB.Net程序員!如何在DataGrid上創建搜索按鈕?

由於這是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> 
+0

沒有代碼顯示。你究竟在尋找什麼?行中的文本是否與搜索字符串匹配? – Chad 2011-05-16 16:01:37

+0

對不起,我開始顯示代碼,但由於它大於200行,我認爲最好只是獲取URL /提示並自行通過代碼。不過,如果它對您有幫助,我會很樂意發佈此代碼!我正在搜索列中的文本與搜索字符串匹配的行。 – salvationishere 2011-05-16 16:04:26

+0

另外,我希望我可以使用DataGridView,但我不認爲我的版本支持這一點。只是舊的DataGrid。 – salvationishere 2011-05-16 16:05:50

回答

0

您可能需要創建一個GlobalFunctions.GlobalF.FillSparePartsTable(1, SearchString)函數重載將接受搜索字符串作爲參數。

創建與查詢數據源等

select * from search table where searchcolumn like @Searchstring 

和百分比(%)sybmol追加到參數的任一側。

然後在您的搜索按鈕onclick事件中它調用新函數並綁定結果。