When i click on a row in gridview1, i want to go to gridview2 with the id i get from first grid.
How to get id from 开发者_JAVA百科gridview1 to gridview2 and gridview2 autobinding?
You should define in gridview markup your primary key for table that would be showed in master grid like here:
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False"
DataKeyNames="UserID" onrowcommand="gvUsers_RowCommand">
where "ID" - is the name of your actual primary key. And add some button for selecting purpose like here:
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="SELECT" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
Then in code behind create RowCommand event handler in the way like here:
protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
Control ctl = e.CommandSource as Control;
GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
object objTemp = gvUsers.DataKeys[CurrentRow.RowIndex].Value as object;
if (objTemp != null)
{
string id = objTemp.ToString();
//Do your operations
}
}
When you find your Master id you can load Detail gridview.
精彩评论