How to add a Column with Button Control in Grid View? - winforms

I am working on windows forms using DevExpress. I bonded my DevExpress GridView on Form_Load.
private void DXRemarks_Load(object sender, EventArgs e) {
gridView.DataSource = myDataTable;
}
This draws all the columns in my gridView. Now, I want to add a column consisting of a button control to get the value of a particular row on button's click from that gridView. I don't know how to do that (can't bind both my data and button together).
Can anyone help me please?

You could avoid the button altogether by instead using the _CellDoubleClickevent instead.
for the event use:
gridView.Rows[e.RowIndex].Cells[n].Value
where "n" is the cell reference of the value you want to retrieve (starting from 0)
then either use ToString() or a convert to get the value you need from the cell - depending on what the type of value you want to retrieve is.
You can make this easier by setting the SelectionMode property to FullRowSelect and making the cells uneditable - thus making a double click on any row provide the value
If you are dead set on using a button to obtain the value, then
gridView.SelectedCells[n].Value
assigned to the onclick event will get the value of cell "n" from your datagridview. As above, you can then use ToString() or a convert method to access the value

Related

enable/disable data grid columns using ivalue convertor in silverlight

I am working on a silverlight application using MVVM. My requirements is to display existing user data in data grid so that first two columns remains non-editable and rest will be editable.
At start datagrid loads data from database, at that point if user click on data grid first two columns should be non-editable.
After that user insert a new row (i create a button, when that is clicked a new row is added at the bottom of the grid) all columns should be editable including first two. Now user can click Add row buttons more than once, point is rows created by Add button click should be editable.
I am stuck at this problem since yesterday any help would be great!
I dont know Silverlight but I think the following should work:
On your command to add a new row, set a flag like "AllRowsEditable" to true and throw a PropertyChanged for this Property. In your view you bind the IsReadonly property of the first two columns to that "AllRowsEditable" property.
EDIT: Write a ViewModel for your DataGrid items. For example "RowViewModel". To have a good structure I would introduce two properties like "IsFirstPropertyReadOnly" and "IsSecondPropertyReadOnly" in that ViewModel. "...firstProperty..." is your properties name. In your XAML you can bind to this properties. In your first initialization you load the items and set the property values to true. All items added after that you set that properties to false.

Disable the automatic selection of datagrid row using the mouse or keyboard

In my sample project I have a datagrid that gets populated with values from the server. The datagrid row can be selected using the mouse initially. But in my code, this row selection should happen only after an "activate" button is clicked. How should I do this?
I tried the IsEnabled property for datagrid, but that causes the entire datagrid to be inactive
(the text and headers are grayed out).
Please help me out.
I guess that should work:
datagrid.SelectionChanged += (obj, args) =>
Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
datagrid.UnselectAll()));
An alternative is modifying DataGrid styles. Anyway, you need to also consider your "activated" value.

how to raise an event when a value in a cell of a wpf datagrid changes using MVVM?

I need help with a wpf datagrid using the MVVM design pattern.
I have a datagid that is bound to an observablecollection. The first column in the grid contains decimal values that cannot be edited. The second column contains a textbox into which a decimal value must be entered. The third column must display the difference between the value in the first column and the value in the second column AS IT IS ENTERED. I was hoping that handling the observablecollection's Collectionchanged event will allow met to determine when a field of one of the items in the collection has changed, but that does not seem to work.
I've also tried handling the PropertyChanged event of the grid's selected item, but that's not working either.
Can someone please indicate to me how to raise an event in the viewmodel whenever 'n value in a textbox, in a datagrid DataGridTemplateColumn, is changed? And then how do I set the calculated value in the third column's corresponding row?
You should try to tackle it from the other end (i..e from the ViewModel).
Your item(calling it CollectionItem) in the ObservableCollection should implement INotifyPropertyChanged.
You should tweak your grid so that data change is registered/commited as you change them (not on focus out/move)
and then in your CollectionItem should try to refresh the value based on the value change of input. let me know if you want more detail

Silverlight 4 datagrid not re-sorting

I'm new to both Silverlight and RIA. I have a simple form with a DataGrid bound to a DomainDataSource object. The rows displayed represent section headings to be displayed on a webpage. One of the columns is called OrdinalPosition and I have specified that the grid is to sort by this column. I have a custom column with up and down arrow buttons. The desired behavior is that when the user clicks the up/down buttons the OrdinalPosition is incremented/decremented so that they can specify what order the sections appear in.
If I manually change the value in the OrdinalPosition column, as soon as I move off the row the grid reorders itself. However, if I use code-behind to change the value the grid does not reorder itself (even though the grid does display the new value.) Here is my codebehind for the button click...
private void incrementOrdinal(object sender, System.Windows.RoutedEventArgs e)
{
Button btn = (Button)sender;
Section s = (Section)sectionDataGrid.SelectedItem;
s.Ordinal++;
sectionDataGrid.CommitEdit();
}
Is there something I should be doing to cue the grid to reorder its records?
DomainDataSource will not re-sort data automatically when the records change unless the IEditableCollectionView interface is used to apply the changes through the data that the DomainDataSource exposes through its Data or DataView.
Try something like the following:
IEditableCollectionView view = (IEditableCollectionView)selectionDataGrid.ItemsSource;
Section s = view.CurrentItem;
view.EditItem(s);
s.Ordinal++;
view.CommitEdit();
This is what DataGrid performs when doing edits through the UI. The ItemsSource is bound to the DomainDataSource.Data property, which is an instance of the DomainDataSourceView class, representing the IEnumerable data that was loaded. The DomainDataSourceView implements IEditableCollectionView and when CommitEdit is called against that view after using EditItem, it will re-sort the data on the current page.
Note that when there are changes, re-sorting locally will not allow items to move onto or off of the current page.

How do I detect when a cell's value has changed in Silverlight?

I'm working in Silverlight, trying to figure out how to set a grid cell font color based on the contents of the cell.
I have an ObservableCollection bound to a DataGrid, and my items implement INotifyPropertyChanged so the grid updates as I change the values; it's all working perfectly, including letting me sort items and keep the sorting while I update the underlying items.
I know I can use the LoadingRow event to change colors, but the only way I can get the event to fire is by changing the grids datasource, in which case my sorting goes out the window.
So, what I really want is a way to either
loop the rows in the datagrid,
find the cell I need, and change
it's color or
implement a custom
column that I can use to dynamically
set the color.
The problem is how to actually do either of those things :).
You should use databinding for this.
Bind your cell font color to the content of the cell
Create a converter IValueConverter that converts the value to a color depending from your needs
See here for a good example
http://weblogs.asp.net/joewrobel/archive/2009/01/25/conditional-formatting-in-the-silverlight-datagrid.aspx

Resources