DataGridViewCheckBox Not Checking from Bound Data - c#

I am having trouble getting a DataGridView checkbox to show as checked. The DataGridView is bound to a List of a model that is being populated from a database.
There are three values for this model class, 2 strings and a boolean. At run time I can see that inside the list the Boolean value is set to True, but the datagridview check box is still showing without a check.
I do have one DataGridView that is set up the same exact way, its data source is bound to a List of model classes, and it has several Boolean values that are showing checked correctly.
I have been attempting to find solutions for this, and I have rebuilt the datagridview several times to no change. So, I'm not sure where to go from here on resolving this issue.
Here is the code for the securityGroups model that the list is made up of. There are currently only 2 entries, for this, the 'Cashiers' group should not be Checked, the 'Managers' group should be checked.
class securityGroups
{
public string secGroupName { get; set; }
public string companyName { get; set; }
public Boolean InGroup { get; set; }
}
Now I have another model that I am loading the same way and setting a list of the models that go into another datagridview that is working just fine. Here is that model:
class securityAreas
{
public string secAreaName { get; set; }
public string secAreaDesc { get; set; }
public string secAreaGrou { get; set; }
public Boolean Individual { get; set; }
public Boolean Closed { get; set; }
public Boolean Group { get; set; }
}
Edit 2:
The companyName column visible is set to false.
secGroups = dbm.loadSecurityGroups(emp.empSec);
empSecGroups = secGroups;
dgEmpSecGroups.DataSource = secGroups;
dgEmpSecGroups.Columns[0].ReadOnly = true;
dgEmpSecGroups.Columns[1].ReadOnly = true;
dgEmpSecGroups.Columns[1].Visible = false;
Here is the code that sets up the datagridview. secGroups is a list of securityGroups model, which is being loaded from a database from my dbm class and returned to the form to load the datagrid.

Related

LINQ - Compare two lists of my model that share a property who's value is not equal

I have a model that contains string properties and in my application I've created two collections of those models by selecting files and reading data in them. I have a Source collection and a Target collection of type LayerModel. Each layermodel represents a layer from the drawing file selected by the user, and has properties for each of the pertinent settings I am trying to play with.
The model looks like this:
public class LayerModel : IEquatable<LayerModel>
{
public string Path { get; set; }
public string Name { get; set; }
public string OnOff { get; set; }
public string Freeze { get; set; }
public string Color { get; set; }
public string Linetype { get; set; }
public string Lineweight { get; set; }
public string Transparency { get; set; }
public string Plot { get; set; }
}
I would like to use LINQ to compare those models to produce a list of objects from the target collection who's particular property value is not equal to the source collection.
I have List<LayerModel> SourceDrawingLayers and List<LayerModel> TargetDrawingLayers. The other day I had a similar question regarding this application and I'm able to produce a collection where every single model with any difference at all is added, and that's great, but now I need to drill down and get specific differences.
This is what I've tried. The first line I am posting here returns nothing, where OnOff is the property I am trying to compare:
List<LayerModel> onOffMismatch = TargetDrawingLayers.Where(c => !SourceDrawingLayers.Any(d => c.OnOff == d.OnOff)).ToList();
I thought I had an issue with the logic so I tried to reverse it and changed the method to:
List<LayerModel> onOffMismatch = TargetDrawingLayers.Where(c => SourceDrawingLayers.Any(d => c.OnOff != d.OnOff)).ToList();
This returns every single layer from the TargetDrawingLayers collection.
I realize now that I need to run the following comparison:
Look at all the layers in the TargetDrawingLayers list, find any SourceDrawingLayer whose Name property has the same value, then compare the OnOff property. If the property is different, I will add it to a List. As a bonus, if a Target layer is not found in the source colleciton, add that to separate ExtraLayers List.
Can anyone help out with that?

entity framework 6 datagridview nested child databind

I'm running on a dead end trying to save edited data in a datagridview using EF6 and nested child objects. Hoping someone can point me in the right direction..
I have 2 entities:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Function { get; set; }
}
public class Function
{
public int Id { get; set; }
public int Property { get; set}
}
Next I want to show Customer and the child object from function in a datagridview like so:
Id .. Name .. Function.Property
Datagridview is customized by disabling AutoGenerateColumns. DataPropertyName is set to the objects below:
I created a ModelView:
public class CustomerModelViewObject
{
public int Id { get; set; }
public string Name{ get; set; }
public int f_prop { get; set; } /* Function.Property */
}
So far, so good. I can see the items and I can edit them. When clicking on the 'save' button I need the changes to reflect to the database, but I can't get it to work. I've been looking and searching for 2 days now, but to no avail.
Query and datasource:
var q = from it in _context.KlantenConfigs
select new CustomerModelViewObject
{
Id = it.Id,
Name= it.Name,
f_prop= it.Function.Property
};
klantenConfigDataView.DataSource = q.ToList();
I really hope someone can help me with this. I will be grateful for ever! :-)
To save, you need to (after your save event) get the updated values from the datagridview, update the related entities in _context.KlantenConfigs and then call _context.SaveChanges()
For an easy way to do so see this answer: https://stackoverflow.com/a/21284808/3997704

Grouping by non-column in objectlistview

I have about 5-6 rows of data that I need to display, when I add data I'd like to be able to sort the data in 4 different sections and none of these will be grouped by any column value its simply getting grouped by a SQL value etc.
How do you do this effectively with the objectlistview?
Its pretty straightforward with the standard listview:
listView1.Items[0].Group = listView1.Groups[0];
I can't seem to figure it out in the objectlistview though.
Thanks.
Adding code:
public class AdminHistory
{
public string Ordernr { get; set; }
public string newImei { get; set; }
public string Imei { get; set; }
public string Fel { get; set; }
public string Modell { get; set; }
public string Garanti { get; set; }
public string Datum { get; set; }
public string User { get; set; }
public string Status { get; set; }
}
private void admin_Load(object sender, EventArgs e)
{
List<AdminHistory> list = new List<AdminHistory>();
list.Add(new AdminHistory() { Ordernr = "12345", newImei = "",
Imei = "test", Fel = "test", Modell = "abc",
Garanti = "ja", Datum = "2014-01-01 18:31", User = "123", Status = "waiting"});
adminHistory.SetObjects(list);
}
My objectlistview has the propper aspectnames.
Basically I don't want to group by any of the fields above, I simply want to be able to create a virtual group and group a lot of data into 4 separate groups, then these groups will be displayed in the listview and the user may view them as he/she wishes.
Example image by creators of objectlistview:
http://www.codeproject.com/KB/list/ObjectListView/fancy-screenshot2.png
The grouping in that image does not group by a column, (I think atleast) but rather a virtual group just like I'm trying to achive.
The grouping in that image does not group by a column, (I think atleast) but rather a virtual group just like I'm trying to achive.
The example actually IS grouped by a columns aspect value. But it additionally uses the "Grouping by Ratings" feature using groupColumn.MakeGroupies.
As far as I know, grouping is always bound to a column. However, you could achieve what you want with a small workaround.
"The crucial part to understand is that all model objects that have the same “key” are placed in the same group. By default, the “key” is the aspect of the model object as calculated by the grouping column."
You can return your own "group key" by installing a GroupKeyGetter delegate, but this property is bound to a column. However, you could just use a "spare" column of your choice , install the GroupKeyGetter for it and return the desired "key" which could be anything.
mySortColumnOfChoice.GroupKeyGetter = delegate(object rowObject) {
var key = /* create your key here */
return key;
};
Then use
objectListView1.BuildGroups(mySortColumnOfChoice, SortOrder.None);
to create the group using your "custom" key.

Getting value and putting value to combobox

