这可能是一个重复的问题,但我没有找到任何有助于我的解决方案。所以再单独询问。如果有人向我提供任何解决方案,我将不胜感激。
我想显示数据网格视图,只选择该数据的任何行,并使用选定的行值填充另一个网格视图。
<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 :(得分:1)
在这里,我为您提供了一个非常通用的解决方案,您可以从这里获得想法并实现您自己的代码。
ASP加价
使用Dummy LinkButton,以便呈现ASP.Net __doPostBack JavaScript函数,因为我们需要它通过提高PostBack来生成Row Clickable。
<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
GridView1的OnRowDataBound事件处理程序
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
GridView1的OnSelectedIndexChanged事件处理程序
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