Reputation: 1403
how to edit a particular cell in a datagrid and accept data in that cell with textbox.the other cells in the datagrid need not to be editable in C#.net 2005 thank s in advance
Upvotes: 0
Views: 821
Reputation: 851
If you're attempting to update directly on the datagrid:
// Override the OnMouseClick event.
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
if (base.DataGridView != null)
{
// Get the location (Row/Column) of the selected cell.
Point point1 = base.DataGridView.CurrentCellAddress;
// e.ColumnIndex/e.RowIndex can be replaced with a hard-coded
// value if you only want a specific cell, row, or column to
// be editable.
if (point1.X == e.ColumnIndex &&
point1.Y == e.RowIndex &&
e.Button == MouseButtons.Left &&
base.DataGridView.EditMode !=
DataGridViewEditMode.EditProgrammatically)
{
// Open the cell to be edited.
base.DataGridView.BeginEdit(true);
}
}
}
This will allow a user to edit the cell directly. If you hard-code a value in place of e.ColumnIndex, (example: hard code a 5), then only the 5th column will be editable. The same works for e.RowIndex.
Upvotes: 1
Reputation: 94653
Try it,
DataTable dt = new DataTable();
dt.Columns.Add("No",typeof(int));
dt.Columns.Add("Name");
dt.Rows.Add(1, "A");
dt.Rows.Add(2, "B");
dt.Columns[0].ReadOnly = true;
dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystroke;
dataGridView1.DataSource =dt;
Upvotes: 1