DataGrid问题,TableStyle属性设置后无效
一个简单的程序,取出商品价格信息,在DataGrid中显示,我在TableStyle里面指定了每一列的标题,可是运行时,每一列的标题仍然是字段名.
后来想办法用代码实现,
dim mystyle As New DataGridTableStyle
dim TextCol As New DataGridTextBoxColumn
TextCol.MappingName = "iCode"
TextCol.HeaderText = "商品代码"
mystyle.GridColumnStyles.Add(objTextCol)
extCol = New DataGridTextBoxColumn
TextCol.MappingName = "iName"
TextCol.HeaderText = "商品名称"
mystyle.GridColumnStyles.Add(objTextCol)
......
运行时还是无效,郁闷求解!
问题点数:30、回复次数:6Top
1 楼lyj670(空中一只鸟)回复于 2005-01-23 14:24:02 得分 10
你少了几行代码。
给你一段我在用的。
Private Sub AddCustomDataTableStyle()
Dim w1 = DataGrid1.Width
Dim ts1 As DataGridTableStyle
ts1 = New DataGridTableStyle
ts1.MappingName = "tab"
ts1.RowHeadersVisible = False
Dim TextCol As DataGridTextBoxColumn
Dim ch As DataGridBoolColumn
TextCol = New DataGridTextBoxColumn
TextCol.MappingName = "id"
TextCol.HeaderText = "序号"
TextCol.Width = 0
AddHandler TextCol.TextBox.MouseDown, New MouseEventHandler(AddressOf TextBoxMouseDownHandler)
AddHandler TextCol.TextBox.DoubleClick, New EventHandler(AddressOf TextBoxDoubleClickHandler)
ts1.GridColumnStyles.Add(TextCol)
TextCol = New DataGridTextBoxColumn
TextCol.MappingName = "发件人姓名"
TextCol.HeaderText = "发件人"
TextCol.Width = 100
AddHandler TextCol.TextBox.MouseDown, New MouseEventHandler(AddressOf TextBoxMouseDownHandler)
AddHandler TextCol.TextBox.DoubleClick, New EventHandler(AddressOf TextBoxDoubleClickHandler)
ts1.GridColumnStyles.Add(TextCol)
DataGrid1.TableStyles.Add(ts1)
End SubTop
2 楼hamadou(闵峰--为了理想而奋斗)回复于 2005-01-23 14:40:51 得分 0
缺少mystyle的MappingName
和 datagrid的tablestyleTop
3 楼rack79(叫我以什马力)回复于 2005-01-23 18:12:18 得分 0
该加的代码都加了,没有,还有我是从视图取数据,这应该不会有影响吧Top
4 楼rack79(叫我以什马力)回复于 2005-01-24 22:23:48 得分 0
没人能回答了吗?Top
5 楼zwxrain(Lilo)回复于 2005-01-25 08:20:37 得分 10
看我的的這個代碼,希望對你有幫助...
datagrid 的樣式表(DataGridTableStyle)應用...
首先 我們先定一個 datatable 和 一個datarow
Private idtb_temp As New DataTable
Private idrw_row As DataRow
private sub GetDataTable()
idtb_temp.Columns.Add("prdodr_subodr_code") '''定義datatable 的列名
idtb_temp.TableName = "SearchTable"
Dim ldcl_header As Windows.Forms.DataGridTextBoxColumn
Dim ldgts_styles As New Windows.Forms.DataGridTableStyle
ldgts_styles.SelectionForeColor = System.Drawing.Color.Yellow
'''選中行的前景色,即字體顏色
ldgts_styles.SelectionBackColor = System.Drawing.Color.Brown '''選中行的背景色
ldgts_styles.ForeColor = System.Drawing.Color.Coral
''' datagrid 中將要顯示的字的顏色
ldgts_styles.AlternatingBackColor = System.Drawing.Color.Cyan
'''datagrid中奇數行所顯示的顏色
ldgts_styles.BackColor = System.Drawing.Color.Cyan
'''datagrid中偶數行所顯示的顏色
ldgts_styles.AllowSorting = False
'''些樣式表定義datagrid不允許自動排序..
ldgts_styles.MappingName = "SearchTable"
ldcl_header = New Windows.Forms.DataGridTextBoxColumn
'''實例化一個datagridtextboxcolumn
ldcl_header.MappingName = "prdodr_subodr_code"
'''引用前面定義的 “列名”
ldcl_header.HeaderText = "第一列"
'''datagrid 中顯示的 表列頭 文字
ldcl_header.ReadOnly = True '''些列設定為只讀
ldcl_header.TextBox.BorderStyle = BorderStyle.Fixed3D
ldcl_header.TextBox.ForeColor = System.Drawing.Color.Red
ldgts_styles.GridColumnStyles.Add(ldcl_header)
For i As Integer = 0 To 7
idrw_row = idtb_temp.NewRow
idrw_row.Item("prdodr_subodr_code") = "第" & i & "行"
idtb_temp.Rows.Add(idrw_row)
Next
idtb_temp.DefaultView.AllowNew = False
Me.DataGrid1.TableStyles.Add(ldgts_styles)
Me.DataGrid1.DataSource = idtb_temp
end sub
Top
6 楼landlordh(work wonders)回复于 2005-01-25 08:51:31 得分 10
'通用样式化Datagird
'Power by:landlordh
'调用:
' strHeaders() 里面改为自已需要的列名
' iWidths() 里面改为相对应的列宽
' inputnum 在样式化之前赋值
' style(DataGrid1, DataSet1.Tables("codes"))
'
'=================================================================
'列名,字段对应名
Public strHeaders() As String = {"编号", "时间", "主题", "内容"}
'列宽,为0隐藏(优先级低于inputnum),对应的列宽
Public iWidths() As Integer = {0, 70, 130, 0}
'显示几列,为空则显示所有,为0则隐藏所有
Public inputnum As String
Public Sub style(ByVal datagridname As DataGrid, ByVal dataname As DataTable)
Try
datagridname.DataSource = dataname
'dataname.DefaultView.AllowNew = False
'dataname.DefaultView.AllowEdit = False
Dim i As Integer
If Trim(inputnum) <> "" Then
Dim num As Integer
Try
num = CInt(Trim(inputnum))
Try
For i = 0 To iWidths.Length - 1
If i >= inputnum Then
iWidths(i) = 0
End If
Next
Catch ex As Exception
End Try
Catch ex As Exception
MsgBox("你输入的字符不符合要求")
End Try
End If
Dim ts As New DataGridTableStyle
ts.MappingName = dataname.TableName
ts.AlternatingBackColor = System.Drawing.Color.PapayaWhip
ts.BackColor = System.Drawing.Color.WhiteSmoke
ts.GridLineColor = System.Drawing.Color.Tan
ts.SelectionBackColor = System.Drawing.Color.LightSteelBlue
ts.SelectionForeColor = Color.Black
ts.RowHeaderWidth = 10
ts.PreferredRowHeight = 20
For i = 0 To dataname.Columns.Count - 1
Dim ac As New DataGridTextBoxColumn
'处理点击行
'AddHandler ac.TextBox.Enter, AddressOf TextBoxEnterHandler
ac.HeaderText = strHeaders(i)
ac.MappingName = dataname.Columns(i).ColumnName
ac.TextBox.Width = iWidths(i)
ts.PreferredColumnWidth = ac.TextBox.Width
'哪列,i=第几列数-1
If i = 1 Then
ac.Format = "yyyy-mm-dd hh:mm:ss"
'ac.ReadOnly = True
'ac.NullText = ""
ac.Alignment = HorizontalAlignment.Center
End If
If i = 1 Or i = 2 Then
'ac.ReadOnly = True
'ac.NullText = ""
ac.Alignment = HorizontalAlignment.Center
End If
ts.GridColumnStyles.Add(ac)
Next
datagridname.TableStyles.Clear()
datagridname.TableStyles.Add(ts)
Catch ex As Exception
MessageBox.Show("格式化网格出错了")
End Try
End Sub
Public Sub TextBoxEnterHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim inner As Control = sender
inner.Visible = False
If DataGrid1.CurrentRowIndex >= 0 Then
DataGrid1.Select(DataGrid1.CurrentRowIndex)
End If
'filltext()
End SubTop




