locked
Show data from another table when select a row in gridview RRS feed

  • Question

  • User-340993414 posted

    Hello,

    I have the following diagram :

    I want to show Contact (if exists) when selecting a row in servers.

    Here is my aspx code that show a list of servers :

    <asp:GridView ID="serversGV" runat="server"
    AutoGenerateColumns="False"
    DataKeyNames="id,service_id"
    DataSourceID="serversDS" AllowPaging="True"
    >
    <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
    <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
    <asp:BoundField DataField="service_id" HeaderText="service_id" SortExpression="service_id" />
    <asp:BoundField DataField="entite_id" HeaderText="entite_id" SortExpression="entite_id" />
    </Columns>
    </asp:GridView>
    <asp:EntityDataSource ID="serversDS" runat="server"
    ConnectionString="name=InventoryEntities"
    DefaultContainerName="InventoryEntities"
    EnableFlattening="False" EntitySetName="Servers"
    >
    </asp:EntityDataSource>

    Note that I use two keys in DataKeyNames to retrieve the service_id key.

    Here is my aspx code for EntityDataSource for Contacts :

    <asp:EntityDataSource ID="tagserviceEDS" runat="server" 
            ConnectionString="name=InventoryEntities" 
            DefaultContainerName="InventoryEntities" 
            EnableFlattening="False" 
            EntitySetName="Tag_Services"
            Include="Contact_Service.Contacts"
            AutoGenerateWhereClause="true"        
            OnSelected="tagserviceEDS_Selected">
            <WhereParameters>
                <asp:ControlParameter ControlID="serversGV" Name="id" PropertyName="SelectedDataKey.Values[service_id]" DbType="Int32" />
            </WhereParameters>
        </asp:EntityDataSource>

    I also insert a GridView :

    <asp:GridView ID="gv" runat="server" DataSourceID="tagserviceEDS">
            <Columns>
                <asp:BoundField DataField="Contacts.firstName" HeaderText="firstName" ReadOnly="true" />
            </Columns>
        </asp:GridView>

    With code behind :

    protected void tagserviceEDS_Selected(object sender, EntityDataSourceSelectedEventArgs e)
            {
                if (e.Results != null)
                {
                    var tagservice = e.Results.Cast<DAL.Tag_Services>();
                    var res = tagservice.First().Contact_Service;
    
                    if (res.Count > 0)
                    {
                        gv.DataSource = res.ToList();
                        gv.DataBind();
                    }
                }
            }

    Like that I receive an error that say something like the data in the second gridview cannot be modified during bind. (I'm french so I don't have the exact error in english).

    If I remove the DataSourceID in gridview gv, the event Select is not raised.

    What am I doing wrong?  How can I show contacts?

    Thanks for your help.

    Thursday, August 9, 2018 8:38 AM

All replies

  • User-893317190 posted

    Hi miniil02,

    It seems you want to change the datasource of your gridview gv when it has bound data of type  tag_services. It will cause error.
    I suggest that you could use gridview ‘s  OnSelectedIndexChanged event to help bind data of type contacts when the select index of gridview  changes.


    Below is my code.

    <asp:gridview runat="server" ID="gridview" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="EntityDataSource1" OnSelectedIndexChanged="gridview_SelectedIndexChanged" 
                    SelectedIndex="1"  autogenerateselectbutton="True" >
                    <Columns>
            
                        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                        <asp:BoundField DataField="Tag_servicesId" HeaderText="Tag_servicesId" SortExpression="Tag_servicesId" />
                    </Columns>
                    <selectedrowstyle backcolor="LightCyan"
             forecolor="DarkBlue"
             font-bold="true"/>  
    
                </asp:gridview>
                <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=Model1Container" DefaultContainerName="Model1Container" EnableFlattening="False" EntitySetName="Servers">
                </asp:EntityDataSource>
            </div>
    
    
    
            <asp:GridView ID="gv" runat="server"  AutoGenerateColumns="False" >
            <Columns>
           
               <asp:BoundField DataField="firstName" HeaderText="firstName" ReadOnly="true" />
            </Columns>
        </asp:GridView>
    

    Code behind.

    //the dbcontext object
    Model1Container container = new Model1Container();
            protected void Page_Load(object sender, EventArgs e)
            {
                Servers server = container.Servers.Find(1);
                List<Contacts> contacts = server.Tag_services1.Contact_Service.Select(cs => cs.Contact).ToList();
                gv.DataSource = contacts;
                gv.DataBind();
            }
    
    protected void gridview_SelectedIndexChanged(object sender,EventArgs e)
            {
                
               //get the id of selected server
                int b = (int)gridview.SelectedDataKey.Value;
              Servers server=  container.Servers.Find(b);
    
                // use linq to find all the contacts related to the selected server
               List<Contacts> contacts = server.Tag_services1.Contact_Service.Select(cs=>cs.Contact).ToList();
                gv.DataSource = contacts;
                gv.DataBind();
                
            }
    

    And the result.

    Best regards ,

    Ackerly Xu

    Friday, August 10, 2018 8:55 AM