WinForm Combobox - incorrect SelectedValue at Form load - winforms

My windows Form contains 1 combobox bound to Categories bindingsource, and 1 datagrid bound to Products binding source. When I load the form the combobox shows the first value in the categories table and not the selected value in the Products table, and when I change position in the Products bindingsource I can get the right selectedValue in the combobox (it displays correct values). So My problem is in the first load of combobox items.
My Combo properties:
data source = categorybindingsource
display Member = CategoryName
Value Member = CategoryID
Selected Value = productBindingSource – CategoryID
And here is my code:
NorthwindDataContext dc;
private void Form1_Load(object sender, EventArgs e)
{
dc = new NorthwindDataContext();
productBindingSource.DataSource = dc.Products;
this.categoryIDComboBox.DataSource = dc.Categories;
}

Try to invert the order of initialization of combobox and datagrid
NorthwindDataContext dc;
private void Form1_Load(object sender, EventArgs e)
{
dc = new NorthwindDataContext();
this.categoryIDComboBox.DataSource = dc.Categories;
productBindingSource.DataSource = dc.Products;
}

Related

How do I set values for the entries in a Windows Forms ComboBox?

I want to have a drop down list with 12 choices.
I found that ComboBox is what I need (if there is a better control kindly tell me).
I dragged and drop a combo box into a panel using VS2012 and then clicked on the left arrow that appears on the combo box. The following wizard shows:
As you can see, I am just able to type the name of the choice but not the value of it.
My question is how to get the value of these choices?
What I have tried
I built an array with the same length as the choices, so when the user selects any choice, I get the position of that choice and get the value from that array.
Is there a better way?
You need to use a datatable and then select the value from that.
eg)
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Description", typeof(string));
dt.Load(reader);
//Setting Values
combobox.ValueMember = "ID";
combobox.DisplayMember = "Description";
combobox.SelectedValue = "ID";
combobox.DataSource = dt;
You can then populate your datatable using:
dt.Rows.Add("1","ComboxDisplay");
Here, the DisplayMember(The dropdown list items) are the Descriptions and the Value is the ID.
You need to include a 'SelectedIndexChanged' Event on your combobox (If using VS then double click the control in Design Mode) to get the new values. Something like:
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int ID = Combobox.ValueMember;
string Description = ComboBox.DisplayMember.ToString();
}
You can then use the variables in the rest of your code.
You cannot use the wizard to store values and text. To store DisplayText/Value pair the combobox needs to be connected to some data.
public class ComboboxItem
{
public string DisplayText { get; set; }
public int Value { get; set; }
}
There are two properties on the combobox - DisplayMember and ValueMember. We use these to tell the combobox that - show whats in DisplayMember and the actual value is in ValueMember.
private void DataBind()
{
comboBox1.DisplayMember = "DisplayText";
comboBox1.ValueMember = "Value";
ComboboxItem item = new ComboboxItem();
item.DisplayText = "Item1";
item.Value = 1;
comboBox1.Items.Add(item);
}
To get the value -
int selectedValue = (int)comboBox1.SelectedValue;

Getting data from detail row

How can i get data from devexress gridcontrol's detail row via double-click.
If i focused on child row gridview's double click event doesn't catch.
i tried this method, but my request is catching data by double click
private void gcOperasyonlar_FocusedViewChanged(object sender, DevExpress.XtraGrid.ViewFocusEventArgs e)
{
if (e.View != null && e.View.IsDetailView)
(e.View.ParentView as GridView).FocusedRowHandle = e.View.SourceRowHandle;
GridView detailView = gcOperasyonlar.FocusedView as GridView;
MessageBox.Show(detailView.GetFocusedRowCellValue("Kalip").ToString());
}
thanks for your help
There is also an easier way:
ColumnView cv = _gridControlxyz.FocusedView as ColumnView;
selectedRow row = cv.GetRow(cv.FocusedRowHandle)
I found this code on the forum, It might be useful as long as your grid is not editable (so that mouse click doesn't activate the editable field).
private void gridView1_DoubleClick(object sender, EventArgs e) {
GridView view = (GridView)sender;
Point pt = view.GridControl.PointToClient(Control.MousePosition);
DoRowDoubleClick(view, pt);
}
private static void DoRowDoubleClick(GridView view, Point pt) {
GridHitInfo info = view.CalcHitInfo(pt);
if(info.InRow || info.InRowCell) {
string colCaption = info.Column == null ? "N/A" : info.Column.GetCaption();
MessageBox.Show(string.Format("DoubleClick on row: {0}, column: {1}.", info.RowHandle, colCaption));
}
}
http://www.devexpress.com/Support/Center/Question/Details/A2934
Let's say you have two gridviews (I'm guessing you're using gridviews in your grid control): gvMaster and gvDetail.
You should implement event DoubleClick for your gvDetail in order to achieve desired functionality:
private void gvDetail_DoubleClick(object sender, EventArgs e) {
var gv = sender as GridView; // sender is not gvDetail! It's an instance of it. You have as many as there are rows in gvMaster
var row = gv.GetDataRow(e.FocusedRowHandle); // or use gv.GetRow(e.FocusedRowHandle) if your datasource isn't DataSet/DataTable (anything with DataRows in it)
MessageBox.Show(row["Kalip"].ToString());
}

wpf treeview get selected value

I have a TreeView and a ListBox. Both of them get populated from different DataTables.
The Treeview gets its data from:
DataTable product_type = new DataTable();
product_type.Columns.Add(new DataColumn("id_product_type", typeof(int)));
product_type.Columns.Add(new DataColumn("name", typeof(string)));
product_type.Columns.Add(new DataColumn("id_parent",typeof(int)));
DS.Tables.Add(product_type);
The parent child relation:
DS.Relations.Add(new DataRelation("rsParentChild", product_type.Columns["id_product_type"], product_type.Columns["id_parent"]));
So I want to get the id_product_type from the TreeView on SelectedItemChanged and pass it to my listbox, but how do I get the actual value, the int?
Tht's what you are looking for(i used a dataview to bind the list) :
private void tlstView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
int new_value =(int)((DataRowView)e.NewValue).Row["id_product_type"];
}

