c# - How do I get customer list into a winforms app? -
i trying customer list customers.cs
mainform.cs
. there way list customers.cs
without using sql server? want show first name under column name colfirstname
, last name under column name collastname
on mainform.cs
. have code used mainform.cs
sql
customer list, required customer list without using 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 exists.", nameof(customer)); customer.id = _ids.next(); _customers.add(customer); return customer; } public customer ( int id ) { return (from c in _customers 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 > 0", nameof(id)); //find item var item = _customers.firstordefault(i => i.id == id); //remove if (item != null) _customers.remove(item); } private readonly list<customer> _customers; private readonly sequence _ids = new sequence(100); } }
code used 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 don't think possible list without having connection database you're trying pull data out of.
you can dataadapter , dataset in unconnected mode in dataset can put select statement if don't want modify database , show data user
Comments
Post a Comment