WinForm DataGridView 只有一行,而 DataSource 有两行或更多行

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

我给dataGridView的DataSource设置了一个有2个元素的List,但是它总是只有一行,RowCount 属性是1。这太奇怪了

我下面的代码有一些实体 class 名称,我必须解释一下。

HoaDon is Order
ChiTietHoaDon is OrderDetail
SanPham is Product

代码:

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";
            }
        }
    }
}

这是因为您正在使用 List<ChiTietHoaDon> 作为 datagridview 的数据源,它不会通知更改,因此网格不会显示新项目。

您应该改用 BindingList<ChiTietHoaDon>

但是如果你想继续使用List<ChiTietHoaDon>,更新网格你可以先设置dgrChiTiet.DataSource=null;然后dgrChiTiet.DataSource = allChiTiet;