XtraGrid Questions - c#

I have follwoing feew questions on XtraGrid (Dev Express).
How to enable editing the cell by double clicking on it? by defalut XtraGrid permits cell editing if we just click on it. I dont want this to be happen.
How do I get the column/Row information which is edited?. Is there any event like AfterRowEdit() or AfterCellEdit()?
Thanks,
Omkar

1 You could capture the click event and enable the editor if its clicked twice in a short timespan.
2 To get column/row information I would add a special editor to the column and capture its events.

Try setting the OptionsBehaviour.EditorShowMode property of your view to MouseDownFocused. That way the user has to focus the cell first, and the editor will show up only on second click.
Have a look at view's ValidateRow event, or if you need any processing BEFORE editing a row, you could use the view's ShowingEditor event, and grab the actual row by the view's FocusedRowHandle property.

Disable the gridview editor.
capture DoubleClick event on the gridcontrol.
And in this event enable the gridview editor
===========
Bind each column to a repository item
Go to a column and find the columnedit property.
Set a repository item to that column.
Then assign the validating event to the repository item.
Code:
private void your_gridcontrol_double_click(object sender, EventArgs e)
{
GridHitInfo hit = your_gridview.CalcHitInfo((e as MouseEventArgs).Location);
if (hit.InRow)
{
}
}

Related

Creating a custom definition/property for a control

I'm currently about to design a project that uses a DataGridView. As one of the events I would like it to perform a task upon double-clicking on a row.
Looking around I can find examples of creating your own event, however I notice that DataGridView has no property or definition for double-clicking a row, and was wondering how I could specify this myself?
There's a CellMouseDoubleClick event that, in contrast to the "normal" DataGridView's mouse double click event, fires only when the user actually double-clicks the cell of a row.
About the question on how to do it yourself: You could try to derive a new class from DataGridView, attach the CellMouseDoubleClick event internally and just fire a new event, passing the clicked row. For example:
private void DataGridView1_CellMouseDoubleClick(Object sender, DataGridViewCellMouseEventArgs e)
{
// Determine the row the clicked cell belongs to
...
// Fire a new event for that row
...
}

Remove Edit, Update, and Cancel links from a RadGrid

I am trying to remove the links that are displayed in a Telerik RadGrid by default. Here is what the grid looks like before I try to remove the edit link:
I have found this snippet of code, it is used to remove the edit link:
if (!IsPostBack)
{
foreach (GridItem item in RGV_POI.MasterTableView.Items)
{
if (item is GridEditableItem)
{
GridEditableItem editableItem = item as GridDataItem;
editableItem.Edit = true;
}
}
RGV_POI.Rebind();
}
This is how the grid looks after the code:
The edit link still shows up on the first item. Is there a way to remove the edit, update, and cancel link on each item in the RadGrid? I want to be able to remove/disable the links, using a button click event. Then be able to add/enable the links back, using a button click event.
I'm not aware of Telerik RadGrid Control, but for sure the control should inherit asp:GridView. You can make links not visible in RowDataBound event. Here how you can do it.
Add OnItemDataBound="Grid_ItemDataBound" on the grid view.
In the code behind:
protected void Grid_ItemDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Item.DataItem == null)
return;
//cell of all the link button edit/update etc.
TableCell cell = e.Item.Cells[//index of the column];
foreach(Control c in cell.Controls)
{
c.Visible = false;
}
}
You should check the ID of the cancel, edit, update buttons somehow. Probably you should give more information about the controls in the aspx.
EDIT:
Use OnItemDataBound event it existing in their documentation: http://www.telerik.com/help/aspnet-ajax/events_t_telerik_web_ui_radgrid.html
The edit link button in a RadGrid is actually a column itself, specifically a GridEditCommandColumn. In order to show/hide this in the event of a button click, you would have to essentially rebuild all the columns programatically in the click event handler, including or excluding the GridEditCommandColumn as needed. You cannot add or remove a single column programatically when the rest of the grid is created declaratively. It would be useful if we could see more of how the grid is declared and built in your application.
Creating a RadGrid Programatically
It may be possible to change the GridEditCommandColumn.Display property, however. If you can get a handle on the column itself, instead of the individual cells, you may be able to adjust this as needed in your button click events.
You should remove the GridEditCommandColumn if you do not want your items editable. Another option is to change its visibility on the server through its Visible/Display property. You can use the grid's GetColumnSafe(columnName) method to get the neded reference: http://www.telerik.com/help/aspnet-ajax/grid-using-getitems-getcolumn-methods.html
To get rid of the update/cancel buttons, you can use a custom template, although I do not see why you would need to do that if your grid is not editable: http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx

How can I skip the CellValueChanging state of DevExpress ColumnView?

