سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

Try using BeginEdit and EndEdit on the DataGridViewRow

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top