【发布时间】:2014-03-18 08:32:02
【问题描述】:
在 Web 窗体 ASP.NET 应用程序中,我有一个存储过程,它连接两个表,如下所示:
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
因此,使用 ID、描述和连接到部门的 ServiceChannel 选择部门。
我在类型化数据集中使用的这个存储过程带有一个名为Department 的TableAdapter,它的方法是GetDepartmentsWithServiceChannels:
static public DepartmentDataTable GetDepartmentsWithServiceChannels()
{
using (DepartmentTableAdapter departmentTA = new DepartmentTableAdapter())
{
return departmentTA.GetDepartmentsWithServiceChannels();
}
}
我在视图中使用此方法在代码隐藏中绑定DepartmentCollection,如下所示:
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();
}
MyGridvIew 如下所示:
<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>
很遗憾,return departmentTA.GetDepartmentsWithServiceChannels() 部分返回以下错误:
输入字符串的格式不正确。无法存储 在 ServiceChannel 列中。预期类型是 Int32。
如何让GridView 显示外键的字符串值?
【问题讨论】:
-
使用读取器通过循环读取值,您通过应用查询为gridview获取该值,将结果存储在数据表中并将结果循环到其中并添加数据行并将结果一一存储在其中,然后将数据表与网格视图
标签: c# asp.net tsql gridview foreign-keys