简体   繁体   中英

WinForm DataGridView have only one row although DataSource have two or more rows

I set a List have 2 elements to the dataGridView's DataSource, but it's always have one row, the RowCount property is 1. This is so strange

My code below have some Entity class name which I must explain.

HoaDon is Order
ChiTietHoaDon is OrderDetail
SanPham is Product

Code:

using GymFitnessOlympic.Controller;
using GymFitnessOlympic.Models;
using GymFitnessOlympic.Models.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GymFitnessOlympic.View.ActForm
{
    public partial class FrmBanHang2 : Form
    {
        List<SanPham> allSanPham;
        List<ChiTietHoaDon> allChiTiet = new List<ChiTietHoaDon>();
        HoaDon hoaDon = new HoaDon();

        public FrmBanHang2()
        {
            InitializeComponent();
            int phongID = Login.GetPhongHienTai().MaPhongTap;
            allSanPham = SanPhamController.GetList(phongID);
            dgrChiTiet.AutoGenerateColumns = false;
            dgrChiTiet.AutoGenerateColumns = false;
            loadListSanPham(allSanPham);
        }

        void loadListSanPham(List<SanPham> li)
        {

            lbSanPham.DataSource = li;
        }

        void updateTable()
        {
            dgrChiTiet.DataSource = allChiTiet;
            int n = dgrChiTiet.RowCount;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (spnQuantity.Value <= 0)
            {
                dxErrorProvider1.SetError(spnQuantity, "Quantity must be greater than 0");
                spnQuantity.Focus();
                return;
            }
            int gia;
            if (!int.TryParse(txtPrice.Text, out gia) || (gia % 500 != 0) || gia <= 0)
            {
                txtPrice.Focus();
                dxErrorProvider1.SetError(txtPrice, "Invalid Price");
                return;
            }
            if (lbSanPham.SelectedItem != null)
            {
                var sp = (SanPham)lbSanPham.SelectedItem;
                int soLuong = int.Parse(spnQuantity.Text);
                ChiTietHoaDon c = new ChiTietHoaDon()
                {
                    Gia = gia,
                    MaSanPham = sp.MaSanPham,
                    SoLuong = soLuong,
                    SanPham = sp

                };
                var old = allChiTiet.FirstOrDefault(c1 => c1.MaSanPham == c.MaSanPham);
                if (old != null)
                {
                    old.SoLuong += c.SoLuong;
                }
                else
                {
                    allChiTiet.Add(c);
                }
            }
            updateTable();
        }

        private void lbSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbSanPham.SelectedItem != null)
            {
                var sp = (SanPham)lbSanPham.SelectedItem;
                txtTenSanPham.Text
                     = sp.TenSanPham;
                txtPrice.Text = sp.Gia.ToString();
                spnQuantity.Text = "1";
            }
        }
    }
}

This is because you are using List<ChiTietHoaDon> as data source of your datagridview and it doesn't notify changes so the grid doesn't show new items.

You should use BindingList<ChiTietHoaDon> instead.

But if you want to continue using List<ChiTietHoaDon> , to update the grid you can first set dgrChiTiet.DataSource=null; and then dgrChiTiet.DataSource = allChiTiet;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM