C# entity framework get data from combobox - c#

private void Submit_Click(object sender, EventArgs e)
{
ScoutContext db = new ScoutContext();
ScoutData cust = new ScoutData();
cust.FName = textBox1.Text;
cust.LName = textBox2.Text;
cust.FName = textBox3.Text;
cust.FaWork = textBox4.Text;
cust.MoName = textBox5.Text;
cust.MaWork = textBox6.Text;
cust.PlaceOfBirth = textBox7.Text;
cust.City = textBox8.Text;
cust.School = textBox9.Text;
cust.FaceBook = textBox10.Text;
cust.Phone = textBox11.Text;
cust.MPhone = textBox12.Text;
cust.IDNumber = textBox13.Text;
cust.NOfQaid = textBox14.Text;
cust.GroupID = ?????????????????
db.SaveChanges();
}
i work on Windows form, i have this data that the user fill the textbox, after that i need to save the data to my context ( database ), this is my code to insert data to my data base , but i have data ( numbers and some string ) the user will chose from ComboBox. i need to get this data and save it to a list of object , this is the code :
public class Groups
{
[Key]
public string GroupsID { set; get; }
public string NameOfGroup { set; get; }
***public virtual List<ScoutData> Members { set; get; }***
}
The context:
public class ScoutContext : DbContext
{
public ScoutContext()
: base("Scout")
{
// if (!Database.Exists("ScoutData"))
// Database.SetInitializer(new DropCreateDatabaseAlways<ScoutContext>());
}
public DbSet<ScoutData> ScoutDatas { set; get; }
public DbSet<Groups> GroupesScout { set; get; }
}
i need to get this data from the combobox to Members list and save it to a list of object (Members)

It depends on what you have in the combobox.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var combo = sender as ComboBox;
// If combobox has ScoutData then do this
var item = combo.SelectedItem as ScoutData;
// If combobox has something else then do this
var item2 = combo.SelectedItem as SomeThingElse;
var newScout = new ScoutData { FName = item2.FName /*, etc, etc */ };
// Then add it to your list
}

Related

How do I make it so that the values from the 'combobox' can be selected?

I work in Windows Forms. I have a combobox ('Departments' name) it contains a list of departments. By selecting a department in comboBox1(Staff), employees working in this department appear. But I can't select an employee, because they are not displayed
Code, filling in comboBox1(Staff). dataNames - dictionary(department name - array of employees)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (Departaments.SelectedItem != null)
{
this.Staff.Items.Clear();
var dataNames = DataForComdodox.ArrNames(Departaments.SelectedItem.ToString());
this.Staff.Items.AddRange(dataNames);
}
}
Code in Form1.Designer - comboBox1(Staff)
this.Staff.DrawMode = System.Windows.Forms.DrawMode.Normal;
this.Staff.FormattingEnabled = true;
this.Staff.Items.AddRange(new object[] {
});
this.Staff.Location = new System.Drawing.Point(374, 84);
this.Staff.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.Staff.Name = "Staff";
this.Staff.Size = new System.Drawing.Size(158, 21);
this.Staff.TabIndex = 0;
this.Staff.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
I tried to change Staff.SelectedIndex = 0, after I fill comboBox1(Staff) with values, but in the end, when choosing a department, an employee was selected automatically with an index of 0
If var dataNames = DataForComdodox.ArrNames(xx) returns a list or array of class employees, you could override the ToString() in the employee class.
public class Employee
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public string DeptName { get; set; }
public override string ToString()
{
return $"{this.EmpId}: {this.EmpName}";
}
}

How to add buttons in a DataGridView from a List<> C#?

