2014-06-06 62 views
0

這可能是一個重複的問題,但我沒有找到任何可以幫助我的解決方案。所以再單獨詢問。如果有人向我提供任何解決方案,我將不勝感激如何在數據GridView中選擇一行並填充另一個GridView?

我想顯示一個數據gridview,並選擇任何數據的行,另一個gridview將填充使用該選定的行值。

 <div> 
     <table align="center" cellpadding="1" cellspacing="1" width="950px"> 
      <tr> 
       <td> 
        <asp:GridView ID="GridView1" runat="server"></asp:GridView> 
       </td> 
       <td> 
        <asp:GridView ID="GridView2" runat="server"></asp:GridView> 
       </td> 
      </tr> 
     </table> 
    </div> 

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
    GetAccounts() 
End Sub 

Protected Sub GetAccounts() 
    Dim objCommand As SqlCommand 
    Dim objAdapter As SqlDataAdapter 
    Dim objDataSet As DataSet 
    sqlString = "SELECT * FROM hanger" 

    objCommand = New SqlCommand(sqlString, conn) 
    objAdapter = New SqlDataAdapter(objCommand) 
    objDataSet = New DataSet() 
    objAdapter.Fill(objDataSet) 
    GridView1.DataSource = objDataSet 
    GridView1.DataBind() 

    If conn.State = ConnectionState.Open Then : conn.Close() : End If 
End Sub 
+0

其中,試圖做的代碼你問什麼? – Plutonix

+0

@Plutonix我這樣做是第一次,所以不知道從哪裏開始!對於那個很抱歉。 – saz

+0

OnSelectRowCommand獲取當前選定的行詳細信息,使用這些詳細信息創建表並將此表設置爲第二個網格的DataSource – malkam

回答

1

在這裏,我給你一個非常通用的解決方案,你可以從這裏得到想法並實現你自己的代碼。

ASP標記

虛擬LinkBut​​ton的使用,使ASP.Net __doPostBack的JavaScript功能被渲染爲我們將要求它使行通過提高回傳點擊。

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" 
    runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" 
    OnSelectedIndexChanged="OnSelectedIndexChanged"> 
    <Columns> 
     <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> 
     <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" /> 
    </Columns> 
</asp:GridView> 
<asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton> 

代碼隱藏

頁面加載事件處理程序

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
    If Not Me.IsPostBack Then 
     Dim dt As New DataTable() 
     dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")}) 
     dt.Rows.Add(1, "John Hammond", "United States") 
     dt.Rows.Add(2, "Mudassar Khan", "India") 
     dt.Rows.Add(3, "Suzanne Mathews", "France") 
     dt.Rows.Add(4, "Robert Schidner", "Russia") 
     GridView1.DataSource = dt 
     GridView1.DataBind() 
    End If 
End Sub 

OnRowDataBound事件漢dler中GridView1

Protected Sub OnRowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     e.Row.Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" & e.Row.RowIndex) 
     e.Row.Attributes("style") = "cursor:pointer" 
    End If 
End Sub 

OnSelectedIndexChanged事件處理程序的GridView1

Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs) 
    Dim index As Integer = GridView1.SelectedRow.RowIndex 
    Dim name As String = GridView1.SelectedRow.Cells(0).Text 
    Dim country As String = GridView1.SelectedRow.Cells(1).Text 

    //Extract the Row Identifier And Bind The Second GridView Here 
End Sub 
相關問題