VB.NET Function for display Icon in cell Datagridview
Step 1 : Create Column Datagridview
'set column name
datagridview1.Columns.Add("qsymbol", "Symbol")
datagridview1.Columns.Add("qname", "Name")
datagridview1.Columns.Add("qprevclose", "Prev. Close")
datagridview1.Columns.Add("qopen", "Open")
datagridview1.Columns.Add("qdhigh", "High")
datagridview1.Columns.Add("qdlow", "Low")
CreateImgColumn(datagridview1, "Sign", 12)
datagridview1.Columns.Add("qchange", "Change")
Step 2 : set value to "Sign" Column
For Each row As DataRow In pdtdata.Rows
'--- data rows
n = datagridview1.Rows.Add()
datagridview1.Rows(n).Cells("qsymbol").Value = row.Item("qsymbol")
datagridview1.Rows(n).Cells("qname").Value = row.Item("qname")
datagridview1.Rows(n).Cells("qprevclose").Value = row.Item("qprevclose")
datagridview1.Rows(n).Cells("qopen").Value = row.Item("qopen")
datagridview1.Rows(n).Cells("qdhigh").Value = row.Item("qdhigh")
datagridview1.Rows(n).Cells("qdlow").Value = row.Item("qdlow")
datagridview1.Rows(n).Cells("qchange").Value = row.Item("qchange")
datagridview1.Rows(n).Cells("qlastprice").Value = row.Item("qlastprice")
datagridview1.Rows(n).Cells("qvolume").Value = row.Item("qvolume")
datagridview1.Rows(n).Cells("qdatetime").Value = row.Item("qdatetime")
SetImg(n, "Sign", row.Item("qchange"), datagridview1)
Step 3 : Put Functon code in common module
Sub CreateImgColumn(ByRef datagridview1 As DataGridView, ByVal ColName As String, ByVal pwidth As Integer)
Dim img As New DataGridViewImageColumn()
datagridview1.Columns.Add(img)
img.HeaderText = ColName
img.Name = ColName
img.ImageLayout = DataGridViewImageCellLayout.Zoom
datagridview1.Columns(ColName).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
datagridview1.Columns(ColName).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
datagridview1.Columns(ColName).Width = pwidth
End Sub
Sub SetImg(ByVal nRow As Integer, ByVal pColName As String, ByVal pmode As Double, ByRef datagridview1 As DataGridView)
Select Case pmode
Case 0
datagridview1.Rows(nRow).Cells(pColName).Value = My.Resources.icon_dot
Case Is > 0
datagridview1.Rows(nRow).Cells(pColName).Value = My.Resources.icon_up
Case Is < 0
datagridview1.Rows(nRow).Cells(pColName).Value = My.Resources.icon_down
End Select
End Sub
No comments:
Post a Comment