ComboBox items.count doesn't match DataSource - c#

I have a ComboBox that is bound to a DataSource. I want to dynamically add items to the ComboBox based on certain conditions. So what I've done is add the options to a new list, and then change the DataSource of the ComboBox like so:
cbo.DataSource = null;
cbo.DataSource = cbos;
cbo.DisplayMember = "Title";
cbo.ValueMember = "Value";
Then, I check cbo.Items.Count, and it has not incremented - it does not equal the count of the DataSource. Any ideas what I can do here?
Note this is WinForms and not ASP.NET.

Did you check the Count immediately or at a later time? There is the possibility that the ComboBox does not actually update it's contents until there is an operation such as a UI refresh and hence the count will be off until that time.
On case where this may happen is if you update the DataSource before the Handle is created for the ComboBox. I dug through the code a bit on reflector and it appears the items will not be updated in this case until the ComboBox is actually created and rendered.

If anyone experiences this problem on a dynamically added combobox, the answer is to ensure that you add the combobox to the controls of a container in the form.
By adding "this.Controls.Add(cbo);" to the code before setting the datasource, the problem goes away.

I've found the cause...
I took out the cbo.Datasource = null line.. and added a cbo.Invalidate() at the end. This has solved the problem.
Thanks all for the advice.

cbo.DataSource = null;
cbo.DataSource = cbos;
cbo.DisplayMember = "Title";
cbo.ValueMember = "Value";
Now before setting cbo.SelectedValue, or relying on Items to be up-to-date, call
cbo.CreateControl ;
and Items will be re-calculated.
The problem is that SelectedValue/SelectedIndex, which are WinForms properties, only accept the values that are legal according to the Items list, but that one is built only after GUI interaction, i.e. after instantiating a "real" Windows GUI combo box, i.e. after obtaining a Windows handle for the combobox.
CreateControl forces the creation of the Windows handle, no matter what.

ComboBox cbNew = new ComboBox();
cbNew.Name = "cbLine" + (i+1);
cbNew.Size = cbLine1.Size;
cbNew.Location = new Point(cbLine1.Location.X, cbLine1.Location.Y + 26*i);
cbNew.Enabled = false;
cbNew.DropDownStyle = ComboBoxStyle.DropDownList;
cbNew.DataSource = DBLayer.GetTeams(lineName).Tables[0];
cbNew.DisplayMember = "teamdesc";
cbNew.ValueMember = "id";
Console.WriteLine("ComboBox {0}, itemcount={1}", cbNew.Name, cbNew.Items.Count);
// The output displays itemcount = 0 for run-time created controls
// and >0 for controls created at design-time
gbLines.Controls.Add(cbNew);
TO
ComboBox cbNew = new ComboBox();
cbNew.Name = "cbLine" + (i+1);
cbNew.Size = cbLine1.Size;
cbNew.Location = new Point(cbLine1.Location.X, cbLine1.Location.Y + 26*i);
cbNew.Enabled = false;
cbNew.DropDownStyle = ComboBoxStyle.DropDownList;
Console.WriteLine("ComboBox {0}, itemcount={1}", cbNew.Name, cbNew.Items.Count);
// The output displays itemcount = 0 for run-time created controls
// and >0 for controls created at design-time
gbLines.Controls.Add(cbNew);
cbNew.DataSource = DBLayer.GetTeams(lineName).Tables[0];
cbNew.DisplayMember = "teamdesc";
cbNew.ValueMember = "id";
The DataSource, DisplayMember and ValueMember property must be set after the control has been added to its container.

By adding "this.Controls.Add(cbo);" to the code before setting the datasource, the problem goes away.
//Create dynamic combobox and add to Panel
ComboBox ddCombo = new ComboBox();
controls = new Control[1] { ddCombo };
panel.Controls.AddRange(controls);
//After creating add to table layout
tableLayoutPanel.Controls.Add(panel, 0, 0);
ddCombo .Name = "ddName";
ddCombo .Width = 200;
ddCombo .Location = new Point(x, y); ddCombo .DataSource = ds;//or any List
ddCombo .SelectedIndex = ddCombo .Items.Count - 1;

Just to clarify are you calling the count() method
After calling the databind() method

