对与其他表相关的 GridView 列进行排序
我有一个绑定到 DataView 的 GridView。 DataView 表中的某些列是相关表(例如 Customer)的外键。我也想对这些列启用排序,但我所能做的就是对外键(fiCustomer)而不是 CustomerName 进行排序。
我尝试过但没有成功(“找不到列 ERP_Customer.CustomerName ”):
<asp:TemplateField HeaderText="Customer" SortExpression="ERP_Customer.CustomerName" >
也尝试了 DataViewManager,但我在检测要排序的表时遇到问题:
Dim daCharge As New ERPModel.dsERPTableAdapters.ERP_ChargeTableAdapter
daCharge.Fill(dsERP.ERP_Charge)
Dim viewManager As New DataViewManager(Me.dsERP)
viewManager.DataViewSettings(dsERP.ERP_Charge).RowFilter = filter
viewManager.DataViewSettings(dsERP.ERP_Charge).Sort = sort 'sort is the GridView's SortExpression
Me.GrdCharge.DataSource = viewManager.CreateDataView(dsERP.ERP_Charge)
我必须在 DataViewManager 的不同表上应用排序,但该表与相关表有所不同。 我已在 RowDataBound-Event fe 中的代码隐藏中绑定了 TemplateColumns:
Dim LblCustomer As Label = DirectCast(e.Row.FindControl("LblCustomer"), Label)
LblCustomer.Text = drCharge.ERP_CustomerRow.CustomerName 'drCharge inherits DataRow
在与其他表相关的列上对 GridView 进行排序的推荐方法是什么?我可以使用客户名而不是外键构建自定义数据表,并将此列绑定到 TemplateField。但模型中的庞大数据集就不再有意义了。
编辑: 看来我的问题不太清楚或者太特殊了。 也许我可以用更通用的术语重新表述它。 我有一个带有数据集的模型。我将一个数据表(ERP_Charge)从它绑定到我的GridView(实际上我从该表中获取一个数据视图)。此数据表中的列与数据集中的其他数据表相关(定义了关系)。当我想让网格可排序时,属于 ERP_Charge 的列没有问题。但是无法对具有其他表外键的列进行排序,因为 Gridview 显示的不是 CustomerID,而是 Customername。我在 RowDataBound 中获取了客户名称。 通常我会加入表格并为客户名称添加数据列。然后我将这个“虚拟”数据表设置为 Gridview 中的数据源,并且可以对其进行排序。但我不想在页面中动态创建数据表(它属于模型)。 我必须在数据集设计器中定义它吗?我认为定义数据集中的关系就足够了。
更新:我已经解决了我的排序问题,如下所示。
问候
i have a GridView bound to a DataView.
Some columns in the DataView's table are foreignkeys to related tables(f.e. Customer). I want to enable sorting for these columns too, but all i can do is sorting the foreignkey(fiCustomer) and not the CustomerName.
I have tried this without success(" Cannot find column ERP_Customer.CustomerName "):
<asp:TemplateField HeaderText="Customer" SortExpression="ERP_Customer.CustomerName" >
A tried also the DataViewManager, but i've a problem to detect the table to sort:
Dim daCharge As New ERPModel.dsERPTableAdapters.ERP_ChargeTableAdapter
daCharge.Fill(dsERP.ERP_Charge)
Dim viewManager As New DataViewManager(Me.dsERP)
viewManager.DataViewSettings(dsERP.ERP_Charge).RowFilter = filter
viewManager.DataViewSettings(dsERP.ERP_Charge).Sort = sort 'sort is the GridView's SortExpression
Me.GrdCharge.DataSource = viewManager.CreateDataView(dsERP.ERP_Charge)
I have to apply the sort on a distinct table of the DataViewManager, but this table would differ on the related tables.
I have bound the TemplateColumns in Codebehind in RowDataBound-Event f.e.:
Dim LblCustomer As Label = DirectCast(e.Row.FindControl("LblCustomer"), Label)
LblCustomer.Text = drCharge.ERP_CustomerRow.CustomerName 'drCharge inherits DataRow
What is the recommended way to sort a GridView on columns related to other tables? I could build a custom datatable with the customername instead of the foreignkey and bind this column to the TemplateField. But then my huge dataset in the model makes no sense anymore.
EDIT:
It seems that my question was not clear or too special.
Perhaps i can rephrase it in a more general term.
I have a model with a Dataset. I'm binding one Datatable(ERP_Charge) from it to my GridView(actually i take a Dataview from that Table). In this Datatable are columns that are related to other Datatables in the Dataset(relations are defined). When i want to make the grid sortable its no problem on the columns that belong to ERP_Charge. But the columns with foreign keys to other table could not be sorted because the Gridview shows f.e. not the CustomerID but the Customername. I get the Customername in RowDataBound.
Normally i would join the tables and add a Datacolumn for the Customername. Then i set this "virtual" Datatable as Datasource from the Gridview and i'm able to sort it. But i didnt want to create the datatables in the Page on the fly(it belongs into the model). Do i have to define it in the dataset-Designer? I thought it would be sufficient to define the relationships in the dataset.
UPDATE: i had solved my sorting problems as below.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:我通过以下方式解决了排序问题:我更改了模型中的数据集并将相关表加入到主表(ERP_Charge)中。我将列添加到我想要排序的数据表(fe CustomerName)中。但我这样做时没有将其保存在 DataAdapter 的配置向导中,因为这会删除所有 Update-、Delete- 和 InsertCommands。因此,我仅将 SelectCommand 更改为从向导生成的那个。因此,我保留了数据模型的优势,能够对相关表的列进行排序。由于连接的原因,现在查询可能会慢一些,但它对我有用。
UPDATE: i had solved my sorting problems in the following way: I changed my DataSet in the model and joined the related table to the Main-Table(ERP_Charge). I added the columns to the Datatable(f.e. CustomerName) that i want to sort. But i did that without saving it in the configuration wizard of the DataAdapter, because that would delete all Update-,Delete- and InsertCommands. Hence i changed only the SelectCommand with the one that was generated from the Wizard. Therefore i kept the benefit of my Datamodel with the ability to sort for columns of related tables. Maybe queries are a little slower now because of the joins but it works for me.