I'm working in RadGridView where i need to copy the cell content.
As part of the application functionality, i want the Grid SelectionUnit to always be of type Row. In addition to this, we also want to expose a mechanism where the user can select a cell value for copy to clipboard.
Is there a way to do this? For example, double clicking a cell copies the cell value where as single click always selects the row.
Try this,
Not a perfect one, but for your requirement it suits.
void testGridView_CopyingCellClipboardContent(object sender, GridViewCellClipboardEventArgs e)
{
if (grdName.CurrentCell != null && grdName.CurrentCell.Column == e.Cell.Column)
{
e.Cancel = false;
}
else
e.Cancel = true;
}
Where replace your GridView name with "grdName".
Related
I need to allow the user to select a row by clicking on the row header to allow row deletion, but when the row header is clicked, the first cell gets focus and the text is highlighted, which I need to prevent so the user can't make accidental edits to that first cell.
Here's an example of what's happening:
As you can see, the first cell has focus and the user can edit that cell, when all that was done was the row header was clicked.
I tried the answer to this SO question, but neither event fired when I clicked a row header. Here's the code that I used for that attempt:
private void dgvCategories_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
if (e.StateChanged == DataGridViewElementStates.Selected)
{
Console.WriteLine("true");
dgvCategories.ReadOnly = true;
}
}
private void dgvCategories_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.StateChanged == DataGridViewElementStates.Selected)
{
Console.WriteLine("false");
dgvCategories.ReadOnly = false;
}
}
I also tried experimenting with the various DataGridViewEditMode options, but none of those accomplished what I need.
The DGV SelectionMode is set to RowHeaderSelect.
I still need to be able to grab the hidden "id" field in the row for the delete process. Here's my code for that:
private void btnDeleteRow_Click(object sender, EventArgs e)
{
int index = dgvCategories.SelectedRows[0].Index;
string rowId = dgvCategories[0, index].Value.ToString();
string deleteString = string.Format("DELETE FROM masterfiles.xref WHERE id = " + rowId);
conn.Open();
NpgsqlCommand deleteCmd = new NpgsqlCommand(deleteString, conn);
deleteCmd.ExecuteNonQuery();
conn.Close();
}
That works; I just need to prevent that first cell from automatically getting focus for editing when the row is selected. I still need to allow the user to select individual cells for editing, as well.
Try :
1- make the DataGridView selection by one row instead of one cell .
2- Disable allowing user to edit the DataGridView.
3- Make DataGridView ReadOnly .
Do 1 & 2 all the time except at editing operation .
In telerik Rad GridView (2014) Winform, i have a gridview with a disabled column but if i type an especific text in another column i want that column to be enabled to write, but only the cell for that specific row
Since you want to some cells editable, you should not disable the whole column. For the purpose of your case, you can use the CellBeginEdit event, which you can cancel if your condition is not met, hence the user will not be able to edit the specified cell:
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
if (e.Column.Name == "columnAtHand" && e.Row.Cells["DependantColumn"].Value == "disallowEdit")
{
e.Cancel = true;
}
}
I have a simple RadGridView, inside the first cell i have Checkbox in which I want to change the color of my row if this Checkbox is checked state and return to the original color if this Checkbox is in unchecked state.
Currently this is what i have try and this paint my Row when my Checkbox is checked but how can i change to the original color when my Checkbox is uncheckd ?
private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
{
if ((bool)e.RowElement.RowInfo.Cells["Select"].Value == true)
{
e.RowElement.DrawFill = true;
e.RowElement.GradientStyle = GradientStyles.Solid;
e.RowElement.BackColor = Color.SkyBlue;
}
else
{
e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
}
}
The code you have is correct and it should change the color of the rows with checked first cell. However, there is one thing to note. The editor of the check box cell is permanent editor, and its value will be submitted to the cell, only after you change the current row in the grid. If this does not work for you, there is an easy way to submit the value immediately upon changing the check box state. Here you can find out how to do it: http://www.telerik.com/help/winforms/gridview-editors-howto-checkboxeditor-submit-value-change.html
I am using the devexpress TreeList control. In Treelist I have a situation where one of my column is read only. This column may have some text values added when something happens in another cell. I have restricted user entry in the cell by setting a property like this
treeList1.Columns["col3"].OptionsColumn.ReadOnly = true;
Now I want to remove text value from some of the cells and since it is read only the delete buttons does not work. Can you suggest the event/method and the code which will allow user to delete the text?
Any help would be much appreciated.
Edited solution :
You should know that when the cursor is in the cell (in edit mode), and you press a button, it's not the TreeList who send the KeyDown event, but the RepositoryItemButtonEdit. So, you should handle the event also for the RepositoryItemButtonEdit.
To not duplicate code, I've wrote one handler 'onKeyDown', in whitch I verify who is the sender.
treeList1.KeyDown += onKeyDown;
riButtonEdit.KeyDown += onKeyDown;
And here is a code exemple showing you how to handle the KeyDown event for both treeList and repositoryButtonEdit, and set the cell value to null :
private void onKeyDown(object sender, KeyEventArgs e)
{
// Test if the button pressed is the delete button
if (e.KeyCode != Keys.Delete)
return;
// Test if the focused column is colValue
if (treeList1.FocusedColumn != colValue)
return;
// Set the cell value to null
treeList1.FocusedNode.SetValue(colValue, null);
// If it's the ButtonEdit who send the event, make it's EditValue null
var btnEdit = sender as ButtonEdit;
if (btnEdit != null)
{
btnEdit.EditValue = null;
}
}
I have a simple question which i am not able to solve myself.
I have an ObjectListView filled with some of my objects. But in addition to that I want to have another column, with a default text "Delete". On clicking that column, the selected Row should be deleted. How do I do that?
You can achieve this by making the desired row editable and use the CellEditActivation event. Initialize your OLV and "delete-column" as follows:
// fire cell edit event on single click
objectListView1.CellEditActivation = ObjectListView.CellEditActivateMode.SingleClick;
objectListView1.CellEditStarting += ObjectListView1OnCellEditStarting;
// enable cell edit and always set cell text to "Delete"
deleteColumn.IsEditable = true;
deleteColumn.AspectGetter = delegate {
return "Delete";
};
Then you can remove the row in the CellEditStarting handler as soon as the column is clicked:
private void ObjectListView1OnCellEditStarting(object sender, CellEditEventArgs e) {
// special cell edit handling for our delete-row
if (e.Column == deleteColumn) {
e.Cancel = true; // we don't want to edit anything
objectListView1.RemoveObject(e.RowObject); // remove object
}
}
To improve on this, you can display an image in addition to the text.
// assign an ImageList containing at least one image to SmallImageList
objectListView1.SmallImageList = imageList1;
// always display image from index 0 as default image for deleteColumn
deleteColumn.ImageGetter = delegate {
return 0;
};
Result:
If you don't want to display any text next to the image you can use
deleteColumn.AspectToStringConverter = delegate {
return String.Empty;
};
You could also set the Aspect to an empty string, but consider this as "best practice". By still returning an aspect, sorting and grouping will still work.
If the "Delete" column is not the first column in the ObjectListView, you will have to set
ShowImagesOnSubItems = true;
See also ObjectListView show icons.