how to add value from one combo box to another combo box - winforms

I have a combo box with some list of values .After selecting from the list I need to add the rest of the values in the combo box to another combo box .

If I have understand you correctly, this code will help you
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//If you need, clear second combo box from old values before you copy
//comboBox2.Items.Clear();
foreach (var item in comboBox1.Items)
{
if (item != comboBox1.SelectedItem)
{
comboBox2.Items.Add(item);
//If you need to remove item that were copied to second combo box, from the first
//comboBox1.Items.Remove(item);
}
}
}

Related

DataGridView row formatting based on hidden column

Is there any way to set the style of a row based on some value from a column that is not visible to the user? The grid contains a couple of rows and I want some rows to be colored in red if they were deleted. I have a hidden column that stores true if the columns were deleted, false otherwise. I've tried CellFormatting but since my column is not visible, e.ColumnIndex never has the correct value for my hidden column.
Any help will be greatly appreciated.
Edit:
Below is an image of what I am trying to accomplish. You can see that the second row has the text red which is due to the values in a column that the user cannot see in the datagrid. This grid should be colored like this when the user see the form for the first time too (on load).
Instead of CellFormatting, try CellValueChanged for unbound data or DataBindingComplete for a bound data set. For example, let's say that you are "deleting/undeleting" a row using the following Button.Click event:
private void Button1_Click(object sender, EventArgs e)
{
bool value = (bool)dataGridView1.CurrentRow.Cells["Deleted"].Value;
dataGridView1.CurrentRow.Cells["Deleted"].Value = !value;
// For bound data (like a DataTable) add the following line:
// ((DataTable)dataGridView1.DataSource).AcceptChanges();
}
Unbound Data
Changing the rows "deleted" column value in this way will trigger the following event handler. Therefore, you can color your row based on that column's value of True or False:
private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns["Deleted"].Index)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = (bool)dataGridView1[e.ColumnIndex, e.RowIndex].Value ? Color.Red : Color.Black;
}
}
Bound Data
For bound data, such as from a DataTable, handling the DataBindingComplete event will be enough. This event will trigger when the binding is first set as well as after changes - such as the changes from the Button1.Click event. Here, you'll loop through the rows and set the desired style according to the hidden column's value. (Note the additional change to the Button1_Click event handler for a grid with a DataTable source. This is needed to give an immediate style change - otherwise it won't happen until you navigate to a different row.)
private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.DefaultSCellStyle.ForeColor = (bool)row.Cells["Deleted"].Value ? Color.Red : Color.Black;
}
}
Based on my understanding, you want to get a column's value when the column is a invisible column in DataGridView.
Is it right? Please correct me if I'm wrong.
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = CreateDataTable();
dataGridView1.Columns["ID"].Visible = false; // Set the ID column invisible.
MessageBox.Show(dataGridView1.Rows[2].Cells["ID"].Value.ToString()); // Get the ID column value.
}

Silverlight how do I add a "select all" option to a checkbox list?

I have a checkbox list in Silverlight. It's actually a Telerik rad combo box with checkboxes in it.
What I'm trying to do is add an initial item to that list with the label "Select All". When the user clicks on that item it will select or deselect the items in the list. In addition, when the user deselects on of the items it should deselect the "Select All".
The problem is that I have a CheckedItemsChanged event that fires when an item in the list is changed. If I try to change the list during that event it complains that I can't change a collection while in the collection changed event.
Is there another way I can do this?
I'm guessing you're attempting to do something like
void SomeComboBox_CheckedItemsChanged(object sender, SomeEventArgs e)
{
// Do stuff with checked items in list
}
Does it help if you use Dispatcher.BeginInvoke to do the stuff involving the checked items, i.e. something like the following?
void SomeComboBox_CheckedItemsChanged(object sender, SomeEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
// Do stuff with checked items in list
});
}

WPF DataGrid Button Click in New Line Gets the wrong line in Current Item

I have a WPF DataGrid with a button on one of the columns.
When I click the button I have this function called:
MyClass mySC = (MyClass)(CollectionViewSource.GetDefaultView(grdMyClass.DataContext).CurrentItem);
This code works perfect, but when I click on the button on the new line (the last on on the grid) I get the msSC of the line before it, and not null, or something that related to the last new line.
how can I check if the button was clicked in the new line ?
If your intention is that clicking on a button should do something with the data bound to that row then just get the data from the button's data context. If null then user clicked on an empty row.
private void Button_Click(object sender, RoutedEventArgs e)
{
MyClass data = (sender as FrameworkElement).DataContext as MyClass;
}
I just got the exact same problem and created my own solution.
I have a DataGrid with ItemsSource binded to a list in my ViewModel, and SelectedIndex binded to an int in the viewmodel, in order to be able to play with the list when we choose something (and for example simulate an event such as "OnSelectionChanged")
So the solution is very simple here:
//App selected on the list
private int _selectedApp;
public int SelectedApp
{
get { return _selectedApp; }
set
{
if (value == _listApps.Count) _selectedApp = -1;
else _selectedApp = value;
OnPropertyChanged("SelectedApp");
}
}
I just check that the index isn't out of range: if it is, I set it to -1, so my app considers nothing is actually selected.
Hope this can help, feel free to ask more :)

hitTest.RowIndex is always -1

In project there is a DataGridView.
I have a little bit of code that I which displays information based on
the cell that was clicked.
My problem is how to detect if the user clicked on a column or row
header (anything other than a cell).
All this is tied to the 'dataGridView1_CellMouseDown' method, and I'm using
the HitTest to attempt to detect what the user clicked, but all I'm
getting is 'TopLeftHeader' when the user clicks a cell and 'None'
everywhere else and the Row index always comes as -1
Using the CellMouseDown event gives you coordinates relative to the cell that was clicked.
Use the control's MouseDown event instead, which will give you control-based coordinates.
See the example on MSDN.
You can still use the CellMouseDown event handler. In fact i find it a bit cleaner because with MouseDown event, you have to create a HitTest to get the selected row.
The following code is equivalent:
private void dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
// If right-click
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
// Get selected row
var selectedRow = dgvBatches.Rows[e.RowIndex];
}
}
private void dgv_MouseDown(object sender, MouseEventArgs e)
{
// If right-click
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
// Get the selected row/column
DataGridView.HitTestInfo info = dgvBatches.HitTest(e.X, e.Y);
// Get selected row
var selectedRow = dgvBatches.Rows[info.RowIndex];
}
}

Selected item in RadTreeView

How can i retreive "Header" of selected item in RadTreeView(SilverLight) control?
Assuming I understand correctly that you only want to get at header content (in this example simple text) and that you are using RadTreeViewItem as your tree nodes you could do something like this in your selection event response:
private void radTreeView1_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
{
foreach (RadTreeViewItem item in e.AddedItems)
{
string header = item.Header.ToString();
// Do soemthing with the header value here
}
}
This example allows for multiple selection mode, but works as well for a single selection.

Resources