How can i access the selected date from datepicker column from the selected row of the wpf datagrid in code behind if i know the datagrid's selected row as
DataRowView drv = datagrid.SelectedItem as DataRowView;
where datagrid is my datagrid's name.
If you bind you datagrid to data, then you can access the selected value on your data object.
If you know the column name then you can get value using
drv["columnName"]
Related
I have a datagrid which has an item source of observablecollection and I want to 1 column to be saved int the observablecollection and not show it to the user in the data grid.
how can I do that?
You can hide the Column in the code behind like this:
YourDataGrid.Columns[IndexOftheColumn].Visibility = Visibility.Collapsed;
Alternatively if you want this to be xaml-only, you should set AutoGenerateColumns to False and define custom columns in your Xaml.
Need to do some logic based on the current row selected. New to WPF before I would do something like this;
int i myDataGridView.CurrentCell.RowIndex
Now that is not available, what is best way to get selected row?
Use DataGrid.SelectedItem property. It gives the Selected Item on the DataGrid if SelectionMode is Single. Else DataGrid.SelectedItems gives the multiple selection if SelectionMode is Extended or Multiple
Try this:
SomeObject someObject = (SomeObject)myDataGridView.SelectedItem
I have a DataGrid that is part of a DataTemplate that is assigned to the ContentTemplate of a TabControl. The TabControl's ItemsSource is bound to a collection and, as such, the DataContext for the DataGrid changes to a new collection once each Tab is selected. Currently, there are bindings for ItemsSource and SelectedItem on the DataGrid.
When I move through the tabs the DataGrid is able to keep the selected row synchronized properly but the problem I'm having is that CurrentCell is always set to the first column and first row regardless of what SelectedItem equals.
I've tried setting the CurrentCell property when the DataContext changes for the DataGrid but the DataGrid always resets it back to the first row and first column. Does anyone know how I can accomplish keeping the CurrentCell on the same Row as SelectedItem when DataContext changes?
This worked for me (where dg is the DataGrid):
DataGridCellInfo cellInfo = new DataGridCellInfo(dg.SelectedItem, dg.Columns[0]);
dg.CurrentCell = cellInfo;
i have a datagrid bound to a property. In this grid i have columns which consists of cells which are like hyperlink i mean when user clicks on the cell value based on these values another gird will get populated. i want to know how to get the cell value and pass it to some method so that other grid will get populated.
The best way to do this is in your viewmodel.
You should bind the SelectedItem of your datagrid to a new property in your ViewModel. In the set method of this new Property, call a new method to populate a new ObservableCollection/List/whatever...
Finally, bind your "other grid" ItemsSource to this new observable collection from your ViewModel.
Edit:
If you need to load one thing or another depending on the column you are going to use the code behind, take a look at this:
Silverlight DataGrid how to get cell value from a selected item?
In my form I have a DataGridView bound to a BindingSource that has a DataSet as DataSource. I also have some TextFields and ComboBoxes on the form that are bound to different columns in the DataSet through the BindingSource. The idea is that the values in the columns on the selected row in the DataGridView are reflected in the other controls on the form. Maybe I make it sound a bit complicated, but it's fairly easy to connect the TextFields, and also the ComboBoxes bound to tables in the dataset.
My problem is that this time I want to set the items in the ComboBox from an array and not from a table in the DataSet. This is what I've tried:
Me.ComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.TblBindingSource, "ColumnName", True))
Dim ItemArray(2) As String
ItemArray(0) = ""
ItemArray(1) = "Default"
ItemArray(2) = "User-set"
ComboBox.DataSource = ItemArray
Now, this seems to work partially as the ComboBox is populated correctly, and I can select a value, and it appears in the DataGridView. But it doesn't update its selected value as I change rows in the DataGridView. The column ("ColumnName") is a ComboBoxColumn that gets its item list in the way shown above, and it seams to work as expected.
If it wasn't clear; I have several ComboBoxes with similar functionality that works, but they are bound to a column in a DataTable, as follows:
Me.ComboBox1.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.Tbl1BindingSource, "WiDMethodX", True))
Me.ComboBox1.DataSource = Me.Tbl2BindingSource
Me.ComboBox1.DisplayMember = "SomeColumn"
Me.ComboBox1.ValueMember = "SomeColumn"
If it matters, the DataSet comes from an Access Database.
SelectedValue is used in conjunction with the ValueMember property, but since your array list doesn't have descriptive fields, that won't work.
Try using SelectedItem for your binding:
Me.ComboBox.DataBindings.Add(New Binding("SelectedItem", _
Me.TblBindingSource, "ColumnName", True))
I realize this questions is old, but I had similar problem I resolved. I bound the text property of the combo box to the binding source member that relates to the value member or display member of my combo box datasource. Make sure that you fill your data tables (for binding source and combo box datasource) AND bind your combo box to its datasource prior to databinding text of combo box.
Dim dtForBindingSource as DataTable
Dim bs as BindingSource
Dim dtForComboBox as DataTable
'Code to fill dtForBindingSource would go here
bs.DataSource = dtForBindingSource
'Code to fill dtForComboBox would go here
ComboBox.DataSource = dtForComboBox
ComboBox.DisplayMember = "ColumnToDisplay"
ComboBox.ValueMember = "ColumnXYZ"
'Now that datasources exist and combo box is set up I do databindings.
ComboBox.DataBindings.Add("Text", bs, "ColumnToDisplay")