DataGridView - Focus a specific cell
How to set focus on any specified cell in DataGridVi开发者_开发百科ew? I was expecting a simple way like Focus(rowindex,columnindex) but it is not that easy.
Set the Current Cell like:
DataGridView1.CurrentCell = DataGridView1.Rows[rowindex].Cells[columnindex]
or
DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5)
and you can directly focus with Editing by:
dataGridView1.BeginEdit(true)
you can set Focus
to a specific Cell
by setting Selected
property to true
dataGridView1.Rows[rowindex].Cells[columnindex].Selected = true;
to avoid Multiple Selection just set
dataGridView1.MultiSelect = false;
the problem with datagridview is that it select the first row automatically so you want to clear the selection by
grvPackingList.ClearSelection();
dataGridView1.Rows[rowindex].Cells[columnindex].Selected = true;
other wise it will not work
I had a similar problem. I've hidden some columns and afterwards I tried to select the first row. This didn't really work:
datagridview1.Rows[0].Selected = true;
So I tried selecting cell[0,0]
, but it also didn't work, because this cell was not displayed. Now my final solution is working very well:
datagridview1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
datagridview1.CurrentCell = datagridview1.FirstDisplayedCell;
So this selects the complete first row.
public void M(){
dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
dataGridView1.CurrentCell.Selected = true;
dataGridView1.BeginEdit(true);
}
Just Simple Paste And Pass Gridcolor() any where You want.
Private Sub Gridcolor()
With Me.GridListAll
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
'.DefaultCellStyle.SelectionBackColor = Color.MediumOrchid
End With
End Sub
in event form_load (object sender, EventArgs e) try this
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count1].Cells[0];
this code make focus on last row and 1st cell
//For me it's the best way to look for the value of a spezific column
int seekValue = 5;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
var columnValue = Convert.ToInt32(row.Cells["ColumnName"].Value);
if (columnValue == seekValue)
{
dataGridView1.CurrentCell = row.Cells[0];
}
}
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
int row = e.RowIndex;
int col = e.ColumnIndex;
if (row < 0 || col != 3)
return;
if (e.FormattedValue.ToString().Equals(String.Empty))
{
}
else
{
double quantity = 0;
try
{
quantity = Convert.ToDouble(e.FormattedValue.ToString());
if (quantity == 0)
{
MessageBox.Show("The quantity can not be Zero", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true;
return;
}
}
catch
{
MessageBox.Show("The quantity should be decimal value.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true;
return;
}
}
}
You can try this for DataGrid:
DataGridCellInfo cellInfo = new DataGridCellInfo(myDataGrid.Items[colRow], myDataGrid.Columns[colNum]);
DataGridCell cellToFocus = (DataGridCell)cellInfo.Column.GetCellContent(cellInfo.Item).Parent;
ViewControlHelper.SetFocus(cellToFocus, e);
精彩评论