datagrid取列名问题

Tom_ruanbb 2006-12-15 03:22:19
自动生成列的,怎么取列名
...全文
107 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eray 2006-12-15
  • 打赏
  • 举报
回复
补充一下,如果想动态为列设定格式,需要一个数组来定义每列的格式的信息。参考代码(VB):
Dim i As Integer
'头部分
If e.Item.ItemType = ListItemType.Header Then

Formats = New String(e.Item.Cells.Count - 1) {}
For i = 0 To e.Item.Cells.Count - 1
Formats(i) = "M"'初始格式化信息,避免为空
Next
For i = 0 To e.Item.Cells.Count - 1
If e.Item.Cells(i).Text = "CS" Then
Formats(i) = "0.00"
e.Item.Cells(i).Text = "平均车速"
ElseIf e.Item.Cells(i).Text = "CSL" Then
e.Item.Cells(i).Text = "车数"
ElseIf e.Item.Cells(i).Text = "JZ" Then
e.Item.Cells(i).Text = "净重"
ElseIf e.Item.Cells(i).Text = "LB" Then
e.Item.Cells(i).Text = "类别"
Formats(i) = "T"
ElseIf e.Item.Cells(i).Text = "SHDWNAME" Then
e.Item.Cells(i).Text = "收货单位"
End If
Next
'内容部分
ElseIf e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim j As Integer
For j = 0 To e.Item.Cells.Count - 1
If Formats(j) = "T" Then
If Trim(e.Item.Cells(j).Text) = "J" Then e.Item.Cells(j).Text = "购进"
If Trim(e.Item.Cells(j).Text) = "C" Then e.Item.Cells(j).Text = "售出"
End If
If Formats(j) = "0.00" Then
e.Item.Cells(j).Text = CType(e.Item.Cells(j).Text, Double).ToString(Formats(j))
End If

Next
End If
Eray 2006-12-15
  • 打赏
  • 举报
回复
在DataGrid的ItemDatabound事件中,添加如下代码:
If e.Item.ItemType = ListItemType.Header Then
str=e.Item.Cells(i).Text
End if

第一行是判断是否为头
如果是,取出这个头的名字。
同理还有:
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
...
End IF
这个是内容项(当然需要名括交替项)

有这两个判断,不仅可以动态取出列名,还可以根据列名,动态的翻译列名,动态翻译内容,动态为列设定数据格式等等...
aaajedll 2006-12-15
  • 打赏
  • 举报
回复
同意楼上的做法(不错的建议)
如果你想实现动态,也不是不可能,但是有点难度!
califord 2006-12-15
  • 打赏
  • 举报
回复
你可以在程序里定义好

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