IntroductionIn one of my project, i want to edit cell in DataGrid in .NET Compact Framework.The .NET CF is did not built features to editable cell in datagrid.So i would like to implement this features with MSDN help. ImplementationThe implementation is very simple, Firstlly we have to add a textbox into form and rename "txtEPQTY" and then make it visible false and then place DataGrid Control in form add few columns as well. Now when user focus the cell in datagrid, i'm going to replace by textbox and make it visible to enter the value and then when fouse lost assign value to Cell and make textbox is visible false. Code DataGridCell editCell;
bool isEditPQTY = false;
bool isUpdateCurrentCell = false;
private void grdItems_CurrentCellChanged(object sender, EventArgs e)
{
if (!isUpdateCurrentCell)
{
if (isEditPQTY && !grdItems.CurrentCell.Equals(editCell))
{
isUpdateCurrentCell = true;
grdItems.Visible = false;
DataGridCell currentCell = grdItems.CurrentCell;
grdItems[editCell.RowNumber, editCell.ColumnNumber] = txtEPQTY.Text;
grdItems.CurrentCell = currentCell;
grdItems.Visible = true;
isUpdateCurrentCell = false;
txtEPQTY.Visible = false;
isEditPQTY = false;
}
editCell = grdItems.CurrentCell;
txtEPQTY.Text = grdItems[editCell.RowNumber, editCell.ColumnNumber].ToString();
Rectangle cellPos = grdItems.GetCellBounds(editCell.RowNumber, editCell.ColumnNumber);
txtEPQTY.Left = cellPos.Left - 1;
txtEPQTY.Top = cellPos.Top + grdItems.Top - 1;
txtEPQTY.Width = cellPos.Width + 2;
txtEPQTY.Height = cellPos.Height + 2;
txtEPQTY.Visible = true;
isEditPQTY = true;
}
}
}That's all, just copy and paste this code and run it. |