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"];
}
Related
When my datagrid loads up I am able to get the focus of the first row by providing selected index=0 in the xaml but when I perform searching the focus gets lost so I want focus to be retain at the first row no matter I do sorting and searching on the datagrid.
Here is my code that searches particular thing in the datagrid.
private void TextBox_TextChanged(object sender, RoutedEventArgs e)
{
TextBox STB = (TextBox)sender;
this.SearchValue = STB.Text;
//ContentPresenter CP = (ContentPresenter)STB.TemplatedParent;
//DataGridColumnHeader DGCH = (DataGridColumnHeader)CP.TemplatedParent;
//DataGridColumn DGC = DGCH.Column;
//this.ColumnName = DGC.Header.ToString();
this.Datalist.Filter = this.CustomeFilter;
DataGrid dataGrid = this as DataGrid;
dataGrid.CurrentCell = new DataGridCellInfo(
dataGrid.Items[0], dataGrid.Columns[0]);
dataGrid.BeginEdit();
}
In above code I am trying to get the focus of the current cell but all in vain.
private bool CustomeFilter(object item)
{
SymbolData ltpObj = item as SymbolData;
//WpfApplication1.Model.LtpMessage ltpObj = item as WpfApplication1.Model.LtpMessage;
string values = (string)ltpObj.Symbol.ToString();
values = values.ToUpper();
//return values.StartsWith(this.SearchValue.ToString().ToUpper());
if (values.StartsWith(this.SearchValue.ToString().ToUpper()))
{ return true; }
else
return false;
}
You should read this:
How to programmatically select and focus a row or cell in a DataGrid in WPF: https://blog.magnusmontin.net/2013/11/08/how-to-programmatically-select-and-focus-a-row-or-cell-in-a-datagrid-in-wpf/
You can select and focus a row or cell of a DataGrid programmatically and get the same behaviour as when using the mouse by accessing the visual user interface elements of the DataGrid control and calling the UIElement.Focus() method on a particular DataGridCell object as described in the blog post above. There are code samples included.
You cannot simply set the SelectedItem or SelectedIndex property of the DataGrid to focus the row or cell though.
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;
I have a DataGrid showing some databases having quite some columns.
I would like that, when the user edit a new row, some values are set automatically.
With the windows form DataGrid that would be easy, since there's RowsAdded event handler.
But how could i handle this with the wpf DataGrid ??
Edit : my DataGrid is bound in Xaml to a public property which is an ITable. When user select a table in a ComboBox, the property is updated with corresponding table.
Yes there's autogenerating column, and the way the user can enter a new row is to edit the last blank row (default behaviour).
You can do this in the LoadingRow event. Try something like this:
private void myDataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
MyObject myObject = e.Row.Item as MyObject;
if (myObject != null)
{
myObject.PropertyOne = "test";
myObject.PropertyTwo = 2;
}
}
Ok i think i got it.
When a DataTable is bound to a DataGrid, a CollectionView is created in order to see it. You can get it by using the (static/shared) CollectionView.GetDefaultView(ThePropertyThatIsBound) method.
Since it implements ICollectionChanged, you can add an event handler to the CollectionChangedEvent.
In the CollectionChanged event handler, if you have a new item (e.NewItems.Count>0) you must check it against System.Windows.Data.CollectionView.NewItemPlaceholder and if it is not a place holder, then it is a brand new item, for wich i can set all default values.
Assign a CollectionViewSource to your DataGrid then listen to the CollectionChanged event as following :
..
public CollectionViewSource ViewSource { get; set; }
..
this.ViewSource = new CollectionViewSource();
this.ViewSource.Source = new List<YourObjectType>();
this.ViewSource.View.CollectionChanged += View_CollectionChanged;
..
private void View_CollectionChanged(object sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems.Count > 0)
{
YourObjectType myObject = e.NewItems[e.NewItems.Count-1] as YourObjectType;
if (myObject != null)
{
myObject.Property = TheValueYouWant;
..
}
}
}
..
<DataGrid ItemsSource="{Binding ViewSource.View}" ../>
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;
}
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();
}