Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:36:22+00:00 2026-06-09T21:36:22+00:00

Below is my code and I have been trying to color the cells but

  • 0

Below is my code and I have been trying to color the cells but not sure how to use the “Style” property when my source for grid is a DB. I am new to this so having difficulties getting started.

Some website or pointers would help a lot.

I want to be able to color some background cells or also color some rows or specific columns…Basically everything color related. How can I do this with my current snippet? Also a link to where I can learn more would be appreciated.

LarsTech I am trying to add you to a chat but I dont have enough rep so I think I can’t contact you due to that.

Imports System.Data.SqlClient
Imports System.Collections.Generic

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim connectionString As String = "data source=SQst; database=MVar2; User ID=Wepp; Password=2010Live; Integrated Security=false;"

        Dim sql As String = "SELECT * FROM Prer"

        Dim connection As New SqlConnection(connectionString)

        Dim dataadapter As New SqlDataAdapter(sql, connection)

        Dim ds As New DataSet()

        connection.Open()

        dataadapter.Fill(ds, "Authors_table")

        connection.Close()

        DataGridView1.DataSource = ds

        DataGridView1.DataMember = "Authors_table"
    **DataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.PaleGreen
    DataGridView1.Rows[3].Cells[1].Style.BackColor = Color.Red**    

    End Sub



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Call PrintDGV.Print_DataGridView(DataGridView1)

    End Sub





End Class

Errors I get:

Error   1   Property access must assign to the property or use its value.   
Error   2   Identifier expected.    
Error   3   Property access must assign to the property or use its value.   
Error   4   Identifier expected.    

I tried:
DataGridView1.Rows(0).Cell(0).Style.BackColor = Color.Red

and I got 1 error:

Error   1   'Cell' is not a member of 'System.Windows.Forms.DataGridViewRow'.   

EDIT: After looking more around the web I got to color selected cells using below code:

DataGridView1.Item(4, 5).Style.BackColor = Color.Red

This however doesn’t color rows or columns so I am still looking to make those things work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:36:23+00:00Added an answer on June 9, 2026 at 9:36 pm

    There are a few different ways of doing this. I beliebe that currentcell refers to the current active cell, so coloring based on that after filling your grid will yield no results.

    A few ways to do this are:

    c#:
    For rows/cells:
    dataGridView1.Rows[RowNumber].DefaultCellStyle.BackColor = Color.PaleGreen;
    dataGridView1.Rows[RowNumber].Cells[1].Style.BackColor = Color.Red;
    
    for columns:
    dataGridView1.Columns[ColumnNumber].DefaultCellStyle.BackColor = Color.Black;
    
    
    VB:
    DataGridView1.Rows(0).DefaultCellStyle.BackColor = Color.Green
    DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.Blue
    

    Both of these will apply color to the cell background. Use some logic in order to control how/which cells/rows are being colored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is the code I have been trying to use to delete a row
I have been trying to access the below code DECLARE N_DEPTNO DEPT.DEPTNO %TYPE :=&DEPT_NUM;
Good evening, I have been trying to wrap my head about this, below, code
I have below code to insert a style into DOM (there is a use
I have been trying to create a directory in /sdcard programmatically, but it's not
I have been trying to test this out. I put in the code below
In my example code below, you can see that I have been trying to
Hi all I have been trying to fix this but could not fix, the
I have below code: <a href=# id=@item.Id name=vote ><img src=/Content/images/021.png style=float:left alt= /></a> which
I have been trying with limited success to code a JSF application. In one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.