Pergunta

In a Web Forms ASP.NET application I have a stored procedure that joins two tables as follows:

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

So the Departments get selected with ID, Description and a ServiceChannel that is connected to a department gets returned as well.

This stored procedure I use in a Typed Dataset with a TableAdapter called Department that has the method GetDepartmentsWithServiceChannels:

static public DepartmentDataTable GetDepartmentsWithServiceChannels()
{
    using (DepartmentTableAdapter departmentTA = new DepartmentTableAdapter())
    {
        return departmentTA.GetDepartmentsWithServiceChannels();
    }
}

This method I use in a view to bind a DepartmentCollection in the code-behind as follows:

    private void BindDepartmentsAfterSorting(string sortexpression, SortDirection 
              sortDirection)
    {
        DepartmentCollection deptCollection = 
              ServiceInterfaceRegistry.DepartmentManager.GetDepartments(false);

        if (deptCollection != null)
        {
            Common.Comparer<Department> objcmp = new Common.Comparer<Department>();
            objcmp.SortClasses.Add(new SortClass(sortexpression, sortDirection));
            deptCollection.Sort(objcmp);
        }
        MyGridView.DataSource = deptCollection;
        MyGridView.DataBind();
    }

Where MyGridvIew looks as follows:

<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">
            <Columns>
                <asp:ButtonField ButtonType="Button" Text="Delete/edit" CommandName="Edit">
                    <ItemStyle Width="20px" />
                </asp:ButtonField>
                <asp:TemplateField Visible ="False">
                    <ItemTemplate>
                        <asp:Button ID="Delete" runat="server" CommandName="Delete"  
           Text="Verwijderen" Font-Bold="false" />
                        </ItemTemplate> 
                        <ItemStyle Width="20px" />  
                  </asp:TemplateField>
                <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" SortExpression="Description" />
                <asp:BoundField DataField="ServiceChannel" HeaderText="Service Channel" ReadOnly="True"  />

            </Columns>
            <HeaderStyle HorizontalAlign="Left" />
</asp:GridView>

Unfortunately, the return departmentTA.GetDepartmentsWithServiceChannels() part returns the following error:

Input string was not in a correct format.Couldn't store in ServiceChannel Column. Expected type is Int32.

How can I make the GridView do display the string value of the foreign key?

Foi útil?

Solução

Solution was to make ServiceChannel object with ID property based on the datatable and match this object's ID in the GetDepartments method:

Department tmpDepartment = new Department(departmentRow);
            tmpDepartment.ServiceChannel = channels.Where(c => c.Id == 
                   departmentRow.ServiceChannelID).FirstOrDefault();

then this property can be accessed on the view via

e.Row.Cells[e.Row.Cells.Count - 1].Text = ((Department) 
                               e.Row.DataItem).ServiceChannel.Description;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top