C# 如何在GridView中显示外键的值?
在Web Forms ASP.NET应用程序中,我有一个将两个表连接在一起的存储过程,如下所示:C# 如何在GridView中显示外键的值?,c#,asp.net,tsql,gridview,foreign-keys,C#,Asp.net,Tsql,Gridview,Foreign Keys,在Web Forms 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.S
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
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();
}
<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>
因此,将使用ID、描述和连接到部门的ServiceChannel选择部门,并返回该部门
我在类型化数据集中使用的这个存储过程有一个名为Department
的TableAdapter
,它的方法是getDepartmentswithServiceChannel
:
static public DepartmentDataTable GetDepartmentsWithServiceChannels()
{
using (DepartmentTableAdapter departmentTA = new DepartmentTableAdapter())
{
return departmentTA.GetDepartmentsWithServiceChannels();
}
}
我在视图中使用此方法在代码隐藏中绑定部门集合
,如下所示:
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
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();
}
<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>
不幸的是,返回departmentTA.GetDepartmentswithServiceChannel()
部分返回以下错误:
输入字符串的格式不正确。无法存储
在ServiceChannel列中。应为Int32类型
如何使
GridView
显示外键的字符串值?解决方案是基于datatable使ServiceChannel
对象具有ID属性,并在GetDepartments
方法中匹配此对象的ID:
Department tmpDepartment = new Department(departmentRow);
tmpDepartment.ServiceChannel = channels.Where(c => c.Id ==
departmentRow.ServiceChannelID).FirstOrDefault();
然后,可以通过在视图上访问此属性
e.Row.Cells[e.Row.Cells.Count - 1].Text = ((Department)
e.Row.DataItem).ServiceChannel.Description;
使用reader-through循环读取值,通过应用查询为gridview获取该值,将该结果存储在datatable中并将结果循环到其中,然后逐个添加datarow并将结果存储在其中,然后将datatable与gridview绑定