How to add images to DataGridView cell using C# and Windows Forms

There are many ways to skin this old cat. When choosing the way, you must first decide when should the adding of images take place. I chose to add images during sorting.

Let us assume you already added some data to gridview via datatable using ;

   1: dataGridView1.DataSource = siparis_tablo_guncel;

So now you know we have a filled up dataGridView.

Now let us assume in the dataGridView1 we want to add images according to the data within the 6th column. So what we first need to do is to create an image column by writing;

   1: DataGridViewImageColumn img = new DataGridViewImageColumn();

   2:  

   3: img.Name = "img";

   4: img.HeaderText = "Image Column";

   5: img.ValuesAreIcons = true;

   6:  

   7: dataGridView1.Columns.Add(img);

Now we have a image column. With the name “img” with the header “Image Column” As you can see in the 5th line I have stated that my images are actually icons. The reason I did this was that I found out that icons take up less space and flickers less. You can if you want change this option as you see fit.

Now let us get cracking and add those images according to the columns;

   1: int number_of_rows = dataGridView1.RowCount;

   2: for (int i = 0; i < number_of_rows; i++)

   3: {

   4:     if (dataGridView1.Rows[i].Cells[6].Value.ToString() == "true")

   5:     {

   6:         Icon image = SUT.Properties.Resources.succcess_icon;

   7:         dataGridView1.Rows[i].Cells["img"].Value = image;

   8:     }

   9:     else

  10:     {

  11:         Icon image = SUT.Properties.Resources.cancel_icon;

  12:         dataGridView1.Rows[i].Cells["img"].Value = image;

  13:     }

  14: }

 

I hope this has been informative. Please do tell if you see any mistakes and wish to make suggestions.

Have fun!

Fork me on GitHub

Installing node.js 0.6.2 on Fedora 16

 

The logo of the Node.js Project from the offic...

Image via Wikipedia

This one was a little hard. I got lots of help so I decided to post it online since it doesn’t exist on the internet yet… I wonder why….

The simple step by step instruction:

  1. sudo yum install git

  2. git clone --depth 1 git://github.com/joyent/node.git
    
  3. cd node
  4. git checkout v0.6.2
  5. sudo yum install openssl-devel
  6. sudo yum install gcc # you can skip this if you already have gcc
  7. sudo yum install gcc-c++ # you can skip this if you already have gcc-c++
  8. ./configure       [ thank you Ed for the heads up! ]
  9. make -j2
  10. make install
  11. export PATH=$PATH:/usr/local/bin:/usr/local #this is to add usr local to your path wher nodejs was installed
  12. sudo visudo
  13. Find Defaults secure_path=/sbin:/bin:/usr/sbin:/usr/bin
    go to the end “a” for append, type “:/usr/local/bin”, ESC, “:wq”
  14. curl http://npmjs.org/install.sh |sudo sh #installing npm a very good too to install nodejs packages
  15. sudo yum install mongodb #installing mongodb
  16. #go to the dir where you wish to code and use npm!!
  17. npm install mongodb
  18. npm install mongoose
  19. npm install express
  20. npm install coffee-script
  21. npm install stylus
  22. npm install underscore
  23. npm list
    /home/john/someproject
    ├── coffee-script@1.1.3
    ├─┬ express@2.5.1
    │ ├─┬ connect@1.8.0
    │ │ └── formidable@1.0.7
    │ ├── mime@1.2.4
    │ ├── mkdirp@0.0.7
    │ └── qs@0.3.2
    ├── mongodb@0.9.7-0
    ├─┬ mongoose@2.3.13
    │ ├── colors@0.5.1
    │ ├── hooks@0.1.9
    │ └── mongodb@0.9.6-23
    ├─┬ stylus@0.19.3
    │ ├── cssom@0.2.0
    │ ├── growl@1.1.0
    │ └── mkdirp@0.0.7
    └── underscore@1.2.2

Blinking lights MSP430 FG461x

Photo of two experimenter boards for the MSP43...

Image via Wikipedia

I have been working on MSP430 for sometime now. It is an TI (Texas Instruments ) chip. Very fun to play with. We are using a MSP430FG461x series experimental board. This little experimental board has all the bells and whistles one may need to create any application from a home automation controller to a simple step-motor driver. TI provides very good documentation for this grown up toy.

