I want to change the focus on my grid cell.
Suppose I click on a cell[1,1] then I want to set the focus on cell[1,2].
Where cell[1,1] means cell of (column 1) and (row 1)
You could use the AfterCellActivate event and write this code
void grid_AfterCellActivate(object sender, EventArgs e)
{
if (grid.ActiveCell != null &&
grid.ActiveCell.Column.Index == 1 &&
grid.ActiveCell.Row.Index == 1)
{
grid.ActiveCell = grid.Rows[1].Cells[2];
// And if you want also to automatically
// put your cell in edit mode add this line
grid.PerformAction(UltraGridAction.EnterEditMode);
}
}
Related
I have a C# devexpress project. I am using the checkbox for my project. Here is the screenshot of my design.
I want to click the "In Price" and "Out Price" cell, and select the rows (multiple. To be exact: I have a number of rows. If I click a cell, the cell will be selected, if I click another row cell, the first-row will still remain selected, and then the second one also be selected.)
Here is a demo of what I exactly expecting:
I am assuming that, because of the Toggle button, only if I click on the checkbox column, it is selected. If I need to turn it off, How can I do that?
If I need to write code what I am expecting, the code will be written:
private void gridView2_RowCellClick(object sender, RowCellClickEventArgs e)
{
if (e.Column.FieldName == "InPrice" || e.Column.FieldName == "OutPrice")
{
//Here I need to write the code.
}
}
private void gridView2_RowCellClick(object sender, RowCellClickEventArgs e)
{
if (e.Column.FieldName == "InPrice" || e.Column.FieldName == "OutPrice")
{
if ((Control.ModifierKeys & Keys.Control) != Keys.Control)
{
GridView view = sender as GridView;
GridHitInfo hi = view.CalcHitInfo(e.Location);
if(hi.InRowCell)
{
view.FocusedRowHandle = hi.RowHandle;
view.FocusedColumn = hi.Column;
view.ShowEditor();
CheckEdit edit = (view.ActiveEditor as CheckEdit);
if(edit != null)
{
edit.Toggle();
(e as DevExpress.Utils.DXMouseEventArgs).Handled = true;
}
}
}
}
}
This code works what I exactly wanted. If anyone need this, it may help you.
I want to hide one or two grid cells from my datagriview. But with this code all the grids are hidden and this is not what I want.
I want to hide one or two rectangles cells from my Datagridview.
I do not want to hide columns or data which Contain my cells .
I just wanna hide a Specified cells.
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
The recommended way to hide or modify cell border style is to code the CellPainting event.
Don't worry, no actual painting is required. All you need to do is set a few fields in the e.AdvancedBorderStyle parameter.
Here is an example:
Note the 'vertically merged' look of of the cells in the 3rd column; same for the 'horizontally merged' cells at the bottom. Also the double border of a cell in the 5th column.
private void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 2 && e.RowIndex == 6)
e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
if (e.ColumnIndex == 2 && e.RowIndex == 1)
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
if (e.ColumnIndex == 4 && e.RowIndex == 4)
{
e.AdvancedBorderStyle.All = DataGridViewAdvancedCellBorderStyle.InsetDouble;
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;
}
}
Note that hiding borders is rather straight forward : Simply hide the right or the bottom border; other borderstyles require some trial and error (or a deeper understanding ;-)
Here I first set the style for all sides but as it paints the botton white (at least that's what I think it does) I then set the botton border back to single.
You may want to streamline the way the checks are done; this is just a simple example.
Update:
Here is a code to make the merging more dynamic: Use the mergeCells function to mark a cell for merging or un-merging with its right or bottom neighbour:
private void mergeCells(DataGridViewCell cell, bool mergeH, bool mergeV)
{
string m = "";
if (mergeH) m += "R"; // merge horizontally by hiding the right border line
if (mergeV) m += "B"; // merge vertically by hiding the bottom border line
cell.Tag = m == "" ? null : m;
}
The CellPainting now looks like this:
private void customDGV1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
DataGridViewCell cell = ((DataGridView)sender)[e.ColumnIndex, e.RowIndex];
if (cell.Tag == null) return;
string hide = cell.Tag.ToString();
if (hide.Contains("R"))
e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
else
e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single;
if (hide.Contains("B"))
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
else
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;
}
Update 2:
If you want to apply this to the ColumnHeaders you need to turn off dgv.EnableHeadersViualStyles first..
I have a data-gird view, and its first cell is student register no. and the cell is auto complete text box.When we focus on any of the data in auto compete text box then corresponding data shown in next cell. How can I do it? Which event is used? My first cell is already an auto complete cell. My requirement is, the auto complete text box contains number of data. when cursor move upward or downward in the auto complete text box(here register.no) the next cell automatically shown student name. For this which event is used?
try this
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex.Equals(3) && e.RowIndex != -1)
{
if (dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value != null)
{
dataGridView1[1,(dataGridView1.CurrentCell.ColumnIndex + 1)].Value ="Your Value"
}
}
}
I have a DataGridView with several columns and several rows of data. One of the columns is a DataGridViewCheckBoxColumn and (based on the other data in the row) I would like the option to "hide" the checkbox in some rows. I know how to make it read only but I would prefer it to not show up at all or at least display differently (grayed out) than the other checkboxes. Is this possible?
Some workaround: make it read-only and change back color to gray.
For one specific cell:
dataGridView1.Rows[2].Cells[1].Style.BackColor = Color.LightGray;
dataGridView1.Rows[2].Cells[1].ReadOnly = true;
Or, better but more "complicated" solution:
suppose you have 2 columns: first with number, second with checkbox, that should not be visible when number > 2. You can handle CellPainting event, paint only borders (and eg. background) and break painting of rest. Add event CellPainting for DataGridView (optionally test for DBNull value to avoid exception when adding new data in empty row):
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
//check only for cells of second column, except header
if ((e.ColumnIndex == 1) && (e.RowIndex > -1))
{
//make sure not a null value
if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != DBNull.Value)
{
//put condition when not to paint checkbox
if (Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value) > 2)
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.Border | DataGridViewPaintParts.Background); //put what to draw
e.Handled = true; //skip rest of painting event
}
}
}
}
It should work, however if you change value manually in first column, where you check condition, you must refresh the second cell, so add another event like CellValueChanged:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dataGridView1.InvalidateCell(1, e.RowIndex);
}
}
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell.aspx
DataGridViewCheckBoxCell.Visible = false;
Edit: Oh, wait, it's read only. Derp.
In which case, try replacing the cell with an empty DataGridViewTextBoxCell.
Taken from Customize the Appearance of Cells in the Windows Forms DataGridView Control, you could catch the CellPainting event and not draw the cell if its in read only mode. For example:
public Form1()
{
InitializeComponent();
dataGridView1.CellPainting += new
DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}
private void dataGridView1_CellPainting(object sender,
System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
// Change 2 to be your checkbox column #
if (this.dataGridView1.Columns[2].Index == e.ColumnIndex && e.RowIndex >= 0)
{
// If its read only, dont draw it
if (dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly)
{
// You can change e.CellStyle.BackColor to Color.Gray for example
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
e.Handled = true;
}
}
}
}
The only caveat is that you need to call dataGridView1.Invalidate(); when you change the ReadOnly property of one of the DataGridViewCheckBox cell's.
Can anyone tell me how to prevent multiple cells from being selected in datagridview control?
use the MultiSelect property
edit: depending on what you want to accomplish, you might have to also use the SelectionMode property
in the dataGridView properties >> Behavior section >> set MultiSelect to false
If you are looking to prevent control clicks from multiple columns you can do the following:
private int nSelectedColumn = 0;
(add a CellClick event on the datagridview and copy in the following code:)
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (Control.ModifierKeys == Keys.Shift || Control.ModifierKeys == Keys.Control)
{
if (_nSelectedColumn != 0)
{
if (_nSelectedColumn != e.ColumnIndex)
{
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = false;
if (Control.ModifierKeys == Keys.Shift)
dataGridView.ClearSelection();
}
}
else
_nSelectedColumn = e.ColumnIndex;
}
else
_nSelectedColumn = e.ColumnIndex;
}
Also ensure multiselect is enabled and SelectionMode is set to CellSelect.
The effect is that you can control click items in a column and even shift click in the same column. The cell will deselect when you control click outside of that column and if you shift click outside of the column it will deselect everything.