title: How to display value of foreign key in GridView?
body: <p>In a Web Forms ASP.NET application I have a stored procedure that joins two tables as follows:</p>
  
  <pre><code>CREATE PROCEDURE dbo.usp_DepartmentsServiceChannelsSelect
  AS
      SET NOCOUNT ON
  SELECT     d.ID, d.Description,  s.ServiceChannel
  FROM         Departments d
  INNER JOIN [ServiceChannels] s
  ON s.ID = d.ServiceChannel
  
  GO
  </code></pre>
  
  <p>So the Departments get selected with ID, Description and a ServiceChannel that is connected to a department gets returned as well.</p>
  
  <p>This stored procedure I use in a Typed Dataset with a <code>TableAdapter</code> called <code>Department</code> that has the method <code>GetDepartmentsWithServiceChannels</code>:</p>
  
  <pre><code>static public DepartmentDataTable GetDepartmentsWithServiceChannels()
  {
      using (DepartmentTableAdapter departmentTA = new DepartmentTableAdapter())
      {
          return departmentTA.GetDepartmentsWithServiceChannels();
      }
  }
  </code></pre>
  
  <p>This method I use in a view to bind a <code>DepartmentCollection</code> in the code-behind as follows:</p>
  
  <pre><code>    private void BindDepartmentsAfterSorting(string sortexpression, SortDirection 
                sortDirection)
      {
          DepartmentCollection deptCollection = 
                ServiceInterfaceRegistry.DepartmentManager.GetDepartments(false);
  
          if (deptCollection != null)
          {
              Common.Comparer&lt;Department&gt; objcmp = new Common.Comparer&lt;Department&gt;();
              objcmp.SortClasses.Add(new SortClass(sortexpression, sortDirection));
              deptCollection.Sort(objcmp);
          }
          MyGridView.DataSource = deptCollection;
          MyGridView.DataBind();
      }
  </code></pre>
  
  <p>Where <code>MyGridvIew</code> looks as follows:</p>
  
  <pre><code>&lt;asp:GridView ID="MyGridView" runat="server" DataKeyNames="ID" AutoGenerateColumns="False" Width="1000px"
              AllowSorting="True" AllowPaging="True" EmptyDataText="Geen afdeling gevonden." OnRowDataBound="AfdelingGridView_RowDataBound" OnRowDeleting="AfdelingGridView_RowDeleting"  OnRowEditing="MyGridView_RowEditing" OnPageIndexChanging="AfdelingGridView_PageIndexChanging" OnSorting="AfdelingGridView_Sorting"&gt;
              &lt;Columns&gt;
                  &lt;asp:ButtonField ButtonType="Button" Text="Delete/edit" CommandName="Edit"&gt;
                      &lt;ItemStyle Width="20px" /&gt;
                  &lt;/asp:ButtonField&gt;
                  &lt;asp:TemplateField Visible ="False"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Button ID="Delete" runat="server" CommandName="Delete"  
             Text="Verwijderen" Font-Bold="false" /&gt;
                          &lt;/ItemTemplate&gt; 
                          &lt;ItemStyle Width="20px" /&gt;  
                    &lt;/asp:TemplateField&gt;
                  &lt;asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" SortExpression="Description" /&gt;
                  &lt;asp:BoundField DataField="ServiceChannel" HeaderText="Service Channel" ReadOnly="True"  /&gt;
  
              &lt;/Columns&gt;
              &lt;HeaderStyle HorizontalAlign="Left" /&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Unfortunately, the   <code>return departmentTA.GetDepartmentsWithServiceChannels()</code> part returns the following error:</p>
  
  <blockquote>
    <p>Input string was not in a correct format.Couldn't store 
    in ServiceChannel Column. Expected type is Int32.</p>
  </blockquote>
  
  <p>How can I make the <code>GridView</code> do display the string value of the foreign key?</p>