There are 2 distinct UI concept: CellValueChanging vs CellValueChanged.
Quoted from DevExpress Documentation:
The CellValueChanging event is raised each time the edited value is being changed (the user types or deletes a character, chooses a value from the dropdown list, etc.).
On the other hand, CellValueChanged is raised when user had done cell editing by hitting enter or clicking outside of the active cell.
Now my problem is, I have a combobox type column, and I want to always skip the CellValueChanging and make the change final. The current behavior is when user select an item from the combobox, the change doesn't take effect immediately(e.g, the view will not be resorted as per the change). The change is not accepted until user hit enter or click outside of the cell.
---------------07/26/2013 2:25PM update---------------
Sorry that my previous question description misled everybody, so I'll rephrase it.
Here is the current behavior:
Pic1: beginning state. Rows are sorted alphabetically by Target.
Pic2: Change the 2nd row value from B to D
Pic3: After a single mouse left click on item D, the dropdown disappears and the cell value changes to D. However, the rows are not resorted
Pic4: After clicking outside the cell or hitting enter, the rows are resorted.
What I want to achieve is to skip the step in Pic3. In other word, I want any changes committed immediately, without having to make an extra kepboard or mouse click.
What I am showing here is a simplified example of my application. I can't move my CellValueChanged event handler logic to CellValueChanging or EditValueChanged because it would cause some errors.
You want to make some UI change once user edit the cell value, right? I think generally you have to give up handling the CellValueChanged event, but use the CellValueChanging Event instead:
pseudocode:
HandleCellValueChanging(object sender, CellValueChangingEventArgs e)
{
// Get underlying object
// and write the value direct into the object
var rowObj = (YourType)gridview.GetRow(gridView.FocusedRowHandle);
rowObj.TargetField = e.NewValue; // e.Value or e.NewValue, not sure
// Then Do your UI effect
}
Another option is to use RepositoryItemEditor and handle the key-up and/or Mouse-up event instead.
If you want to to catch the moment when an end-user changes some value in gridview's cell editor (e.g. select item in combobox), please handle the EditValueChanged event of a corresponding RepositoryItem. To immediately post this value (make the changes final), you need to call the PostEditor method of a corresponding container:
repositoryItemComboBox1.EditValueChanged += repositoryItemComboBox1_EditValueChanged;
//...
void repositoryItemComboBox1_EditValueChanged(object sender, EventArgs e) {
gridView1.PostEditor();
// do something else if it needed
}
Related example: E3234 - How to update a row's style immediately after a an inplace editor's value is changed

DataGridView Row Enter

I am trying to determine if the user has selected a row to get the currently selected row. But when the form is initialized it calls the event.
Is there a way to check if the data grid view is being initialized? I thought about setting a flag and using RowPostPaint to set the flag.
Any other ways to ignore the select row event during initialization?
Maybe it helps to check if the DataGridView has the focus:
if (this.dataGridView.Focused)
{
// handle selection ...
}
On form initialization the control shouldn't have the focus but when the user manually changes the row selection it should have the focus.
And maybe you are using the wrong event to determine the row selection. Are you using "RowEnter"? I suggest you use "SelectionChanged" and then access the "SelectedRow" property.
It depends to which event you subscribe, I suppose it's SelectionChanged ?
If so - define some boolean variable (like dgvIsInitialized), set it false by default, then load data into datagridview datasource and - after loading - set it true.
And in SelectionChanged event do similar to this:
if (dgvIsInitialized)
MessageBox.Show("Selection changed");

DatagridView Checkbox Checked?

i have a check box in a datagridview windows form and have a event handler cell_Click
on cell click i check the datagridview column for a check box it shows true if the cell is selected too(that is the check box is unchecked and only the the datagrid view cell is selected) and the check box is not selected .i tried for the column gettype and found out the type it shows DatagridViewCheckBox but wrong checked values .???
If I understand you correctly you are saying the checkbox value does not align with the underlying data?
This may well be because the data has been updated and is 'dirty', e.g. it hasn't been committed to the datasource yet. If you add an event handler like this:
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell is System.Windows.Forms.DataGridViewCheckBoxCell)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Then that should update the datasource and you'll have the correct checkbox state when you query the cell.
Several things here:
the cell click event just means that the user clicked with the mouse button on the data grid view, what you're looking for is probably the CellValueChanged
this event will give you the coordinates of the cell that changed. You should check to see if it's in your check box column, then get a reference to the cell and you can check the cell.Value to see if it's true or false. You're not going to find any values on the DataGridViewCheckBoxColumn -- it's going to be at the cell level, and you'll always find the value stored in cell.Value, no matter what type of column it is.

Categories