如何将客户列表添加到winforms应用程序中?

时间:2016-11-17 16:33:04

标签: c# winforms

我正在尝试将Customers.cs的客户列表转换为MainForm.cs。有没有办法从Customers.cs获取列表而不使用SQL Server?我希望它显示列名colFirstName下的名字和colLastName上列名MainForm.cs下的姓氏。我有一个用于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);
        }
    }

1 个答案:

答案 0 :(得分:0)

我不认为可以在没有连接到数据库的情况下获取列表,而是尝试从中提取数据。

您可以使用dataAdapter和数据集进行处于未连接模式 在您的数据集中,如果您不想修改数据库并且只是向用户显示数据,则只能放置SELECT语句