This code produces 2 in the message box for me, can you try it and see how it behaves for you?
You can paste it into a console application, and add a reference to System.Windows.Forms and System.Drawing.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
namespace SO887803
{
static class Program
{
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
}
public partial class MainForm : Form
{
private Button _Button;
private ComboBox _ComboBox;
public MainForm()
{
_Button = new Button();
_Button.Text = "Test";
_Button.Location = new Point(8, 8);
_Button.Click += _Button_Click;
Controls.Add(_Button);
_ComboBox = new ComboBox();
_ComboBox.Location = new Point(8, 40);
Controls.Add(_ComboBox);
}
private void _Button_Click(object sender, EventArgs e)
{
List<Item> items = new List<Item>();
items.Add(new Item("A", "a"));
items.Add(new Item("B", "b"));
_ComboBox.DataSource = null;
_ComboBox.DataSource = items;
_ComboBox.DisplayMember = "Title";
_ComboBox.ValueMember = "Value";
MessageBox.Show("count: " + _ComboBox.Items.Count);
}
public class Item
{
public String Title { get; set; }
public String Value { get; set; }
public Item(String title, String value)
{
Title = title;
Value = value;
}
}
}
}

comboBox1.DataSource=somelist;
int c1=comboBox1.DataSource.Count; // still zero
BindingContext dummy = this.comboBox1.BindingContext;// Force update NOW!
int c2=comboBox1.DataSource.Count; // now it equals somelist.Count

I had the same problem (Im working with VS 2005).
What you need to do is set the DataSource to null, clear the items, reassign the datasource , display and value members.
Eg
cbo.DataSource = null;
cbo.Items.Clear();
cbo.DataSource = cbos;
cbo.DisplayMember = "Title";
cbo.ValueMember = "Value";

Old thread, but I tried some of these solutions, along with suspending/resuming the bindingcontext, binding to and resetting a binding source, and just plain reloading the form. None worked to update my control with the newly bound data at the time of my .datasource setting (my items.count was empty, just like the OP).
Then I realized that my combobox was on a tabpage that was getting removed at the start of the code, and later re-added (after my databinding). The binding event did not occur until the tabpage was re-added.
Seems obvious in retrospect, but it was very difficult to detect at runtime, due to the order of calls and inability to see when things were changing.