I'm newbie with winforms and I'm using a combobox. I like to learn how to load from Database to Controls (Combobox) and how to SAVE values from Control (combobox) to Database field.
Below I need some help on process get from Combobox and put/select value to combobox... But don't know how to do...
Below I get all rows in a list and bind to a combobox. That works fine, but like to do some actions which I'm struggling.
Can someone advice how to do these action?
I'd like to "add on top" of comboboxList an empty value so they can also select an empty value in comboboxlist.
How to select a value in combobox when putting from DB to Control.. I'm doing like this
comboboxAccount.SeletedText = _account.Number, but it's not selecting.
now it's showing "Number" in combobox, but I like to show Number + "-" Description. How to do this? now it's showin e.g. 4000 but I like to show "4000 - Description1"
I like to store the Account.Id into the Database not the Account.Number, but how can I do that because the combobox is showing the Number... so SelectedText is not which I want.
Code:
public class Account
{
public Int32 Id { get; set; } e.g. 1
public string Number{ get; set; } e.g. 4000 (alpha-numeric)
public string Description1 { get; set; } e.g. Customer
public string Description2 {get; set;} e.g. VAT account
public string Language {get; set;} e.g. EN
}
//returns all rows
IList<Account> _account = new List<Account>(Account_repository.GetAll());
comboboxAccount.DataSource = account;
comboboxAccount.DisplayMember = "Number";
comboboxAccount.Add(??); (see point 1)
//saving to database
Client _client = new Client();
_client.Account = comboboxAccount.??; (see point 4)
1) I'd like to "add on top" of comboboxList an empty value so they can
also select an empty value in comboboxlist.
An item can never be added directly to the comobbox items, if the DataSource property is set.
So, the list(datasource) should be updated with empyt/dummy object before setting it as the DataSource.
You might have to find a way to describe that your current object is a dummy object.
Example, you could introduce an EmptyItem property, and handle/filter it before saving:
public class Account
{
public Account(bool isEmptyItem =false)
{
this.EmptyItem = isEmptyItem;
}
public Int32 Id { get; set; }
public string Number { get; set; }
public string Description1 { get; set; }
public string Description2 { get; set; }
public string Language { get; set; }
public bool EmptyItem { get; private set; }
}
IList<Account> _account = new List<Account>();
_account.Add(new Account(true))
_account.AddRange(Account_repository.GetAll());
.
2) *How to select a value in combobox when putting from DB to Control..I'm doing like this comboboxAccount.SeletedText = _account.Number, but it's not selecting.*
Since the comboBox is binded to an object, you cannot set its SelectedText instead you should use SelectedItem
Example://You need to write a logic to find out how to get this item.
this.comboboxAccount.SelectedItem = this.comboboxAccount.Items[1];
3) now it's showing "Number" in combobox, but I like to show Number + "-"
Description. How to do this? now it's showin e.g. 4000 but I like to
show "4000 - Description1"
You might have to expose another propety say DisplayText and bind it to DisplayMember of the combobox.
Example:
public class Account
{
public string Number { get; set; }
public string Description1 { get; set; }
public bool EmptyItem { get; private set; }
public string DisplayText
{
get{return this.Number + "-" + this.Description1}
}
}
this.comboboxAccount.DisplayMember = "DisplayText";
4) I like to store the Account.Id into the Database not the
Account.Number, but how can I do that because the combobox is showing
the Number... so SelectedText is not which I want.
You should use SelectedItem since you have binded an object
var account = this.comboboxAccount.SelectedItem as Account;
var accountID = account.Id;
First, I think you shoul use something like a Dictionary or List> or DataTable.
1) You should insert your empty value in your List and then bind it to your combobox
_account.Insert(0, "Empty");
2) Define your ValueMember also(you shoul have a List or a DataTable):
If you have a DataTabele:
comboboxAccount.ValueMember = "columnID";
comboboxAccount.DisplayMember = "columnValue";
comboboxAccount.DataSource = yourDataTable;
combobox.SelectedIndex = 0; //your empty value
3) You should create another property in your class and use this as DisplayMember;
string NumberDescription{ get; set; };
comboboxAccount.DisplayMember = "NumberDescription";
4) If you want to store the ID of the selected value you should use your previously defined ValueMember(the column ID):
int value = Convert.ToInt32(cbomboboxAccount.SelectedValue);
// if you want to save the text of the value selected in the combobox:
string strValue = comboboxAccount.Text;

Data Binding to DataGridView

Im using a List<Patient> object as the data source to a data grid view. I need to use concatenation of two string properties in the Patient class as the value of a single text box column.
I know this can be done by using the OnRowDataBound event in the webforms GridView. How to handle this situation in win forms? I cant see OnRowDataBound event in the win forms Data grid view.
For clarification my Patient class is,
public class Patient
{
public string Initials { get; set; }
public string LastName { get; set; }
}
I need to bind the combination of these two properties to a single column called 'Name' in the grid view. And also there are some other properties in the Patient class which are directly mapped to the data grid view columns using the DataPropertyName of columns. So it is tedious to fill all the columns programmatically.
One easy solution is adding a new property that computes the value to your (view) model and binding to this property.
public class Patient
{
public string Initials { get; set; }
public string LastName { get; set; }
public string InitialsAndLastName
{
get { return this.Initials + " " + this.LastName; }
}
}

Categories

Resources