开发者

filtering the records in the second grid according to the selection in the first in Asp.Net GridView

开发者 https://www.devze.com 2023-02-11 01:54 出处:网络
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 i

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消