Small question about Grid View !


New member
Jul 28, 2012
New Delhi
Programming Experience

I have a inventory management application, that maintain the records of stock in the database,
the question is that whenever end user wants to view the stocks the detail data come in data grid view
and i want if the particular product have minimum amount..the product line should be show in red color ..and display a message please reorder the product
and like other products if they have sufficient amount of product they should be show in blue color ,
Would you please provide me the code..i will be very thankful ..
There would be other variations but here's one that works:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
	const int minStockColumnIndex = 2;

	const int actualStockColumnIndex = 3;

	private void Form1_Load(System.Object sender, System.EventArgs e)
		DataTable table = new DataTable();

		var _with1 = table.Columns;
		_with1.Add("ID", typeof(int));
		_with1.Add("Name", typeof(string));
		_with1.Add("MinimumStockLevel", typeof(int));
		_with1.Add("ActualStockLevel", typeof(int));

		var _with2 = table.Rows;
		_with2.Add(1, "Product 1", 10, 20);
		_with2.Add(2, "Product 2", 10, 5);
		_with2.Add(3, "Product 3", 10, 20);
		_with2.Add(4, "Product 4", 10, 5);

		DataGridView1.DataSource = table;

		//Set the initial background colour for all rows.
		foreach (DataGridViewRow row in DataGridView1.Rows) {
			if (!row.IsNewRow) {

	private void DataGridView1_CellValueChanged(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
		//Set the background colour for the current row when the stock level or threshold change.
		if (e.ColumnIndex == minStockColumnIndex || e.ColumnIndex == actualStockColumnIndex) {
			dynamic row = DataGridView1.Rows(e.RowIndex);


	private void SetStockBackgroundColor(DataGridViewRow row)
		dynamic cell = row.Cells(actualStockColumnIndex);
		dynamic minStockLevel = Convert.ToInt32(row.Cells(minStockColumnIndex).Value);
		dynamic actualStockLevel = Convert.ToInt32(cell.Value);

		if (actualStockLevel < minStockLevel) {
			cell.Style.BackColor = Color.Red;
		} else {
			cell.Style.BackColor = Color.Blue;
	public Form1()
		Load += Form1_Load;

If the code looks a bit funny it's because I accidentally wrote it in VB and had to convert it online.
Top Bottom