private void gridView1_ValidateRow(object sender, ValidateRowEventArgs e)
{
try
{
GridColumn col = gridView1.Columns["Qty"];
decimal val = Convert.ToDecimal(gridView1.GetRowCellValue(e.RowHandle, "Qty"));
if (val <= 0)
{
e.Valid = false;
gridView1.SetColumnError(col, "Value must be greater than 0");
}
else
{
gridView1.PostEditor();//if everything goes well update the record in db
}
}
catch (Exception)
{
throw;
}
}
private void gridView1_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
{
e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
// if message display is reuired just use DisplayError instead of NoAction
}
{
try
{
GridColumn col = gridView1.Columns["Qty"];
decimal val = Convert.ToDecimal(gridView1.GetRowCellValue(e.RowHandle, "Qty"));
if (val <= 0)
{
e.Valid = false;
gridView1.SetColumnError(col, "Value must be greater than 0");
}
else
{
gridView1.PostEditor();//if everything goes well update the record in db
}
}
catch (Exception)
{
throw;
}
}
private void gridView1_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
{
e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
// if message display is reuired just use DisplayError instead of NoAction
}
Row Selection
Some times we need to select a row from code behind.It is a good practice to check if a row is selected at all. we can use an if statement to do so.if (gridView1.SelectedRowsCount > 0)
{
if its greater than one than do some thing
}
The following code selects a row pragmatically in xtragrid.
gridView1.SelectRow(rowHandle);
Row Selection based on other grid
The following code selects a row when a row is selected in another xtragrid.Place two grids on the form.
In first grid there should be an ID column(any column you may prefer to find it by its value)
In second grid a column should have the same value as in first grid.
After setting up the grids we use LocateByValue method and select the row by using SelectRow(int rowhandle) method.
int ID = Convert.ToInt32(gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "IDOne"));
int rowHandle = gridView1.LocateByValue("ID", ID);
if (rowHandle != DevExpress.XtraGrid.GridControl.InvalidRowHandle)
{
gridView1.FocusedRowHandle = rowHandle;
gridView1.SelectRow(rowHandlegridOne);
}
Row Style
The following code changes the color of a selected row in xtragrid and disables the default row color.
private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
{
GridView view = sender as GridView;
{
view.OptionsSelection.EnableAppearanceFocusedCell = false;
view.OptionsSelection.EnableAppearanceFocusedRow = false;
view.OptionsSelection.EnableAppearanceHideSelection = false;
if (view.IsRowSelected(e.RowHandle))
{
e.Appearance.BackColor = Color.Salmon;
e.Appearance.BackColor2 = Color.SeaShell;
e.Appearance.ForeColor = Color.White;
}
}
For further question please mail: brainstormiert@gmail.com{
view.OptionsSelection.EnableAppearanceFocusedCell = false;
view.OptionsSelection.EnableAppearanceFocusedRow = false;
view.OptionsSelection.EnableAppearanceHideSelection = false;
if (view.IsRowSelected(e.RowHandle))
{
e.Appearance.BackColor = Color.Salmon;
e.Appearance.BackColor2 = Color.SeaShell;
e.Appearance.ForeColor = Color.White;
}
}