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;
Related
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.
I have a for-loop with a RadioButton at each iteration. My problem is I don't know how to distinguish every single RadioButton once I validate my form (how to retrieve their value?) . I have a model for printers :
public class Printer{
public int id{ get; get;}
public string printerName { get; set; }
}
Which itself is part of another model :
public class FinalSiteModel
{
public string name{ get; set; }
public List<Printer> printers{ get; set; }
public bool selected { get; set; }
}
Then as I pass it to the view :
for(var i = 0; i < Model.printers.Count() ; i++)
{
#Html.RadioButtonFor(m => m.selected, new { Name = "groupRadio"});
#Html.Label(Model.printers[i].printerName);
}
I think something is wrong with the way I wrote my RadioButton. I initially used the boolean selected inside of the PrinterModel but my problem then came from the fact that the radiobuttons didn't group since I used
#Html.RadioButtonFor(m => m.printers[i].selected, new { Name = "groupRadio"});
and this bit : m => m.printers[i].selected created a different name for each radiobutton, hence when I check several radiobuttons at the same time, which I don't want.
I'm guessing I should add a parameter to RadioButtonFor ?
Your radio buttons need a value to bind to your selected property. However that property needs to be int, not bool
public class FinalSiteModel
{
...
public List<Printer> Printers { get; set; }
public int SelectedPrinter { get; set; }
}
and then in the view
foreach(var printer in Model.Printers)
{
<label>
#Html.RadioButtonFor(m => m.SelectedPrinter, printer.Id, new { id = "" })
<span>#printer.Name</span>
</label>
}
Note the 2nd parameter of RadioButtonFor() adds the value attribute based on the Id property of Printer, and the new { id = "" } removes the id attribute which would otherwise be invalid because the method generates duplicates
The value of SelectedPrinter will now contain the Id of the selected Printer when you POST your form.
As a side note, never attempt to change the name attribute using new { Name = "groupRadio" } - it would not bind to your model.
So I have two lists (Members and RealEstate)
I want my DataGridView to display results of the MemberID that was written in a text box I have.
Results include:
MemberID, FirstName from the Members List and
EstateType, EstatePrice, EstateArea from the RealEstate List
My code is as follows :
List<Members> member = FrmSell.MembersList.FindAll(owner => owner.MemberID == int.Parse(FrmSell.txt));
List<RealEstate> realestate = FrmSell.EstateList;
dgvProperty.DataSource = member;
dgvProperty.DataSource = realestate;
And when I click on my button it only displays the results from the second list which is the RealEstate list and not the first one, I want to display results from both lists combined into a single data grid view.
If you need any more clarification please do tell me.
Sample data from the list :
//Sample data
Members m1 = new Members(001,"Ahmed","Muhairy",503299999);
MembersList.Add(new Members(002,"Khalfan","AlMarri",502344556));
RealEstate r1 = new RealEstate("Villa",35000,"Quoz",4,2);
EstateList.Add(new RealEstate("House",55000,"Sharjah",6,4));
You will need to combine the fields from the matching entries in the two lists into either a DataSet or DataTable, or into a list of items of a new class that contains the fields from both input types.
For this to work, there must be a field in either the Member or RealEstate class that allows matching up the entries from both input lists.
As an example, I am going to assume the following: your RealEstate class has an OwnerID field, whose value identifies one of the members in the MembersList, because it is equal to the member's MemberID value.
Now you can do the following:
// This combines the fields from Member and RealEstate
// for a row to be displayed in the data grid.
public class MemberRealEstate
{
public int MemberID { get; set; }
public string MemberName { get; set; }
public int EstateID { get; set; }
public string EstateType { get; set; }
public double EstatePrice { get; set; }
public string EstateArea { get; set; }
}
And create a new list, combining entries from the other two:
var forMemberId = int.Parse(FrmSell.txt);
var memberRealEstateList =
(from estate in FrmSell.EstateList
from member in FrmSell.MembersList
where member.MemberID == forMemberId && member.MemberID == estate.OwnerID
select new MemberRealEstate() {
MemberID = member.MemberID,
MemberName = member.FirstName + " " + member.LastName,
EstateID = estate.EstateID,
EstateType = estate.EstateType,
EstatePrice = estate.EstatePrice,
EstateArea = estate.EstateArea
}).ToList();
Now you can assign this list as the DataSource for your DataGridView:
dgvProperty.DataSource = memberRealEstateList;
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.
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; }
}