我如何得到客户名单到一个winforms应用程序

本文关键字:一个 winforms 应用程序 何得 客户名单 | 更新日期: 2023-09-27 18:18:31

我正试图从Customers.csMainForm.cs获得客户列表。是否有一种方法,从Customers.cs列表不使用sql服务器?我希望它在MainForm.cs上显示列名称colFirstName下的名字和列名称colLastName下的姓氏。我有一个代码,我使用MainForm.cs来获得sql客户列表,但我需要在不使用sql连接的情况下获得客户列表。

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);
}
}

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);
        }
    }

我如何得到客户名单到一个winforms应用程序

我不认为这是可能的获得列表没有连接到数据库,你正试图提取数据。

您可以使用dataAdapter和处于未连接模式的Dataset来执行此操作在你的数据集中,如果你不想修改数据库,你只能选择SELECT语句,然后选择Just Show Data to the User