I am trying to do the update, In my update form there is combobox whose items comes from the model which is filled from the database.
Now what i am trying to do is that it selects the item automatically which is coming from the database of a specific record.
below is the code
public partial class UpdateCompanyForm : Form
{
CompanyModel c = new CompanyModel();
public UpdateCompanyForm(CompanyModel company)
{
InitializeComponent();
c = company;
}
private void UpdateCompanyForm_Load(object sender, EventArgs e)
{
CompanyNameEnValue.Text = c.CompanyNameEn;
SectorComboBox.DataSource = GlobalConfig.Connections.GetAll_Sector();
SectorComboBox.SelectedItem = c.Sector;
SectorComboBox.DisplayMember = "Name";
CategoryComboBox.DataSource = GlobalConfig.Connections.GetAll_Category();
CategoryComboBox.SelectedItem = c.Category;
CategoryComboBox.DisplayMember = "CategoryName";
PhoneNumberValue.Text = c.Contacts.PhoneNumber;
}
}
I have 2 items in the SectorCombobox and 2 items in categorycombobox.
It is filled with by the database but i want it select a specific item.
You all know how update works.
Related
I have defined a model in Entity Framework. Now I want to fill out the combobox from this model. But I was unsuccessful.
private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
{
using (SamenEntities c = new SamenEntities())
{
comboBox6.DataSource = c.sabt_como_tahsili.ToList();
comboBox6.ValueMember = "id_vaziat_tahsili";
comboBox6.DisplayMember = "name_vaziat_tahsili";
}
}
No data is displayed in the comboBox
You should fill your combo box when you initialize your form. Or maybe create a RefreshDataSources function which will reload every data set on your form, this combo box included. Like this maybe?
private void RefreshDataSources()
{
using (SamenEntities c = new SamenEntities())
{
#region combobox
comboBox6.DataSource = c.sabt_como_tahsili.ToList();
comboBox6.ValueMember = "id_vaziat_tahsili";
comboBox6.DisplayMember = "name_vaziat_tahsili";
#endregion
// place other controls here
}
}
But doing that in the event SelectedIndexChanged is not the best choice, even when it would work.
EDIT
Just checked the behavior of a usual microsoft combobox item.
The SelectedIndexChanged won't be launched if there is no items inside it, so no chance to execute your code.
public Form1()
{
InitializeComponent();
using (SamenEntities c = new SamenEntities())
{
comboBox6.DataSource = c.sabt_como_tahsili.ToList();
comboBox6.ValueMember = "id_vaziat_tahsili";
comboBox6.DisplayMember = "name_vaziat_tahsili";
}
}
I'm working in C# and I've been given these criteria to meet:
Implement a GUI (Windows Form Application) Horoscope application with the following functionality:
Hint: Use collections to Bound data at Run time.
A combo-box displaying all the signs
An image control to be updated with the selected sign
A list-box with all the predictions for the selected sign
UI to add a prediction for a given sign and refresh the list
I was able to get the combobox to populate with the signs I've chosen doing this:
private void Form1_Load(object sender, EventArgs e)
{
DataTable Horoscope = new DataTable();
DataColumn dc1 = new DataColumn("Sign");
// DataColumn dc2 = new DataColumn("Reading");
Horoscope.Columns.Add(dc1);
// Horoscope.Columns.Add(dc2);
Horoscope.Rows.Add("Libra");
Horoscope.Rows.Add("Leo");
Horoscope.Rows.Add("Virgo");
Horoscope.Rows.Add("Scorpio");
cmbHoro.DataSource = Horoscope;
cmbHoro.DisplayMember = "Sign";
}
My question is how can I assign lets say 3 different readings to each sign then depending on the user picking a sign in the combo box one of the three readings is shown?
you have used DataTable in question, I am using Dictionary here in answer.
you can use DataTable too, then you need to make columns and relationship accordingly.
Main motive of this answer is to show you how you can dynamically change entries of listbox on selection of combobox
here we have a class which holds Horoscope related data : HoroscopeData
public class HoroscopeData
{
public string sign;
public List<string> prediction;
public HoroscopeData(string sign)
{
this.sign = sign;
this.prediction = GetPredictionForSign(sign);
}
private List<string> GetPredictionForSign(string sign)
{
List<string> predition = new List<string>();
for (int i = 0; i < 3; i++)
predition.Add("Prediction of " + sign + " " + i.ToString());
return predition;
}
}
and our data structure is
public Dictionary<int, HoroscopeData> Horoscope = new Dictionary<int, HoroscopeData>();
so our code will look like,
public partial class Form1 : Form
{
public Dictionary<int, HoroscopeData> Horoscope = new Dictionary<int, HoroscopeData>();
public Form1()
{
InitializeComponent();
Horoscope.Add(1, new HoroscopeData("Aris"));
Horoscope.Add(2, new HoroscopeData("Libra"));
Horoscope.Add(3, new HoroscopeData("Leo"));
//adding items in combobox, with tag which is associated with our main data
// so we can easily fetch prediction out of it whenever selection changes
foreach(int tag in Horoscope.Keys)
{
CmbItem item = new CmbItem(Horoscope[tag].sign, tag);
comboBox1.Items.Add(item);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//here on combobox selection, we are fetching predictions of selected sign
// and add them in listbox
CmbItem selectedItem = (CmbItem)(((ComboBox)sender).SelectedItem);
List<string> predictions = Horoscope[selectedItem.tag].prediction;
listBox1.Items.Clear();
foreach (string pre in predictions)
listBox1.Items.Add(pre);
}
}
I Made a datagrid with a button. When I click the button it should load data from the database into the grid.
This works. I linked foreign keys to this datatable, and they also load in the datagrid, but they get displayed as dbname.tablename. I want this to display as their name.
How can I do this?
DataClasses1DataContext db = new DataClasses1DataContext();
reservering deResv = new reservering();
chalet hetHuis = new chalet();
klant deKlant = new klant();
public testpanel()
{
InitializeComponent();
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
var listResv = db.reserverings.OrderBy(x => x.KlantId).ToList(); //
dgTest.ItemsSource = listResv;
}
Let me know if you need anymore info.
I have two comboboxes which I populate using my GetAllCities() method in the CtrlMap.
My idea is, whenever I select another city on the ddFrom it should databind all the cities to ddTo (and later on remove the exact same selected, so user won't be able to select same city as point From and To).
However, Whenever I select something on ddFrom, ddTo populates (as it should), but SelectedIndex gets the same as the ddFrom. Same goes in the opposite way. If I select a city, lets say New York on ddTo it is also selected on ddFrom.
In the GUINewBooking.Designer.cs there's only this event handler registered: this.ddFrom.SelectedIndexChanged += new System.EventHandler(this.ddFrom_SelectedIndexChanged);
ddTo has no event handler registered. Any ideas?
public partial class GUINewBooking : Form
{
private CtrlMap ctrlMap;
public GUINewBooking()
{
InitializeComponent();
ctrlMap = new CtrlMap();
ddFrom.DataSource = ctrlMap.GetAllCities();
ddFrom.DisplayMember = "name";
}
private void ddFrom_SelectedIndexChanged(object sender, EventArgs e)
{
ddTo.DataSource = ctrlMap.GetAllCities();
ddTo.DisplayMember = "name";
}
}
I believe it's because you are using the same data source. You might need to
private void ddFrom_SelectedIndexChanged(object sender, EventArgs e)
{
CtrlMap ctrlMapTo = new CtrlMap();
ddTo.DataSource = ctrlMap2.GetAllCities();
ddTo.DisplayMember = "name";
}
The answer can be found Strange behavior of Windows Forms combobox control
Each combobox DataSource property should be assigned to a different BindingSource object.
Example:
cmbDataType1.DataSource = new BindingSource(datasource, "");
cmbDataType2.DataSource = new BindingSource(datasource, "");
Or in my particular case:
ddFrom.DataSource = new BindingSource(ctrlMap.GetAllCities(), "");
ddTo.DataSource = new BindingSource(ctrlMap.GetAllCities(), "");
I have two Lists, the firs (right) represents a list of all cars and the secound (left) a list of cars for sale.
There are two ListBoxes at my control, each of them will display one of the cars list. I want to click a button (<<) and the selected car at the ListBox 1 will be added to the ListBox 1 list.
The two lists are created outside the control class, so I need to bind then to the ListBoxes. I have tried to use DataSource but I can not use Add Remove from Items if I set it. Which is the best way to do that?
Thanks.
OBS: Ive changed to a ListBox.
The solution that I'm using is: Very very ugly solution... :/
public IList<Item> ItensToMaintaim
{
get { return (IList<Item>)this.itensToMainTainList.DataSource; }
set
{
//Need to set null to refresh
this.itensToMainTainList.DataSource = null;
this.itensToMainTainList.DataSource = value;
this.itensToMainTainList.DisplayMember = "Name";
this.itensToMainTainList.ValueMember = "Name";
}
}
public IList<Item> Itens
{
get { return (IList<Item>)this.itensList.DataSource; }
set
{
//Need to set null to refresh
this.itensList.DataSource = null;
this.itensList.DataSource = value;
this.itensList.DisplayMember = "Name";
this.itensList.ValueMember = "Name";
}
}
private void removeItem_Click(object sender, EventArgs e)
{
if (this.itensToMainTainList.SelectedItem != null)
{
this.itens2.Remove((Item)this.itensToMainTainList.SelectedItem);
this.ItensToMaintaim = this.itens2;
if (this.itensToMainTainList.SelectedIndex < 0)
{
this.itensToMainTainList.SelectedIndex = this.itens2.Count - 1;
}
}
}
private void addItem_Click(object sender, EventArgs e)
{
if (this.itensList.SelectedItem != null)
{
bool contains = false;
contains = this.itens2.Contains(this.itensList.SelectedItem);
if (!contains)
{
this.itens2.Add((Item)this.itensList.SelectedItem);
this.ItensToMaintaim = this.itens2;
}
if (this.itensList.SelectedIndex < this.itens1.Count - 1)
{
this.itensList.SelectedIndex++;
}
}
}
Once you set the DataSource, you can not add items to that collection.
"Items collection cannot be modified when the DataSource property is set."
But you can do it with some workarounds
1) Save to database and load it again with new values and bind it
OR
2) Get the exising data source of the Listbox and store it in a varaiable and add a new item (which is created from the selected item) and then re bind it again
Example ( the classes are specific to my need, you can customize based on your class structure)
//Take the existing
List<MailerKit> objExisting = (List<MailerKit>)comboBox1.DataSource;
//Add the new one
objExisting.Add(new MailerKit { KitName = comboBox1.SelectedText, ID = Convert.ToInt32(comboBox1.SelectedValue) });
//Rebind again
comboBox1.DataSource = objExisting;
comboBox1.DisplayMember = "KitName";
comboBox1.ValueMember = "ID";
You can create your custom events for adding and removing items in the lists. Handle these events in your control class where you have defined the comboboxes to add or remove their items.
Bind lists with datasource, as you did. Then remove and add items to lists, not to combobox itselves.