我正试图从Customers.cs获取一个客户列表到MainForm.cs 。 有没有办法从Customers.cs获取列表而不使用sql server? 我希望它显示列名colFirstName下的名字和MainForm.cs colLastName名colLastName下的最后一个名字。 我有一个代码用于MainForm.cs以获取sql客户列表,但我需要在不使用sql连接的情况下获取客户列表。
Customers.cs代码
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClassProject2.Data { public class Customers { public Customers() { _customers = new List<Customer>() //Customer list { new Customer(_ids.Next()) { FirstName = "Bob", LastName = "Miller" }, new Customer(_ids.Next()) { FirstName = "Sue", LastName = "Storm" }, new Customer(_ids.Next()) { FirstName = "Peter", LastName = "Parker" } }; } public Customer Add ( Customer customer ) { if (customer == null) throw new ArgumentNullException(nameof(customer)); Validator.ValidateObject(customer, new ValidationContext(customer)); if (_customers.Any(c => String.Compare(c.FirstName, customer.FirstName, true) == 0 && String.Compare(c.LastName, customer.LastName, true) == 0)) throw new ArgumentException("Customer already exists.", nameof(customer)); customer.Id = _ids.Next(); _customers.Add(customer); return customer; } public Customer Get ( int id ) { return (from c in _customers where id == c.Id select c ).FirstOrDefault(); } public IEnumerable<Customer> GetAll () { return _customers; } public void Remove(int id) { //Verify > 0 if (id <= 0) throw new ArgumentOutOfRangeException("Id must be > 0", nameof(id)); //Find the item var item = _customers.FirstOrDefault(i => i.Id == id); //Remove it if (item != null) _customers.Remove(item); } private readonly List<Customer> _customers; private readonly Sequence _ids = new Sequence(100); } }我用于MainForm.cs代码
private void IsLinkClicked(MouseEventArgs e) { try { if (e.Button != MouseButtons.Left) return; DataGridView.HitTestInfo Hti = gridCustomers.HitTest(e.X, e.Y); if (Hti.Type == DataGridViewHitTestType.Cell) { if (gridCustomers.Rows.Count > 0) { gridCustomers.CurrentCell = gridCustomers[Hti.ColumnIndex, Hti.RowIndex]; } } if(Hti.ColumnIndex < 0 || Hti.ColumnIndex > gridCustomers.ColumnCount) { value = false; } CustomerForm cf = new CustomerForm(); cf.editMode = true; int rowIndex = gridCustomers.CurrentCell.RowIndex; cf.id = gridCustomers.Rows[rowIndex].Cells["colId"].Value.ToString(); cf.FName = gridCustomers.Rows[rowIndex].Cells["colFirstName"].Value.ToString(); cf.LName = gridCustomers.Rows[rowIndex].Cells["colLastName"].Value.ToString(); cf.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }I am trying to get a customer list from Customers.cs into MainForm.cs. Is there a way to get the list from Customers.cs without using a sql server? I want it to show the first name under the column name colFirstName and the last name under the column name colLastName on the MainForm.cs. I have a code that I used for MainForm.cs to get the sql customer list, but I am required to get the customer list without using a sql connection.
Customers.cs code
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClassProject2.Data { public class Customers { public Customers() { _customers = new List<Customer>() //Customer list { new Customer(_ids.Next()) { FirstName = "Bob", LastName = "Miller" }, new Customer(_ids.Next()) { FirstName = "Sue", LastName = "Storm" }, new Customer(_ids.Next()) { FirstName = "Peter", LastName = "Parker" } }; } public Customer Add ( Customer customer ) { if (customer == null) throw new ArgumentNullException(nameof(customer)); Validator.ValidateObject(customer, new ValidationContext(customer)); if (_customers.Any(c => String.Compare(c.FirstName, customer.FirstName, true) == 0 && String.Compare(c.LastName, customer.LastName, true) == 0)) throw new ArgumentException("Customer already exists.", nameof(customer)); customer.Id = _ids.Next(); _customers.Add(customer); return customer; } public Customer Get ( int id ) { return (from c in _customers where id == c.Id select c ).FirstOrDefault(); } public IEnumerable<Customer> GetAll () { return _customers; } public void Remove(int id) { //Verify > 0 if (id <= 0) throw new ArgumentOutOfRangeException("Id must be > 0", nameof(id)); //Find the item var item = _customers.FirstOrDefault(i => i.Id == id); //Remove it if (item != null) _customers.Remove(item); } private readonly List<Customer> _customers; private readonly Sequence _ids = new Sequence(100); } }Code that I used for MainForm.cs
private void IsLinkClicked(MouseEventArgs e) { try { if (e.Button != MouseButtons.Left) return; DataGridView.HitTestInfo Hti = gridCustomers.HitTest(e.X, e.Y); if (Hti.Type == DataGridViewHitTestType.Cell) { if (gridCustomers.Rows.Count > 0) { gridCustomers.CurrentCell = gridCustomers[Hti.ColumnIndex, Hti.RowIndex]; } } if(Hti.ColumnIndex < 0 || Hti.ColumnIndex > gridCustomers.ColumnCount) { value = false; } CustomerForm cf = new CustomerForm(); cf.editMode = true; int rowIndex = gridCustomers.CurrentCell.RowIndex; cf.id = gridCustomers.Rows[rowIndex].Cells["colId"].Value.ToString(); cf.FName = gridCustomers.Rows[rowIndex].Cells["colFirstName"].Value.ToString(); cf.LName = gridCustomers.Rows[rowIndex].Cells["colLastName"].Value.ToString(); cf.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }最满意答案
我不认为有可能在没有连接到数据库的情况下获取列表,而是尝试从中提取数据。
你可以使用dataAdapter和一个数据集来处理未连接模式在数据集中,如果你不想修改数据库和只显示数据给用户,你只能放置SELECT语句
I don't think it is possible to Get The list without having a connection to the database you're trying to pull data out of.
you can do it with dataAdapter and a Dataset to be in Unconnected Mode In your dataset you can only put SELECT statement if you don't want to modify the database and Just Show Data to the User
更多推荐
发布评论