编辑Datagrid行

时间:2009-09-14 13:48:39

标签: c#

如何编辑数据网格中的特定单元格并使用文本框接受该单元格中的数据。数据网格中的其他单元格无需可编辑 在C#.net 2005中 提前谢谢

2 个答案:

答案 0 :(得分:1)

试试吧,

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;

答案 1 :(得分:1)

如果您尝试直接在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(例如:硬编码5),那么只有第5列可编辑。这同样适用于e.RowIndex。

相关问题