1

How to add different colour icons/images automatic to the Row Header of a DataGridView based on database fill the DataTable VB.NET?

here I failed to apply the column "ColorIndex"

maybe something is wrong in the code implementation from me .

Full code is at the link below and according to the first answer option

Here's a link!

Thanks

 Public Sub jokenselect(ByVal sql As String)
        Try
            con.Open()
            With cmd
                .Connection = con
                .CommandText = sql
            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try
        con.Close()
        da.Dispose()
    End Sub
    Public Sub filltable(ByVal dtgrd As Object, ByVal design As String)

        Try
            da.SelectCommand = cmd
            da.Fill(publictable)
            dtgrd.DataSource = publictable
            Select Case design
                Case "Temp"
            End Select
            da.Dispose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information)
        End Try
    End Sub

    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        colors = GetType(Color).
            GetProperties(BindingFlags.Public Or BindingFlags.Static).
            Where(Function(pi) pi.PropertyType = GetType(Color)).
            Select(Function(pi) CType(pi.GetValue(GetType(Color), Nothing), Color)).
            Where(Function(c)
                      Return ((
                  c.R * 0.299F +
                  c.G * 0.587F +
                  c.B * 0.114F) / 256.0F) <= 0.5F
                  End Function).ToArray()
        bmp = My.Resources.money
        Dim Table1 = New DataTable("TableName")
        Table1.Columns.Add("ColorIndex", GetType(Integer))
        Dim dt = Table1.DefaultView.
    ToTable(False, Table1.Columns.
    Cast(Of DataColumn).
    Where(Function(c) c.ColumnName <> "ColorIndex").
    Select(Function(c) c.ColumnName).ToArray())
        da.Update(dt)
        DataGridView1.GetType().
    GetProperty("DoubleBuffered",
                BindingFlags.Instance Or BindingFlags.NonPublic).
    SetValue(DataGridView1, True)
        'DataGridView1.DataSource = Table1
        DataGridView1.Columns("ColorIndex").Visible = False
        jokenselect("Select * From tblPayrollDecision")
        filltable(DataGridView1, "Temp")
        UpdateColorColumn()
        Table1.AcceptChanges()
    End Sub

Object Reference not set to an instance of an object

roy
  • 693
  • 2
  • 11
  • If you read the referred link - carefully - then you should fill the `DataTable` from the database by calling `filltable(DataGridView1, "Temp")` before adding the additional column `Table1.Columns.Add("ColorIndex", GetType(Integer))`. The order is important. Also, you have the other two working options. Use one? – dr.null Jul 01 '23 at 07:59
  • @dr.null , Thank you for your reply. I have done as per your recommendation then `DataGridView1.DataSource = Table1 DataGridView1.Columns("ColorIndex").Visible = False` After that I made it like that only the "colorindex" column appeared without record – roy Jul 01 '23 at 08:08
  • 1
    @dr.null , `The order is important. Also, you have the other two working options.` Since this is very important if it is not important then I will not post with new questions. – roy Jul 01 '23 at 08:17
  • OK, since it's important then you did the right thing. Now, try and see how it goes. – dr.null Jul 01 '23 at 08:20
  • @dr.null , `Dim dt = Table1.DefaultView` for this code I do comment out then I position it `DataGridView1.DataSource = Table1` remains the same, only appears the column "colorindex" without record – roy Jul 01 '23 at 08:23
  • This `publictable` is your `DataTable` that you should deal with not `Table1`. So, remove this `Dim Table1 = New DataTable("TableName")` and add the column `publictable.Columns.Add("ColorIndex", GetType(Integer))` , – dr.null Jul 01 '23 at 08:29
  • @dr.null , Thank you the guide results from you went perfectly . I have something to ask you `DataGridView1.GetType(). GetProperty("DoubleBuffered", BindingFlags.Instance Or BindingFlags.NonPublic). SetValue(DataGridView1, True)` for this code what purpose and if I don't use it does not matter and for the position of the code I use more after the code `DataGridView1.DataSource = publictable` – roy Jul 01 '23 at 08:41
  • Call this line in the constructor ( `Sub New() ... End Sub` ). if you want to reduce the flickering. Especially when you hover the mouse over the row header or when you click it. Try with and without it to examine that yourself. Also note that, you probably don't need to do this `Dim dt = Table1.DefaultView.ToTable(...` The `UPDATE` command will ignore any additional columns and update the CommandText fields. Use that if you need to create a new `DataTable` that does not include the additional column(s). – dr.null Jul 01 '23 at 08:48

0 Answers0