private void initListView(ThucUong[] arrThucUong) { this.lstViewDSThucUong.Clear(); ListViewItem[] lstViewItem = new ListViewItem[arrThucUong.Length]; for (int i = 0; i < arrThucUong.Length; i++) { ThucUong mThucUong = arrThucUong[i]; ListViewItem item = new ListViewItem(mThucUong.MSHH); item.SubItems.Add(mThucUong.TenHang); item.SubItems.Add(mThucUong.Gia.ToString()); item.SubItems.Add(mThucUong.TinhTrang.ToString()); lstViewItem[i] = item; } // Create columns for the items and subitems. // Width of -2 indicates auto-size. this.lstViewDSThucUong.Columns.Add("MSHH", -2, HorizontalAlignment.Left); this.lstViewDSThucUong.Columns.Add("Ten Hang", -2, HorizontalAlignment.Left); this.lstViewDSThucUong.Columns.Add("Gia", -2, HorizontalAlignment.Left); this.lstViewDSThucUong.Columns.Add("Tinh trang", -2, HorizontalAlignment.Center); //Add the items to the ListView. this.lstViewDSThucUong.Items.AddRange(lstViewItem); this.lstViewDSThucUong.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent); this.lstViewDSThucUong.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.ColumnContent); this.lstViewDSThucUong.AutoResizeColumn(2, ColumnHeaderAutoResizeStyle.ColumnContent); this.lstViewDSThucUong.AutoResizeColumn(3, ColumnHeaderAutoResizeStyle.HeaderSize); }
public ThucUong[] LietKeThucUong() { ThucUong[] arrThuocUong = new ThucUong[0]; // Provide the query string with a parameter placeholder. string queryString = "SELECT * From ThucUong"; // Create and open the connection in a using block. This // ensures that all resources will be closed and disposed // when the code exits. using (SqlConnection connection = new SqlConnection(connectionString)) { // Create the Command and Parameter objects. SqlCommand command = new SqlCommand(queryString, connection); // Open the connection in a try/catch block. // Create and execute the DataReader, writing the result // set to the console window. try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); int soLuongDong = this.LietKeSoLuongThucUong(); // Console.WriteLine("soLuongDong: " + soLuongDong); arrThuocUong = new ThucUong[soLuongDong]; // int soLuongDong = reader.VisibleFieldCount; // Console.WriteLine("soLuongDong: " + soLuongDong); int i = 0; while (reader.Read()) { // Parse data to model ThucUong mThucUong = new ThucUong(); mThucUong.MSHH = reader[0].ToString(); mThucUong.TenHang = reader[1].ToString(); mThucUong.Gia = int.Parse(reader[2].ToString()); mThucUong.TinhTrang = bool.Parse(reader[3].ToString()); arrThuocUong[i] = mThucUong; i++; } reader.Close(); } catch (Exception ex) { //Console.WriteLine(ex.Message); } // Console.ReadLine(); return(arrThuocUong); } }