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

  • Home
  • SEARCH
  • 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 7610937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:32:27+00:00 2026-05-31T01:32:27+00:00

Hi all I need help I want to loop trough the all cells in

  • 0

Hi all I need help I want to loop trough the all cells in datagrid view and I want to check all neighboring cells around it so I need to check all directions around cell.
That would be 8 directions (up,down,left,right,up-left,up-right,down-left,down-right)
And I want to paint it if they are neighbors with black. Both of them.

So here is what I got for now:

for (int x = 0; x < yourGridName.Rows.Count; x++)
{
    //Loop through all cells in that row and change its color.
    for (int y = 0; y < yourGridName.Rows[x].Cells.Count; y++)
    {
        if (dataGridView1.Rows[dataGridView1.SelectedRows[x].Index].Cells[y].Value.ToString() != "YES")
         yourGridName.Rows[x].Cells[y].Style.BackColor = 
         System.Drawing.Color.Black;
    }
  }
}
  • 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-05-31T01:32:29+00:00Added an answer on May 31, 2026 at 1:32 am

    What I understand you need to know how to get all the 8 neighbors for your X/Y grid cell.

    If you at grid cell [X][Y] then the 8 neighbors should be

    grid[X][Y-1] (if Y-1 >= 0)
    grid[X][Y+1] (if Y+1 < Cells.Count)
    grid[X-1][Y] (if X-1 >= 0)
    grid[X+1][Y] (if X+1 < Rows.Count)
    grid[X+1][Y+1] (if Y+1 < Cells.Count && X+1 < Rows.Count)
    grid[X-1][Y-1] (if Y-1 >= 0 && X-1 >= 0)

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

Sidebar

Related Questions

Need a little help with my jquery here I want all my button with
Hey all need a lil help sorting a loop for this table out, cant
I need help building a loop in my stored procedure, basically i want it
All i need help in downloading a file through headers its working fine but
Greetings all, I need help combining some htaccess rewrites, these crazy regular expressions screw
I need some help with SQL Query. I am trying to select all records
All, I need your help to merge the result in the same datetime in
We Really need some help on this one: We've Struggled with all the Apple
I need a little help creating a catch-all error handling page in my ICEfaces
Hey all i am in need of some help trying to figure out how

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.