以编程方式绑定 Gridview 中的标头
我有一个用代码构建的数据表,它可以动态地具有任何给定数量的列。我必须通过基于第一列添加新的 TableRow 对 Gridview 中的行进行分组。此要求意味着我无法使用 AutoGenerateColumns。我在 ItemTemplate 中创建了一个表,用于将行/单元格绑定到 ItemDatabound 事件中的列。这工作正常并且可以完成我需要做的事情。我遇到的问题是我无法将标题绑定到列的名称。当我检查 RowType 是 ItemDatabound 上的标头时,我没有可用的 e.Row.DataItem,因为它从第一个数据行开始。因此,我无法检查我想要打印到该列的标题单元格的 ColumnName 是什么。
这是我在 DataRow 上使用的代码(代码位于 Delphi.net 中,但如果您也使用 c# 或 VB,您应该能够获得 gyst):
if (e.Row.RowType = DataControlRowType.DataRow) then begin
if panlViewAllResults.Visible then begin
for i := 0 to lDataRow.DataView.Table.Columns.Count - 1 do begin
if Assigned(lDataRow.Item[i]) then begin
lCell := TableCell.Create;
lCell.Text := lDataRow.Item[i].ToString;
lRow1.Controls.Add(lCell);
end;
end;
lTable.Controls.Add(lRow1);
end;
end;
我想我可以对标题执行类似的操作。但显然不是:
if (e.Row.RowType = DataControlRowType.Header) then begin
for i := 0 to lDataRow.DataView.Table.Columns.Count - 1 do begin
if Assigned(lDataRow.Item[i]) then begin
lCell := TableCell.Create;
lCell.Text := lDataRow.DataView.Table.Columns[i].ColumnName;
end;
end;
end;
希望有人能对此有所启发。至少可以说我有点卡住了。
TIA 劳埃德
I have a datatable being built in code which can dynamically have any given number of columns. I have to group the rows within the Gridview by adding a new TableRow based on the first column. This requirement means I can't use the AutoGenerateColumns. I've create a table within an ItemTemplate which I'm using to bind the rows/cells against the columns in the ItemDatabound event. This works fine and does what I need to do. The problem I have is that I can't bind my header to the name of the columns. When I check the RowType is a Header on the ItemDatabound I don't have an e.Row.DataItem available because this starts on the 1st data row. As a result I can't check what the ColumnName is with which I want to print to the header cell of that column.
Here is the code I'm using on the DataRow (code is in Delphi.net but you should be able to get the gyst if you use c# or VB too):
if (e.Row.RowType = DataControlRowType.DataRow) then begin
if panlViewAllResults.Visible then begin
for i := 0 to lDataRow.DataView.Table.Columns.Count - 1 do begin
if Assigned(lDataRow.Item[i]) then begin
lCell := TableCell.Create;
lCell.Text := lDataRow.Item[i].ToString;
lRow1.Controls.Add(lCell);
end;
end;
lTable.Controls.Add(lRow1);
end;
end;
I thought I could do something like this for the header. But apparantly not:
if (e.Row.RowType = DataControlRowType.Header) then begin
for i := 0 to lDataRow.DataView.Table.Columns.Count - 1 do begin
if Assigned(lDataRow.Item[i]) then begin
lCell := TableCell.Create;
lCell.Text := lDataRow.DataView.Table.Columns[i].ColumnName;
end;
end;
end;
Hoping someone can throw some light on this. I'm a little stuck to say the least.
TIA
Lloyd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 OnDataBound 事件中执行此逻辑。您需要循环遍历所有行,并且此时您将可以访问所需的所有数据。
You could do this logic in the event OnDataBound. You'll need to loop through all your rows and you will have access to all the data you need at this point.