I have a DevExpress gridview that loads on my screen. Initially, I do not want it to have a focus on any row. Only after a user selects a row do I want there to be focus. Is there a way to achieve this?
if you have multiple rows selection option enabled on your grid you can call the
gridView1.ClearSelection();
method on the gridview. However if it is not enables you have to call
gridView1.UnselectRow(5);
method. You can read all such methods here
with Mvvm pattern you can try this.
I had the same problem and have solved it by overriding the OnPropertyChanged event of the GridView
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { base.OnPropertyChanged(e); this.ClearSelection(); }
Related
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
I notice there is a "AutoGenerateSelectionButton" function but it's not really what I want. I want to be able to two things when a row is clicked (anywhere of the row):
change the color of that entire row.
get the value of a specific column and update another table accordingly.
How can I acheive that without writing client-side javascript functions?
Assuming this is a webform, you need to access SelectedIndexChanged event of the gridview using a codebehind file.
from here you can modify properties
protected void ChangedRow(object sender, EventArgs e)
{
this.GridView1.SelectedRow.BackColor = System.Drawing.Color.Red;
....
}
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)
{
}
}
I have a gridview control inside another gridview control. When I click on the edit button the second gridview inside the 1st one should bind with a data source.
I am using a SqlDataSource and configured it. In which event of the gridview do I need to write the code for binding the records?
I am new to .net.
You can use RowCommand Event
protected void SecondGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
FirstGrid.DataBind()
}
}
You need to write your data binding logic in Parent grid's OnEditCommand event. Further details can be found here
I'm using a DGV to show a list of images with text captions as a picklist. Their must always be a one and only one selection made in the list. I can't find a way to prevent the user from clearing the selection with a control-click on the selected row.
Is there a property in the designer I'm missing that could do this?
If I have to override the behavior in the mouse click events are there other ways the user could clear the current selection that need covered as well?
Is there a third approach I could take that's less cumbersome than my second idea?
The easiest way is to catch the SelectionChanged event and check to see if the user has unselected all rows. If so, reselect the previously selected row. Essentially, you're intercepting their action and switching the selection back. Something like this (code untested but you will get the idea):
DataGridViewRow last_selected_row;
private void dgv_SelectionChanged(object sender, EventArgs e)
{
if (dgv.SelectedRows.Count == 0)
last_selected_row.Selected = true;
else
last_selected_row = dgv.SelectedRows[0];
}
Depending on your application, it might be better to store the row index rather than a reference to the row itself. Also be sure to initialize last_selected_row and update it if you delete any rows.
Any other controls that hook the SelectionChanged event will need to safely handle the case that no rows are selected, in case they fire before the event that switches it back. They can just return immediately though, safe in the knowledge that SelectionChanged will fire again momentarily.
You could also subclass DataGridView and override the OnSelectionChanged method. Then you could reselect the last selected row before the event fires (it will fire when you call base.OnSelectionChanged).
A DGV got a property called multiselect, if you set it to false only one cell/row can be selected at a time.
Just handle the DataBindingComplete event of the datagridview like this:
private void datagridview1_DataBindingComplete(System.Object sender, System.Windows.Forms.DataGridViewBindingCompleteEventArgs e)
{
datagridview1.ClearSelection();
}