'c# winform , How to get only available Computer accounts
I have account table with some accounts. Some of them are already assigned to a computer. Some of them are not - they are available.
Here is my class:
public class ComputerAccount
{
public int AccountId { get; set; }
public string AccountName { get; set; }
public int DepId { get; set; }
public int ComputerId { get; set; }
}
and my computer class:
public class Computer
{
public int ComputerId { get; set; }
public string ComputerName { get; set; }
public int DepId { get; set; }
public string AccountName { get; set; }
}
If, in the Account table, ComputerId == null, then I know this account is available.
I have a computer list in my DataGridView. When I want to update, I just CellClick to load another Update form.
I have Loadaccounts funtion in update form that displays all accounts in ComboBox.
What I want in ComboBox is to populate only those available accounts, plus current account, if the computer has an account. Otherwise only those which are available.
I can populate all computer ids where ComputerId == null, but this should include the current account. It does not display the current account. As previously mentioned, if the computer has an account I want to display it, plus the computer ids where ComputerId == null.
Here is what I am trying:
using (db)
{
var account = (from u in db.ComputerAccounts
where u.DepId == DepId && u.ComputerId == null
select new { Name = u.AccountName, Value = u.AccountId }).ToList();
account.Insert(0, new { Name = "Choose account", Value = -1 });
CmbAccounts.DisplayMember = "Name";
CmbAccounts.ValueMember = "Value";
CmbAccounts.DataSource = account;
}
But this does not populate current account because I'm using u.ComputerId == null. I have this because some Accounts do not have any computer bound. Thank you in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