Like the title says, I would like to be able to add buttons in a DataGridView from a List.
What I have so far:
public class Users
{
public Int64 UserID { get; set; }
public DataGridViewButtonColumn EmailAddress { get; set; }
public DataGridViewButtonColumn Gamertag { get; set; }
public DataGridViewButtonColumn Xuid { get; set; }
public String SignedIn { get; set; }
public String AutoSignin { get; set; }
public Image Gamerpic { get; set; }
}
private List<Users> GetUsersList()
{
try
{
List<Users> list = new List<Users>();
IEnumerable<XboxUser> users = _xbc.Users;
foreach (XboxUser user in users)
{
DataGridViewButtonColumn EmailAddress = new DataGridViewButtonColumn()
{
Text = user.EmailAddress
};
DataGridViewButtonColumn Gamertag = new DataGridViewButtonColumn()
{
Text = user.GamerTag
};
DataGridViewButtonColumn Xuid = new DataGridViewButtonColumn()
{
Text = user.Xuid
};
Users xbuser = new Users()
{
UserID = user.UserId,
EmailAddress = EmailAddress,
Gamertag = Gamertag,
Xuid = Xuid,
SignedIn = user.IsSignedIn.ToString(),
AutoSignin = user.AutoSignIn.ToString(),
Gamerpic = GetImageFromUrl(Gamertag.Text)
};
list.Add(xbuser);
}
return list;
}
catch (Exception ex)
{
}
return null;
}
private void Bo_Refresh_Click(object sender, EventArgs e)
{
List<Users> listUsers = GetUsersList();
Dg_UserList.DataSource = new BindingSource(new BindingList<Users>(listUsers), null);
}
private void Dg_UserList_CellClick(object sender, DataGridViewCellEventArgs e)
{
switch (e.ColumnIndex)
{
case 1:
Clipboard.SetText(Dg_UserList.Rows[e.RowIndex].Cells["EmailAddress"].Value.ToString());
MessageBox.Show("Email " + Dg_UserList.Rows[e.RowIndex].Cells["EmailAddress"].Value.ToString() + " has been copied in the clipboard.", "Copy to Clipboard", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case 2:
Clipboard.SetText(Dg_UserList.Rows[e.RowIndex].Cells["Gamertag"].Value.ToString());
MessageBox.Show("Gamertag " + Dg_UserList.Rows[e.RowIndex].Cells["Gamertag"].Value.ToString() + " has been copied in the clipboard.", "Copy to Clipboard", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case 3:
Clipboard.SetText(Dg_UserList.Rows[e.RowIndex].Cells["Xuid"].Value.ToString());
MessageBox.Show("Xuid " + Dg_UserList.Rows[e.RowIndex].Cells["Xuid"].Value.ToString() + " has been copied in the clipboard.", "Copy to Clipboard", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
Everything appears correctly (even the Image in Gamerpic) except the buttons, which show DataGridViewButtonColumn { Name=, Index=-1 } instead of a button with the right value on it.
I tried with DataGridVewButtonCell as well but does not work either.
However, when I click on a cell that is supposed to be a button, the Clipboard.SetText works but always put DataGridViewButtonColumn { Name=, Index=-1 } in it.
EDIT:
I have been able to do it with Strings instead... Looks less user friendly since there is no button to tell that you can click on it... but it works! FOr sure I would prefer buttons instead :)
Problem
When DataSourced, the DataGridView will auto-create columns based on the data type for each column. By default, each column here is created as a DataGridViewTextBoxColumn, except for the column bound to an Image - which will default to a DataGridViewImageColumn. Even though you have three DataGridViewButtonColumn properties, they will be bound to text columns - which will set the value as follows (example):
NewRow.Cells[1].Value = DataSourceObject.EmailAddress.ToString();
// I.E. EmailAddress is a DataGridViewButtonColumn, so...
NewRow.Cells[1].Value = DataGridViewButtonColumn.ToString();
Console.WriteLine(DataGridViewButtonColumn.ToString()); // prints "DataGridViewButtonColumn { Name=, Index=-1 }"
Solution
Start with the Users class: Change all the DatagridViewButtonColumn properties to type string.
public class Users
{
public Int64 UserID { get; set; }
public String EmailAddress { get; set; }
public String Gamertag { get; set; }
public String Xuid { get; set; }
public String SignedIn { get; set; }
public String AutoSignin { get; set; }
public Image Gamerpic { get; set; }
}
And change your GetUserList method to account for the differences:
private List<Users> GetUsersList()
{
try
{
List<Users> list = new List<Users>();
IEnumerable<XboxUser> users = _xbc.Users;
foreach (XboxUser user in users)
{
Users xbuser = new Users()
{
UserID = user.UserId,
EmailAddress = user.EmailAddress,
Gamertag = user.Gamertag,
Xuid = user.Xuid,
SignedIn = user.IsSignedIn.ToString(),
AutoSignin = user.AutoSignIn.ToString(),
Gamerpic = GetImageFromUrl(Gamertag.Text)
};
list.Add(xbuser);
}
return list;
}
catch (Exception ex)
{
}
return null;
}
And finally, in your Form constructor or Load event - before you've bound your data - manually add the columns like so:
Dg_UserList.AutoGenerateColumns = false;
DataGridViewTextBoxColumn userCol = new DataGridViewTextBoxColumn();
DataGridViewButtonColumn emailCol = new DataGridViewButtonColumn();
DataGridViewButtonColumn tagCol = new DataGridViewButtonColumn();
DataGridViewButtonColumn xuidCol = new DataGridViewButtonColumn();
DataGridViewTextBoxColumn signCol = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn autoCol = new DataGridViewTextBoxColumn();
DataGridViewImageColumn picCol = new DataGridViewImageColumn();
userCol.Name = "UserID"; // Allows access to columns or
emailCol.Name = "EmailAddress"; // cells in the following manner:
tagCol.Name = "Gamertag"; // dgv.Rows[index].Cells["Name"];
xuidCol.Name = "Xuid";
signCol.Name = "SignedIn";
autoCol.Name = "AutoSignin";
picCol.Name = "Gamerpic";
userCol.DataPropertyName = "UserID"; // MUST match DataSource properties.
emailCol.DataPropertyName = "EmailAddress"; // Allows DataSourced columns to match
tagCol.DataPropertyName = "Gamertag"; // up with manually created columns.
xuidCol.DataPropertyName = "Xuid";
signCol.DataPropertyName = "SignedIn";
autoCol.DataPropertyName = "AutoSignin";
picCol.DataPropertyName = "Gamerpic";
userCol.HeaderText = "User ID"; // Allows displaying different text
emailCol.HeaderText = "Email Address"; // for the column headers.
tagCol.HeaderText = "Gamer Tag";
xuidCol.HeaderText = "Xuid";
signCol.HeaderText = "Signed In";
autoCol.HeaderText = "Auto Sign in";
picCol.HeaderText = "Avatar";
Dg_UserList.Columns.Add(userCol);
Dg_UserList.Columns.Add(emailCol);
Dg_UserList.Columns.Add(tagCol);
Dg_UserList.Columns.Add(xuidCol);
Dg_UserList.Columns.Add(signCol);
Dg_UserList.Columns.Add(autoCol);
Dg_UserList.Columns.Add(picCol);
This will allow you to create your button columns but will still allow you to bind the data instead of manually adding each row. Thanks to DataPropertyName the button cells will correctly display the bound cell value.

How to select row from DataGridView with DB to a new DataGridView in C#

I have two DataGridViews,
datagridviewDoses All dishes from DB
datagridviewDoseOrder is empty
I would like as soon as I press the button line from datagridviewDoses.
The line will move to datagridviewDoseOrder.
The buttons I added, I can not move the line to -datagridviewDoseOrder.
private void AddColumn()
{
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.HeaderText = "בחר מנה";
buttonColumn.Name = "בחר מנה";
buttonColumn.Text = "בחר מנה";
buttonColumn.UseColumnTextForButtonValue = true;
dataGridViewDoses.Columns.Add(buttonColumn);
dataGridViewDoses.CellClick += new DataGridViewCellEventHandler(dataGridViewDoses_CellClick);
}
void dataGridViewDoses_CellClick(object sender, DataGridViewCellEventArgs e)
{
}
I was able to recreate your problem and solve it as following:
An example class as the data source for each dgview.
public class Example
{
public string Foo { get; set; }
public string Bar { get; set; }
}
The form displaying two DataGridViews.
public Form1()
{
InitializeComponent();
this.First = new BindingList<Example>()
{
new Example() { Foo = "foo1", Bar = "bar1" },
new Example() { Foo = "foo2", Bar = "bar2" }
};
this.Second = new BindingList<Example>();
this.dataGridView1.DataSource = this.First;
this.dataGridView2.DataSource = this.Second;
DataGridViewButtonColumn btnCol = new DataGridViewButtonColumn();
btnCol.HeaderText = "Transfer";
btnCol.Name = "Transfer";
btnCol.Text = "Transfer";
btnCol.UseColumnTextForButtonValue = true;
this.dataGridView1.Columns.Add(btnCol);
this.dataGridView1.CellClick += new DataGridViewCellEventHandler(col_Click);
}
public BindingList<Example> First { get; set; }
public BindingList<Example> Second { get; set; }
public void col_Click(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0) // The button column somehow is column 0, Foo is 1, and Bar is 2.
{
this.Second.Clear();
this.Second.Add(this.First[e.RowIndex]);
}
}
By clicking on a button, that row's data is moved over to the second DataGridView. I hope this helps.

DataGridColumn Change to ComboBox

I have list usrdetails that I'm using as my data source for my DataGridview.
Two questions:
How can I change the column type for the 3rd Column in my DataGridview from a DataGridTextBoxColumn to a DataGridComboBoxColumn?
Once I've changed the 3rd column to a ComboBox how would I populate with list of strings?
public class usrInfo
{
public string userID { get; set; }
public string username { get; set; }
public string group { get; set; }
public string seclev { get; set; }
public string isext { get; set; }
public usrInfo(string userID, string username, string group, string seclev, string isext)
{
this.userID = userID;
this.username = username;
this.group = group;
this.seclev = seclev;
this.isext = isext;
}
}
public static List<usrInfo> usrdetails = new List<usrInfo>();
private void Form5_Load(object sender, EventArgs e)
{
var comboColumn = new DataGridViewComboBoxColumn();
dataGridView1.DataSource = null;
dataGridView1.DataSource = usrdetails;
for (int i = 0; i < secgrps.Count; i++)
groups.Add(secgrps[i].ToString());
comboColumn.Name ="Security Group";
comboColumn.DataSource = groups;
dataGridView1.Columns.Insert(2, comboColumn);
usrdetails.Add(new usrInfo("domain\\userID", "User Name", "RIGSITE ONLY Wellsite Leader", "7", "Y"));
dataGridView1.Refresh();
if (usrdetails.Count > -1)
num_users = true;
}
You need to add a DataGridViewComboBoxColumn to your dataGridView1 control and assign its DataSource property to the collection of strings you want.
var myStringCollection = new[] {"String1", "String2", "String3"};
var comboColumn = new DataGridViewComboBoxColumn();
comboColumn.Name = "MyComboColumn";
comboColumn.DataSource = myStringCollection; //This sets the source of drop down items
Then insert it into your grid:
if (dataGridView1.Columns["MyComboColumn"] == null)
{
//The int value as first parameter of Insert() is the desired Column Index
dataGridView1.Columns.Insert(0, comboColumn);
}

How to not get repeated names appeared in the Combo Box c#

I encountered a problem where I can't seem to make the names in the Combo Box appear once instead of multiple one. Is there anything in my codes that causes this problem? Any help will be greatly appreciated.
Below is the code to link the names to the combo Box.
private void Create_EmpDetails_Load(object sender, EventArgs e)
{
using (satsEntities Setupctx = new satsEntities())
{
var viewEmpName = (from viewEN in Setupctx.employees
join ufi u in Setupctx.ufis on viewEN.UFISID equals u.UFISID
select new { u.EmployeeName , u.UFISID}).Distinct().ToList();
cbName.DataSource = viewEmpName;
cbName.DisplayMember = "EmployeeName";
cbName.ValueMember = "EmployeeName";
//cbName.ValueMember = "UFISID";
}
}
Each of those rows has a different UFISID, so Distinct() is not removing them.
It sounds like you just want to show employees:
cbName.DataSource = Setupctx.Employees;
I edited to my codes to this and I managed to show only 1 name for instead of multiple records.
private void Create_EmpDetails_Load(object sender, EventArgs e)
{
using (satsEntities Setupctx = new satsEntities())
{
var viewEmpName = (from viewEN in Setupctx.employees
join ufi u in Setupctx.ufis on viewEN.UFISID equals u.UFISID
select new { u.EmployeeName }).Distinct().ToList();
cbName.DataSource = viewEmpName;
cbName.DisplayMember = "EmployeeName";
cbName.ValueMember = "EmployeeName";
//cbName.ValueMember = "UFISID";
}
}
Probably, it would be enough for you to replace
select new { u.EmployeeName , u.UFISID}
with
select new { u.EmployeeName }
in combobaxes we display the string as DisplayMember for users and id of members(maybe important for us) as ValueMember for us. more time we work with ids . my example :
class Country
{
public string Name { get; set; }
public int ID { get; set; }
public Country( int i,string s) { Name = s; ID = i; }
}
class ComboItem
{
public string DisplayMember { get; set; }
public int ValueMember { get; set; }
}
class ComboItemEqualityComparer : IEqualityComparer<ComboItem>
{
public bool Equals(ComboItem item1, ComboItem item2)
{
if (item1.ValueMember == item2.ValueMember && item1.DisplayMember == item2.DisplayMember)
{
return true;
}
return false;
}
public int GetHashCode(ComboItem item)
{
string str = item.DisplayMember + item.ValueMember;
return str.GetHashCode();
}
}
test :
List<Country> countries = new List<Country> {
new Country(1,"UK"),
new Country(2,"Turkey"),
new Country(8,"Turkey"),
new Country(5,"Turkey"),
new Country(2,"Turkey"),
new Country(3,"USA") ,
new Country(3,"USA")}; //.Distinct(new CountryEqualityComparer()).ToList();
var Data = (from i in countries
select new ComboItem { ValueMember = i.ID, DisplayMember = i.Name }).Distinct(new ComboItemEqualityComparer()).ToList();
cbName.DataSource = Data;
cbName.DisplayMember = "DisplayMember";
cbName.ValueMember = "ValueMember";
sometimes we have data that have displayname the same but the id of them arent. we can change the ComboItemEqualityComparer equals method to :
public bool Equals(ComboItem item1, ComboItem item2)
{
if (item1.ValueMember == item2.ValueMember )
{
return true;
}
return false;
}
enjoy.
for this question we can :
....
select new ComboItem { ValueMember = u.UFISID, DisplayMember = u.EmployeeName }).Distinct(new yourIEqualityComparer()).ToList();

Categories

Resources