Our first lab homework for this baby was :

By using the MSP430 FG4618 Microcontroller, write a program that controls the LED #1, #2,#4.When we perpetually push the two buttons at the right bottom corner of the board, all ofthe LEDs turn on.When we push perpetually one of the buttons, only the LED #4 blinks, the others turn off.When we push no buttons, the LED #1 and #2 blinks complementarily and the LED#4 turnsoff.

My biggest problem with this exercise was to find what led was connected to what port. However in the end I figured it all out.

Below you will find the program :

 

 

#include <msp430xG46x.h>

void initPortPins(void);

void main(void)
{
	 WDTCTL=WDTPW + WDTHOLD;
	 initPortPins();
	 while(1) {
	 	if (P1IN == (0x00))
	 	{
			P2OUT |= 0x06;
			P5OUT |= 0x02;
		}
		else if (P1IN == 0x03)
		{
			P2OUT &= 0x00;
			P5OUT &= 0x00;
			P2OUT |= 0x02;
			__delay_cycles (40000);
			P2OUT &= 0x00;
			P2OUT |= 0x04;
			__delay_cycles (40000);
		}
		else
		{
			P2OUT &= 0x00;
			P5OUT &= 0x00;
			__delay_cycles (40000);
			P5OUT |= 0x02;
			__delay_cycles (40000);
		}
	 }
};

void initPortPins(void)
{
  P1DIR = 0x00;	// Set P2.2,1 as outputs
  P5DIR = 0x02; // Set P5.1 as output
  P2DIR = 0x06; // Set P2.1 to 1
};

Installing node.js and npm to a Fedora 14 PC

Hi! Below is the step by step instructions to install the following in a Fedora 14 PC; ( no sudo yum install shortcut :( )

+ npm
+ mongo database
node components
+ mongoose
+ express
+ coffeescript
+ stylus
+ underscore
Step1. Install nodejs : First download the latest nodejs and un-tar it. Than;
export JOBS=2 # optional, sets number of parallel commands.
./configure
make
make install

Step 2. Install npm :

sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf
git clone http://github.com/isaacs/npm.git
cd npm
sudo make install

Step 3. So now you have npm and nodejs. The next step is to install components;

sudo npm install mongoose
sudo npm install express
sudo npm install coffee-script
sudo npm install stylus
sudo npm install underscore

And thats all folks!! Now I have Nodejs and its components installed.

 

 

 

Image Capturing from WebCAM using OpenCV and Pygame in Python

I know there a lot of examples of WebCAM image capturing on the net. Mine is one of that but the main difference is that this little script here simply captures frames in a certain fps and simply saves those images. There a numerous usages fro such a thing. One usage could be a script that uploads this image to a certain ftp site so you can display it in your web page. I needed this little script to follow a moving object. I did not write the whole script. You may think this as a little upgrade from the one on the internet. The script uses OpenCV and Pygame libs. Without further ado the script :

import pygame
import Image
from pygame.locals import *
import sys

import opencv
import cv

#this is important for capturing/displaying images
from opencv import highgui

camera = highgui.cvCreateCameraCapture(0)
i=0
def get_image():
    im = highgui.cvQueryFrame(camera)
    # Add the line below if you need it (Ubuntu 8.04+)
    #im = opencv.cvGetMat(im)
    #convert Ipl image to PIL image
    return opencv.adaptors.Ipl2PIL(im)

fps = 25.0
pygame.init()
window = pygame.display.set_mode((640,480))
pygame.display.set_caption("WebCAM Demo")
screen = pygame.display.get_surface()

while True:
    events = pygame.event.get()
    for event in events:
        if event.type == QUIT or event.type == KEYDOWN:
            sys.exit(0)
    im = get_image()
    if i>100:
	#allowing the camera to focus
	#auto focus is really annoying
        im.save("image_"+str(i)+"", "JPEG")
    i=i+1
    pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)

    screen.blit(pg_img, (0,0))
    pygame.display.flip()
    pygame.time.delay(int(1000 * 1.0/fps))