Data Binding to DataGridView - c#

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; }
}
}

Related

DataGridViewCheckBox Not Checking from Bound Data

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.

Change the order of asp:GridView *autogenerated* columns

First let me say that I've seen similar questions on StackOverflow, but none that I've seen deal specifically with auto-generated columns.
I have a asp:GridView that I am binding to an IEnumerable<Data>, where Data can vary depending on runtime input. However, each Data class shares a couple base properties:
public class BaseData
{
public int ID { get; set; }
public string Name { get; set; }
}
public class AddressData : BaseData
{
public string Street1 { get; set; }
public string Street2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; };
}
public class ContactData : BaseData
{
public string Phone { get; set; }
public DateTime LastUpdated { get; set; }
// tons more public properties...
}
// More classes with lots of different public properties...
These different classes are populated by a database query, so I want to auto-generate columns when I place the data in the GridView because of the large number of different fields. However, I want to display the columns that are in the shared base class first. If I just assign the IEnumerable<Data> to the GridView datasource, the base classes properties are added last to the GridView's columns. I tried remedying the situation like this:
// Callback after getting data from database
public void SetGridView<Data>(IEnumerable<Data> rows) where Data : BaseData
{
// Make sure these columns show up first
BoundField ID = new BoundField() { HeaderText = "ID", DataField = "ID" };
BoundField Name = new BoundField() { HeaderText = "Name", DataField = "Name" };
myGridView.Columns.Clear();
myGridView.Columns.Add(ID);
myGridView.Columns.Add(Name);
myGridView.AutoGenerateColumns = true;
myGridView.DataSource = rows;
myGridView.DataBind();
}
However, this just duplicates the columns at the beginning and end, so the header looks like this:
ID Name Street1 Street2 City State ZipCode ID Name
So is there a way for me to move the columns that I know will be there (base class) to the beginning of the GridView's columns while still having the convenience of auto-generating all the other columns?
Since you are forced to use auto-generated columns, I'd just create an intermediate DataTable to bind the GridView to. In that DataTable object, you can add your columns in the order you prefer. The GridView should then bind in that same order.
Edit:
Modify BaseData, add the following:
private DataTable dataForDisplay;
// This would be overridden by all child classes, adding their columns in the order you want
public virtual DataTable GetDataForDisplay()
{
dataForDisplay = new DataTable();
dataForDisplay.Columns.Add("ID");
dataForDisplay.Columns.Add("Name");
// If you want to change the order, just don't call the base.GetDataForDisplay()
dataForDisplay.Rows.AddRange(this.ID, this.Name);
return dataForDisplay;
}
Then all of the other child classes will override GetDataForDisplay().

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;

How to manipulate objects created/edited/deleted by a DataGrid

I have 3 classes :
public class Category
{
public string Name {get;set;}
public Category Parent {get;set;}
public List<Category> Children {get;set;}
public bool IsMainCategory {get;set;}
}
public class Item
{
public string Description {get;set;}
public Category MainCategory {get;set;}
public List<CategoryValue> Values {get;set;}
}
public class CategoryValue
{
public Category Category {get;set;}
public Item Item {get;set;}
public double Value {get;set;}
}
I want to create a DataGrid in such a way that,
Its column headers will be the names of all the child categories of a main category which has a true value for IsMainCategory except for the first column which will have "Item Description" as the header.
Its rows will correspond to Item objects and each of these Items should have their MainCategory set to the above mentioned main category.
Every cell of a row (Except the first cell) should bind with the Value property of a corresponding CategoryValue object.
I wrote the code to do 1, but I can't figure out how to implement the other two requirements. Can someone help me?
One simple technique is to transform the data in your POCOs into a datatable that you then bind to the DataGrid.
The DataTable is only used for display purposes. All your logic works against the POCOs

How to selectively display and not display column of object in BindingList which is bond to gridcontrol of devexpress winform?

Dear all devexpress winform gurus:)
My way is simple. First, i create a bindinglist:
private BindingList<QuoteOnGrid> quoteOnGrids = new BindingList<QuoteOnGrid>();
Then my object called quote will be added to the BindingList above, the quote have multiple properties and will be displayed on the grid after binding code below:
gridControl1.DataSource = quoteOnGrids;
The picture below show how manually remove column
a busy cat http://i.minus.com/i1JuwLqAdWy2K.jpg
How could I disable the displaying of last three property of these financial quote object without removing these properties?:)
Thanks in advance!
Please use the GridColumn.Visible property:
//...
gridControl1.DataSource = new BindingList<Person>();
gridView1.Columns["ID"].Visible = false;
}
//...
class Person {
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

Categories

Resources