Try this code.
cbo.BindingContext = new BindingContext();
cbo.DataSource = null;
cbo.DataSource = cbos;
cbo.DisplayMember = "Title";
cbo.ValueMember = "Value";
Maybe your ComboBox`s BindingContext is null.

Ba salam,
you can simply refresh the UI by preformLayout() function;
Example:
comboBox1.performLayout();
regards
mohsen s

please try this:
cbo.Parent = <your panel control>;
cbo.DataSource = null;
cbo.DataSource = cbos; cbo.DisplayMember = "Title";
cbo.ValueMember = "Value";
MessageBox.Show(string.Format("itemcount is {0}", cbo.Items.Count);
I think your question sames like I met today.

Related

TableLayoutPanel: How can i navigate with tab key through dynamically added controls?

I have a form which contains a dynamically added TableLayoutPanel, which contains some dynamically added Labels, TextBox, CheckBox. I am obtaining exactly the visualization I would like to have, but I am struggling to get the "tab key" to work for moving from one control to the other.
I have tried to add a:
control.TabIndex = tabIndex++;
control.TabStop = true;
But this doesn't seem to have any impact...
This is the (tested) stub code:
class MyForm : Form
{
public MyForm()
{
InitializeComponent();
string[] titles = {"first","second"};
var myLayout = new TableLayoutPanel();
myLayout.AutoSize = true;
int myTabIndex = 1; //Not really necessary
int rowNumber = 0;
foreach (var title in titles)
{
var label = new Label();
label.Text = title;
myLayout.Controls.Add(label, 0, rowNumber);
var control = new TextBox();
control.TabIndex = myTabIndex++; //Not really necessary
myLayout.Controls.Add(control, 1, rowNumber);
rowNumber++;
}
this.Controls.Add(myLayout);
}
}
This is the window I get, and I am not able to navigate from first to second field using the tab key.
Update:
Applying Visual Studio 2013 Update 5 did not help.
Something must have been corrupted in my project. The best I could do is to move on with a new clean Windows Form project, and everything now is working.

WPF dynamic binding of a control property to another control property

I have a question - I was coding happily a quick little feature to an app, which was a simple comparison output window.
Basically, user clicks a button and I generate a window with a datagrid of two columns of data.
It's all great and a five minutes code living inside one method with no unnecessary reference to anything else. The only problem I encountered was when I wanted to add a TopMost checkbox to this window.
How do I bind the IsChecked property of the box to the TopMost property of the window?
var checkbox = new CheckBox();
checkbox.Name = "cb";
checkbox.Content = "Top most";
var grid = new DataGrid();
grid.ItemsSource = wcList;
grid.Margin = new Thickness(5);
var panel = new StackPanel();
panel.Children.Add(checkbox);
panel.Children.Add(grid);
var win = new Window();
//Binding b = new Binding("cb");
//b.Mode = BindingMode.TwoWay;
//b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
//win.SetBinding(Window.TopmostProperty, b);
win.Title = "WordCount comparison";
win.Content = panel;
win.SizeToContent = SizeToContent.WidthAndHeight;
win.Icon = this.Icon;
win.Show();
This was supposed to be a 5-minutes one-off method, which is why I don't want to go as far as adding any xaml or properties into the code.
Cheers
Bartek
The other way around as you tried (in the commented code):
checkbox.SetBinding(CheckBox.IsCheckedProperty, new Binding("Topmost") { Source = win });
just after you instantiated your win variable.

InvalidArgument=Value of '0' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex

I am getting the above error when i am trying this code. I tried giving just my code but no use. (It was default)
Here is my XML file
The error is in cmbProduct_SelectedIndexChanged event.
cmbProduct --> combobox
cmbBrand --> combobox
Global
DataSet dsUpdate = new DataSet();
Form_load
dsUpdate.ReadXml(#"...\..\stock.xml");
cmbProduct.DataSource = dsUpdate.Tables[0]
.DefaultView.ToTable(true, "productname");//.DefaultView;
cmbProduct.DisplayMember = "productname";
cmbProduct.SelectedIndex = 0;
cmbProduct_SelectedIndexChanged
cmbBrand.Items.Clear();
foreach (DataRow Row in dsUpdate.Tables[0].Select("productname='" + cmbProduct.Text + "'"))
{
//cmbBrand.SelectedIndex = i;
cmbBrand.Items.Add(Row["brandname"].ToString());
//i++;
}
cmbBrand.SelectedIndex = 0; /*ERROR*/
Please help
Thanks in Advance.
Problem is:
when you start application, you do not have items in cmbBrand, but cmbProduct fires SelectedIndexChanged.
Try this:
remove SelectedIndexChanged event initialization from Form1.Designer.cs. Try to find following line:
this.cmbProduct.SelectedIndexChanged += new System.EventHandler(this.cmbProduct_SelectedIndexChanged);
After that, when you populate DataSet with data from xml file, initialize SelectedIndexChanged event:
dsUpdate.ReadXml(#"...\..\stock.xml");
cmbProduct.DataSource = dsUpdate.Tables[0].DefaultView.ToTable(true, "productname");//.DefaultView;
cmbProduct.DisplayMember = "productname";
this.cmbProduct.SelectedIndexChanged += new System.EventHandler(this.cmbProduct_SelectedIndexChanged);
cmbProduct.SelectedIndex = 0;
i had same error. i think this error have a some reasons.
so my error is related to "set DataSource in another thread is not working"
example
//Run in another thread
myComboBox.DataSource = myDataSource; //not set
fix with
myComboBox.Invoke(new Action(() => myComboBox.DataSource = myDataSource));
You can also try this. Before setting combobox DataSource set its BindingContext
cmbProduct.BindingContext = this.BindingContext;
This will happen if you attempt to set the SelectedIndex while there is no valid datasource. If you're resetting the default to 0, and occasionally changing the datasource list, you may see this. You don't need to default to 0 if applying a new datasource, so simple check will avoid it happening:
if (comboBox.Datasource != null) comboBox.SelectedIndex = 0;
If you have this issue:
use the Form_Activated event handler to control setting the indexes.
For me, I had a series of dynamically generated ComboBoxes I added to a Form.
I made a list of the ones where I wanted to use SetIndex=0, then iterated through them in this handler.
I also had a boolean, firstFormActivation, when called the SetIndex the one time only..
You can incidentally use this method for Focus() too, so first field in a Form gets focus when dynamically added.
Here is some code to illustrate the point:
private readonly List<ComboBox> combosToSetIndexOn = new List<ComboBox>();
private bool firstActivation = true;
private Control firstWindowsControl = null;
...
// Other code sets firstWindowsControl...
private void DynamicForm_Activated(object sender, EventArgs e)
{
if (firstActivation)
{
firstActivation = false;
bool fwcPresent = (firstWindowsControl != null);
Console.WriteLine($"DynamicForm_Activated: firstWindowControl present: {fwcPresent}");
if (fwcPresent)
{
firstWindowsControl.Focus();
}
if (combosToSetIndexOn.Count > 0)
{
foreach (ComboBox c in combosToSetIndexOn)
{
Console.WriteLine($"DynamicForm_Activated: processing: {c.Name}");
c.SelectedIndex = 0;
}
}
}
In my case the following was causing my problem
myComboBox.DataSource = myBindingSource
myBindingSource.DataSource = items.ToList() // error
The following worked
myComboBox.DataSource = null;
myBindingSource.DataSource = items.ToList();
MyComboBox.DataSource = myBindingSource;

Clone a DataBound Checked List Box

I have a DataBound CheckedListBox, I "check" few items on list box(source), then I need to clone it to new Checked List Box(target). It need to have all the data, with checked state. I have tried with following function. It is properly flowing through this function.
But finally I can see items on target CheckedListBox but none of the items in target is checked.
private void CloneCheckedListBox(CheckedListBox source, CheckedListBox target)
{
foreach (int checkedItemIndex in source.CheckedIndices)
{
target.SetItemChecked(checkedItemIndex, true);
}
}
Edit:
I have a User control which I have placed on a TabPage, on that User Control there is a "CheckedListBox", I do need to create a new TabPage with the user entered value on selected(current) TabPage(on User Control)
So, what I have done is, create a new Tab Page, get a Copy of the User Control calling it's "Clone()" method.
In "Clone()" method need to have CheckedListBox cloning feature.
Here is my Cloning Code, which is on User Control...
public SearchMain Clone()
{
SearchMain smClone = new SearchMain();
smClone.txtManufacturers.Text = this.txtManufacturers.Text;
smClone.udPriceFrom.Value = this.udPriceFrom.Value;
smClone.udPriceTo.Value = this.udPriceTo.Value;
smClone.chkOld.Checked = this.chkOld.Checked;
smClone.chkPrx.Checked = this.chkPrx.Checked;
smClone.chkDisc.Checked = this.chkDisc.Checked;
smClone.chkStock.Checked = this.chkStock.Checked;
smClone.chkFirstDes.Checked = this.chkFirstDes.Checked;
smClone.chkFirstPN.Checked = this.chkFirstPN.Checked;
smClone.txtSuppPN.Text = this.txtSuppPN.Text;
smClone.txtManuPN.Text = this.txtManuPN.Text;
smClone.txtManufacturers.Text = this.txtManufacturers.Text;
smClone.meDesAND.Text = this.meDesAND.Text;
smClone.meDesOR.Text = this.meDesOR.Text;
smClone.meDesNOT.Text = this.meDesNOT.Text;
smClone.lbManufacSelected.Items.AddRange(this.lbManufacSelected.Items);
smClone.lbSearchWithIn.Items.AddRange(this.lbSearchWithIn.Items);
**CloneCheckedListBox(this.clbLang, smClone.clbLang);**
// CloneCheckedListBox(this.clbTypes, smClone.clbTypes);
return smClone;
}
You can see correct answere here..
Programatically Checking DataBound CheckListBox
try set
source.DataSource = target.DataSource;
target.DisplayMember = "YourDisplayItem";
target.ValueMember = "YourValueItem";
foreach (int checkedItemIndex in source.CheckedIndices)
{
target.SetItemChecked(checkedItemIndex, true);
}

Problem with dynamic create tabPages in Winforms TabControl

I want to create dynamic tabPages in TabControl. In each tabPage I create dataGridView and i want to fill the entire space of each tabPage with this dataGrid. Here is code, where i do this:
private void tabControlMutants_SelectedIndexChanged(object sender, EventArgs e)
{
DataGridView dgw = new DataGridView();
DataGridViewTextBoxColumn testCaseCol = new System.Windows.Forms.DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn resultCol = new System.Windoows.Forms.DataGridViewTextBoxColumn();
//
// dataGridView1
//
dgw.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dgw.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
testCaseCol,
resultCol});
dgw.Location = new System.Drawing.Point(3, 3);
dgw.Name = "dataGridView1";
dgw.AutoSize = true;
dgw.Dock = System.Windows.Forms.DockStyle.Fill;
dgw.TabIndex = 0;
//
// TestCaseColumn
//
testCaseCol.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
testCaseCol.HeaderText = "Test Case";
testCaseCol.Name = "TestCaseColumn";
//
// ResultColumn
//
resultCol.HeaderText = "Result";
resultCol.Name = "ResultColumn";
tabControlMutants.TabPages[(sender as TabControl).SelectedIndex].Controls.Add(dgw);
((System.ComponentModel.ISupportInitialize)(dgw)).EndInit();
//fill dataGridView
}
But it doesn't work, becouse when i resize the main window, data gridView doesn.t change its size (although the dock property is set to fill). Any ideas?
Move the dgw.Dock = System.Windows.Forms.DockStyle.Fill; statement to below the tabControlMutants.TabPages[...].Controls.Add(dgw);line. And maybe below the EndInit(), I'm not sure.
And remove the dgw.Location = ... line because its not needed.
Edit:
I just ran a little test and this basically should work. That means the error is somewhere else, in the code not shown. Maybe in the 'fill rows part'.
I recommend you start removing parts of the code to eliminate the error.
And you do realize you create a Dgv each time a tab is selected, do you? I assume this is demo code.
Remove dgw.AutoSize = true;
Try to add the control first, then set the Dock property

Categories

Resources