2016-03-08 69 views
0

我是這裏的新成員。我想要問一些事。祝大家回答我的問題。對不起,我的英語不好。我來自印度尼西亞。 我有兩個單選按鈕,兩個文本框,兩個datagridview和一個按鈕。該場景是我的第一個文本框是由InvoiceNumber輸入,第二個文本框由AccountNumber輸入。我的問題是當我通過Invoicenumber輸入,然後單擊按鈕搜索時,將顯示Invoicenumber的datagridview,當我通過Accountnumber輸入Accounrnumber的datagridview時,將顯示。這是我的代碼:控制按鈕到Datagridview

Default.aspx.vb:(下面更新爲卡特里的Piyush正確顯示代碼)

Protected Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click 
    periode = cbbulan.SelectedValue.ToString + Microsoft.VisualBasic.Right(cbtahun.Text, 2).ToString 
    If txtseacrh.Text Then 
     Me.BindGrid(txtseacrh.Text, periode) 
     tampildata() 
     Label3.Text = "Consignee : " 
     Label4.Text = "Address : " 
     Label5.Text = "Product :" 
    End If 
    Me.BindGrid2(txtsearch2.Text) 
End Sub 

Private Sub BindGrid(RefNo As String, periode As String) 
    Dim constr As String = ConfigurationManager.ConnectionStrings("dbCon").ConnectionString 
    Using con As New SqlConnection(constr) 
     Using cmd As New SqlCommand() 

      cmd.CommandText = "Declare @awb As varchar(50) 
           Select @awb=awb from msdetail md inner join msdata mt On mt.PuNo=md.Puno 
           where md.Puno='" & RefNo & "' 
           Select '1' as no, Convert(varchar(12), pudate, 103) As Date,CONVERT(varchar(8), pudate,8) as time,'PU' AS status,'' as Recipient, '' as Remaks,'' as PIC from MsDetail where [email protected] 
           And Periode='" & periode & "' 
           union 
           Select '2' as no, CONVERT(varchar(12),pudatein,103) as date,CONVERT(varchar(8), pudatein,8) as time,'DE' AS status,'' as Recipient, '' as Remaks, IdUser as PIC from MsDetail where [email protected] 
           And Periode='" & periode & "' 
           UNION 
           Select '3' as no, CONVERT(varchar(12),stdate,103) as date,CONVERT(varchar(8), stdate,8) as time,StStt AS status,StPenerima as Recipient,StRel as Remaks, st.IdUser as PIC from status st 
           inner Join MsDetail md on md.AWB=st.AWB where [email protected] And Periode='" & periode & "' order by no" 
      cmd.Connection = con 

      Dim dt As New DataTable() 
      Using sda As New SqlDataAdapter(cmd) 
       sda.Fill(dt) 
       GridView1.DataSource = dt 
       GridView1.DataBind() 
      End Using 
     End Using 
    End Using 
End Sub 

Private Sub BindGrid2(Puno As String) 
    Dim constr As String = ConfigurationManager.ConnectionStrings("dbCon").ConnectionString 
    Using con As New SqlConnection(constr) 
     Using cmd As New SqlCommand() 

      cmd.CommandText = "select Refno,Field1,(CONVERT (varchar(10),PuDate,103)) as PuDate,(Convert (varchar(10),Pudatein,103))as Pudatein,mdcompany,MdAdd1,mdadd2,mdadd3,mdadd4,StStt,(CONVERT (varchar(10),StDate,103)) as StDate,StPenerima,StRel from MsDetail inner join MsData on MsDetail.PuNo=MsData.PuNo 
           left join Status on MsDetail.AWB=Status.AWB where MsData.Refno='" & Puno & "'" 
      cmd.Connection = con 

      Dim dt As New DataTable() 
      Using sda As New SqlDataAdapter(cmd) 
       sda.Fill(dt) 
       GridView2.DataSource = dt 
       GridView2.DataBind() 
      End Using 
     End Using 
    End Using 
End Sub 

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
    If Not Page.IsPostBack Then 
     'Fill Years 
     For i As Integer = Year(Now) - 1 To Year(Now) 
      cbtahun.Items.Add(i.ToString()) 
     Next 
     cbtahun.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = True 'set current year as selected 

    End If 
End Sub 

Protected Sub cbbulan_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbbulan.SelectedIndexChanged 
    'Session["cbbulan"]=DropDownList.SelectedValue; 
End Sub 
Private Sub koneksi() 
    strconn = WebConfigurationManager.ConnectionStrings("dbCon").ConnectionString 
    MyCn = New SqlConnection(strconn) 
    If MyCn.State <> Data.ConnectionState.Closed Then MyCn.Close() 
    MyCn.Open() 
End Sub 
Private Sub tampildata() 
    koneksi() 
    Dim sql As String 
    Dim cmd As SqlCommand 
    Dim dread As SqlDataReader 

    ' Dim constr As String = ConfigurationManager.ConnectionStrings("a").ConnectionString 

    sql = "select prname,MdName,isnull(MdAdd1,'') + ' ' + isnull(MdAdd2,'') + ' ' + isnull(MdAdd3,'') as ALAMAT from MsData inner join MsDetail on MsDetail.PuNo=MsData.PuNo 
      Left Join Produk on MsDetail.Code=Produk.Code where msdata.PuNo='" & txtseacrh.Text & "'" 
      cmd = New SqlCommand(Sql, MyCn) 
      dread = cmd.ExecuteReader 
      If dread.HasRows Then 
       While dread.Read = True 
        Label1.Text = dread.Item("mdName").ToString 
        Label2.Text = dread.Item("ALAMAT").ToString 
        Label6.Text = dread.Item("PrName").ToString 
       End While 
       dread.Close() 
      End If 
End Sub 

回答

0

嘗試是這樣的

HTML代碼(.aspx頁)

<div> 
     <asp:TextBox ID="txtInvoice" runat="server" /> 
     <asp:TextBox ID="txtAccountNo" runat="server" /> 
     <asp:Button id="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /> 
    </div> 

    <div> 
     <asp:GridView ID="gv" runat="server" AutoGenerateColumns="true" />    
    </div> 

**Code Behind** 

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    if (!string.IsNullOrEmpty(txtInvoice.Text.Trim())) { 
     gv.DataSource = getInvoiceDetails(txtInvoice.Text); 
     gv.DataBind(); 
    } 
    else if (!string.IsNullOrEmpty(txtAccountNo.Text.Trim())) 
    { 
     gv.DataSource = getAccountDetails(txtInvoice.Text); 
     gv.DataBind(); 
    } 
} 

private DataTable getInvoiceDetails(string invoiceNo) { 
    DataTable dt = new DataTable(); 

    using (SqlConnection con = new SqlConnection(connectionString)) { 
     using (SqlCommand cmd = new SqlCommand("YourQueryToGetInvoiceDetails")) { 
      SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
      adapter.Fill(dt); 
      return dt; 
     } 
    }  
} 

private DataTable getAccountDetails(string invoiceNo) 
{ 
    DataTable dt = new DataTable(); 
    using (SqlConnection con = new SqlConnection(connectionString)) 
    { 
     using (SqlCommand cmd = new SqlCommand("YourQueryToGetAmountDetails")) 
     { 
      SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
      adapter.Fill(dt); 
      return dt; 
     } 
    } 
}