My Question is about to disable and Enable focused cell of Grid View and change disable cell to color on Key press
but i don't have any idea how to do that I Have done specific row Enabled False using row state helper class But now i want to disable cell How can i do that?
You can use GridView's ShowingEditor event:
private void gridViewTest_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
{
GridView gridView = sender as GridView;
if(gridView.FocusedColumn.FieldName == "Product Name") // && <PUT OTHER CONDITIONS HERE>
e.Cancel = true;
}
Related
I have grid view, and I have already disable the column auto width so I can manually set the size of column. after manually resize there's a extra blank column. and what I want is :
To disable the select row function when i click on outside of the column in Active, code and generate
Remove the extra column.
I already success fully hide the remaining column with this event or code
private void gridView1_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == null)
{
e.Handled = true;
}
}
The thing is I still can click on the outside of the genre, and the row selection still follow where I click
Handle the GridView.MouseDown event in the following way:
private void gridView1_MouseDown(object sender, MouseEventArgs e) {
GridView view = (GridView)sender;
var hi = view.CalcHitInfo(e.Location);
Console.WriteLine(hi.HitTest);
if (hi.InRow && !hi.InRowCell)
DXMouseEventArgs.GetMouseArgs(e).Handled = true;
}
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 created a DataGridView that is bound to a DataTable as returned by Postgres:
dgOrderLines.DataSource = Program.DB.GetView(view);
I have implemented the CellEndEdit event:
dgOrderLines.CellEndEdit +=
new DataGridViewCellEventHandler(dgOrderLines_CellEndEdit);
Now, when I start editing a field, the grid automatically moves a "new record" into view that can be used if the edited record is saved:
However, when I update the "new record" from CellClick, when the user clicks the DataGridViewButtonColumn, it is not added:
Adding by hand is not possible, because the DataGrid is bound.
The code for editing the cell:
void dgOrderLines_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
// Get the edited cell
DataGridViewCell cell = dgOrderLines.Rows[e.RowIndex].Cells[e.ColumnIndex];
string column_name = dgOrderLines.Columns[e.ColumnIndex].Name;
if (cell.Value != null) {
UpdateField(e.RowIndex, column_name, cell.Value.ToString());
}
}
and, to update the row when using the button:
private void dgOrderLines_CellClick(object sender, DataGridViewCellEventArgs e) {
if (dgOrderLines.Columns[e.ColumnIndex].CellType.
Name.Equals("DataGridViewButtonCell")) {
// art.SelectedArticles[0] holds the articleid for this record
UpdateField(e.RowIndex, "articleid", "1234");
}
}
I found it.
Apparently, when you start editing in a TextBox cell, the cell is marked "dirty" as soon as you type something. So, all I had to do is make my Button cell dirty. And of course, StackOverflow helped me immediately.
All I had to do was:
myGrid.NotifyCurrentCellDirty(false);
before I start editing and (on CellClick)
myGrid.NotifyCurrentCellDirty(true);
after I write out data to my Button column.
How can I make a specific row cell readonly(not editable) on XtraGrid? For example just for row[0] but not all rows.
You can use the GridView.CustomRowCellEdit event:
//...
var repositoryItemTextEditReadOnly = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
repositoryItemTextEditReadOnly.Name = "repositoryItemTextEditReadOnly";
repositoryItemTextEditReadOnly.ReadOnly = true;
//...
void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) {
if(e.RowHandle == 0)
e.RepositoryItem = repositoryItemTextEditReadOnly;
}
You can use the ColumnView.ShownEditor event:
void gridView1_ShownEditor(object sender, EventArgs e)
{
ColumnView view = (ColumnView)sender;
view.ActiveEditor.Properties.ReadOnly = view.FocusedRowHandle == 0;
}
Source: How to Conditionally Prevent Editing for Individual Grid Cells
When you need to make a grid cell read-only based on a condition, the
best approach is to use the ShowingEditor event of the
GridView and prevent editing via the e.Cancel parameter passed to the event. Simply set it to True when it is necessary to prevent
editing.
// disable editing
private void gridView1_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e) {
GridView view = sender as GridView;
e.Cancel = view.FocusedRowHandle == 0;
}
Source - How to display disabled buttons for particular cells within a ButtonEdit column
Another approach is that assign a read only repository editor control as #DmitryG suggested and I have also implement that way some times when there was a column which contains a button.
In your case you should create two TextEdit repository items. One with
the enabled button and another with the disabled button. Then handle
the GridView.CustomRowCellEdit event and pass the necessary
repository item to the e.RepositoryItem parameter according to a
specific condition. Please see the Assigning Editors to Individual
Cells help topic for additional information.
private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
if (e.Column.Caption == "Any2")
{
if (e.RowHandle == 0)
e.RepositoryItem = columnReadOnlyTextEdit;
else
e.RepositoryItem = columnTextEdit;
}
}
References:
How to customize the Look-And-Feel of my grid cells
How to make my grid columns read-only
Here I have a column name "Status" in which I am using ComboBox whose values are "Pending" and "Delivered" now I want that when a user selects the delivered from the ComboBox then the entire row or this control should be disabled so user could not change it again and by default its value should be "Pending" how can i do it? Is it possible to disable a single row in gridview?
You cannot disable an individual row. However you can make it readonly:
DataGridView1.Rows[rowIndex].ReadOnly = true;
(This makes row with index of rowIndex set to readonly).
For your specific example, you would want to handle the CellValueChanged event of the datagridview and have code along the lines of:
void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4 && DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "Delivered")
{
DataGridView1.Rows[e.RowIndex].ReadOnly = true;
}
}
You can handle the RowChanged event and do something like
if (Status == 'Delivered')
{
e.Row.Enabled = False;
}