Change datagrid column values in DatagridCombobox selectedindexchanged event

I have a datagrid with DatagridComboboxcolumn as one of the column in winforms.
Combobox is contain two items Y,N.
If user select Y,I need to change the value for the two columns of same row.
Same thing will happen when User select "N".
I have tried to register ComboBox_SelectedIndexChanged as follows.
But not able to get the row index or coulmn index for the selected row and change the values of the same row columns.
Please help me asap.
private void gridTesr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if(combo != null)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=new EventHandler(ComboBox_SelectedIndexChanged);
// Add the event handler.
combo.SelectedIndexChanged +=new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
Try declaring your own method to handle the ComboBox.SelectedIndexChanged event.
private void gridTesr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if(combo != null)
{
// Remove an existing event-handler, if present
combo.SelectedIndexChanged -= this.MyComboEventHandler;
// Add the event handler
combo.SelectedIndexChanged += this.MyComboEventHandler;
}
}
private void MyComboEventHandler(object sender, EventArgs e)
{
string myValue = ((ComboBox)sender).SelectedItem.ToString();
DataGridViewCell cell = gridTesr.CurrentCell;
MessageBox.Show(string.Format("The current ComboBox resides in the Row {0} and Column {1} and it currently has the {3} value.",
cell.RowIndex, cell.ColumnIndex, myValue));
}

How to store a hidden value in a WPF combo box for use in SelectionChanged method

I have a ComboBox:
<ComboBox Name="Gen2Fis" ItemsSource="{Binding Path=Table}" SelectionChanged="Gen2Fis_SelectionChanged" DisplayMemberPath="LongName">
The query used to fill this combobox is:
Select ShortName, LongName from Table;
Based on the item selected from this list I want call another method with the selected item, but I need to use the ShortName (that isn't displayed) instead of the LongName (which is).
How would I go about doing this? Can I somehow hide the shortname in the list?
My method for loading the combo box:
public void LoadFINamesIntoList(string mainDB)
{
XiphosStr.ConnectString = mainDB;
dbConnection = new MyDatabaseConnection(XiphosStr.ConnectString);
DataSet ds = dbConnection.ExecuteQuery(Queries.getFIs);
Gen2Fis.DataContext = ds.Tables[0].DefaultView;
}
My method for the selection change:
private void Gen2Fis_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string gen2fi = (XiphosDB2.SelectedItem as ComboBoxItem).Content.ToString();
Gen2Str.ConnectString = gen2fi;
DisplayGen2Users();
}
gen2fi will equal the long name, but I need the short name.
Thanks for any help.
Jason
use SelectedValuePath from combobox and set your value path just like the display member
<ComboBox Name="Gen2Fis" ItemsSource="{Binding Path=Table}" SelectionChanged="Gen2Fis_SelectionChanged" DisplayMemberPath="LongName" SelectedValuePath="ShortName">
and then in the selectionchanged event you would be able to use selectedValue .
private void Gen2Fis_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string gen2fi = ((ComboBox)sender).SelectedValue ;
Gen2Str.ConnectString = gen2fi;
DisplayGen2Users();
}

Resources