C# 编辑数据网格行
如何编辑datagrid中的特定单元格,并使用textbox接受该单元格中的数据。datagrid中的其他单元格不需要编辑 在C#net 2005中 提前感谢s试试看C# 编辑数据网格行,c#,C#,如何编辑datagrid中的特定单元格,并使用textbox接受该单元格中的数据。datagrid中的其他单元格不需要编辑 在C#net 2005中 提前感谢s试试看 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; d
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;
如果您试图直接在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);
}
}
}
这将允许用户直接编辑单元格。如果硬编码一个值来代替e.ColumnIndex(例如:硬编码A5),则只有第5列是可编辑的。这同样适用于e.RowIndex