private void button1_Click(object sender, EventArgs e) { try { OpenFileDialog dialog = new OpenFileDialog(); dialog.ShowDialog(); int ImportedRecord = 0, inValidItem = 0; string SourceURl = ""; if (dialog.FileName != "") { if (dialog.FileName.EndsWith(".csv")) { DataTable dtNew = new DataTable(); dtNew = CSVHelper.GetDataTabletFromCSVFile(dialog.FileName); if (Convert.ToString(dtNew.Columns[0]).ToLower() != "barcode") { MessageBox.Show("Invalid Items File"); btnSave.Enabled = false; return; } txtFile.Text = dialog.SafeFileName; SourceURl = dialog.FileName; if (dtNew.Rows != null && dtNew.Rows.ToString() != String.Empty) { dgItems.DataSource = dtNew; } foreach (DataGridViewRow row in dgItems.Rows) { if (Convert.ToString(row.Cells["barcode"].Value) == "" || row.Cells["ten san pham"].Value == null) { row.DefaultCellStyle.BackColor = Color.Red; inValidItem += 1; } else { ImportedRecord += 1; } } if (dgItems.Rows.Count == 0) { btnSave.Enabled = false; MessageBox.Show("Không đọc được dữ liệu trong file", "XH POS", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show("Vui lòng chọn file csv.", "XH POS", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } catch (Exception ex) { MessageBox.Show("Exception " + ex); } }
private void ShowDataToGridView() { string query = "Select `products`.`id`,`name`,`description`,`expiration_date`, `manufacturer`, `pricemanagement`.`price` from `products` join `pricemanagement` on `products`.`id` = `pricemanagement`.`productid`"; DataTable dt = new DataTable(); DataTable dtnew = new DataTable(); dtnew.Columns.Add("Barcode"); dtnew.Columns.Add("Ten San Pham"); dtnew.Columns.Add("Mo ta"); dtnew.Columns.Add("Don gia"); dtnew.Columns.Add("Han su dung"); dtnew.Columns.Add("Nha san xuat"); if (DBAccess.IsServerConnected()) { dtnew = DBAccess.FillDataTable(query, dt); } else { dtnew = CSVHelper.GetDataTabletFromCSVFile(pathCSV); } dataGridView1.DataSource = dtnew; }