Controls

Combobox

Read only combobox

http://www.codeproject.com/KB/combobox/ReadOnlyComboBoxByClaudio.aspx

Fill combobox manually

List<KeyValuePair<string, string>> items = new List<KeyValuePair<string,string>>();
items.Add(new KeyValuePair<string, string>("", ""));
items.Add(new KeyValuePair<string, string>("From database", "D"));
cboFieldType.ValueMember = "Value";
cboFieldType.DisplayMember = "Key";
cboFieldType.DataSource = items;

Results when bound to class

bound to List<class> bound to List<KeyValuePair>
.SelectedIndex position of the selected item in the list, first item = 0 cell-content
.SelectedItem selected instance of class cell-content
.SelectedText the text that is selected, so probably "" cell-content
.SelectedValue Value of the field of the class determined in .ValueMember cell-content
.Text text in the textbox (the field of the class is determined in .DisplayMember cell-content

Bind to bindingsources

1. Set MainBindingSource eg. customer
DataBindings.SelectedValue: MainBindingSource - element of the MainBindingSource that links to the ChildBindingSource, eg. bsCustomer - CountryID.
If the child entity is also available in the main binding source, also fill in:
DataBindings.SelectedItem: MainBindingSource - child item of the MainBindingSource, eg. bsCustomer - Country.
2. Set ChildBindingSource eg. country of the customer, where the countries are kept in a separate table
DataSource: ChildBindingSource, eg. bsCountry
ValueMember: element of the ChildBindingSource (DataSource) that should be linked to the MainBindingSource, eg. CountryID
DisplayMember: element of the ChildBindingSource (DataSource) to be shown in the combobox, eg. CountryName

TreeView

Fill TreeView by code

TreeNode node = new TreeNode();
node.Text = "Home";
node.Value = "0";
tvNavigation.Nodes.Add(node);
node = new TreeNode();
node.Text = "Quote";
node.Value = "1";
TreeNode subnode = new TreeNode();
subnode.Text = "Quote from calculation";
subnode.Value = "2";
node.ChildNodes.Add(subnode);
subnode = new TreeNode();
subnode.Text = "Load a quote";
subnode.Value = "3";
node.ChildNodes.Add(subnode);
tvNavigation.Nodes.Add(node);

Add eventhandler

tbcAdminTool.SelectedIndexChanged += new EventHandler(this.tbcAdminTool_SelectedIndexChanged);

Datagridview

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License