0

I'm trying to programatically build a DataGrid from 2 different sources of data. I have a List and a DataGrid. The problem isn't with my data processing, It's to do with the values of the DataGridViewRow object. Here is my code:

    protected void buildGrid()
    {
        dgResults.Columns.Add( "sku", "SKU" );
        dgResults.Columns.Add( "itemID", "Item ID" );
        dgResults.Columns.Add( "productName", "Product Name" );
        dgResults.Columns.Add( "eBayQty", "eBay Qty" );
        dgResults.Columns.Add( "stockQty", "Stock Qty" );
        dgResults.Columns.Add( "difference", "Difference" );

        //Add the eBayItem data to the table
        foreach ( string[] eBayItem in ebayItems )
        {
            string SKU = eBayItem[1].ToString();
            int eBayQty = Convert.ToInt32(eBayItem[2]);
            string ProductName = "";
            int stockQty = 0;
            int qtyDifference = 0;

            DataRow[] rows = dbData.Select( "sku ='" + SKU + "'" );
            if (rows.Length == 1) {
                stockQty = Convert.ToInt32( rows[0]["quantity"] );
                ProductName = rows[0]["ProductName"].ToString();
            }
            qtyDifference = stockQty - eBayQty;

            DataGridViewRow dgvr = new DataGridViewRow();
            dgvr.SetValues( SKU, eBayItem[0].ToString(), ProductName, eBayQty, stockQty, qtyDifference );

            if ( qtyDifference != 0 || eBayQty > stockQty )
            {
                dgvr.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
                dgvr.DefaultCellStyle.ForeColor = System.Drawing.Color.White;
            }
            else if ( stockQty > eBayQty )
                dgvr.DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
            else
                dgvr.DefaultCellStyle.BackColor = System.Drawing.Color.GreenYellow;

            dgResults.Rows.Add(dgvr);
        }
    }

The rows are adding to the DataGrid and they are being colored appropriately, but each cell within the rows contain no data? All I end up with is several blank rows that have their background properties set.

Any one got any ideas?

Thanks in advance.

Dan
  • 533
  • 2
  • 6
  • 21
  • possible duplicate of [Why can't I see the DataGridViewRow added to a DataGridView?](http://stackoverflow.com/questions/5698356/why-cant-i-see-the-datagridviewrow-added-to-a-datagridview) – Jordy van Eijk Feb 22 '13 at 10:51
  • Yes, the answer marked as the solution in that question was what I was looking for. I'll post the answer to this specifically as soon as I am able to. – Dan Feb 22 '13 at 11:21
  • I'll add a way cleaner method when i get chance. – Derek Feb 22 '13 at 12:05

2 Answers2

0

Try using BeginEdit and EndEdit on the DataGridViewRow

Jordy van Eijk
  • 2,718
  • 2
  • 19
  • 37
  • I don't have BeginEdit and EndEdit methods available to me on the DataGridViewRow object. I do however on the DataGridView object though, and after trying it, it didn't work. – Dan Feb 22 '13 at 11:07
  • Nvm, I found the solution. I'll post it as soon as I am able to. – Dan Feb 22 '13 at 11:20
0

Here's my take on a solution for doing something like this.

For Demo Purpose I build an EbayItem Class :-

public class EbayItem
    {

        public EbayItem()
        {

        }

        public EbayItem(string sku, int id, string product, int ebayqty, int stockqty)
        {
            SKU = sku;
            ID = id;
            ProductName = product;
            ebayQty = ebayqty;
            stockQty = stockqty;

        }
        public string SKU { get; set; }
        public int ID { get; set; }
        public string ProductName { get; set; }
        public int ebayQty { get; set; }
        public int stockQty { get; set; }
        public int difference
        {

            get { return stockQty - ebayQty; }
            set { difference =  value; }
        }

    }

Inside a Windows Form(Form1_Load) I created a few test entries and added them to a List object. The last entry looks to buy 2 grenades, but there's zero in stock :-

List<EbayItem> Inventory = new List<EbayItem>();

Inventory.Add(new EbayItem("SKU1", 1, "Ski-Mask", 1, 10));
Inventory.Add(new EbayItem("SKU2", 2, "Shotgun", 1, 10));
Inventory.Add(new EbayItem("SKU3", 3, "Rounds", 5, 10));
Inventory.Add(new EbayItem("SKU4", 4, "Grenade", 2, 0));

Instead of adding rows manually to my DataGridView, I'm going to create a BindingSource and populate it with my Inventory :-

BindingSource bs = new BindingSource(Inventory, null);

Next, Assign the datasource for my DGV, setting it to the Binding object. This means that any changes made to the list object ripple through to the DGV automatically :-

dataGridView1.DataSource = bs;

All thats left now is to utilise the CellFormatting event handle of the DGV. I use this to highlight any rows where the difference in stocks is zero or less :-

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            int colIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;

            if (rowIndex >= 0 && colIndex >= 0)
            {
                DataGridViewRow theRow = dataGridView1.Rows[rowIndex];

                int difference = (int)theRow.Cells[5].Value;

                if (difference <= 0)
                {
                    theRow.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
                    theRow.DefaultCellStyle.ForeColor = System.Drawing.Color.White;
                }
            }

        }

Hope this helps.

Derek
  • 8,300
  • 12
  • 56
  • 88
  • Great solution. I really like the databinding as opposed to manually trying to build the table. – Dan Feb 26 '13 at 20:03