1
我設置列表有2個元素到DataGridView的DataSource,但它總有一個行,行數屬性爲1.這太奇怪了的WinForm的DataGridView只有一行,雖然數據源有兩個或更多的行
我下面的代碼有一些我必須解釋的實體類名。
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";
}
}
}
}
請告訴我們您的代碼,因爲這個問題是不夠清楚? – arman1991