如何将客户列表转换为Winforms应用程序?(How do I get customer list into a winforms app?)

编程入门 行业动态 更新时间:2024-10-25 05:22:09
如何将客户列表转换为Winforms应用程序?(How do I get customer list into a winforms app?)

我正试图从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

更多推荐

本文发布于:2023-08-03 09:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   应用程序   客户   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!