如何删除DataGridView中的当前单元格?

时间:2013-07-03 14:17:15

标签: c# winforms datagridview

如何删除DataGridView中的当前单元格。我正在尝试下面的代码,但它正在删除除当前代码之外的所有单元格。

private void dgvPurchase_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    for (int i = 0; i < datagridview.Columns.Count; i++)
    {
        for (int j = 0; j < datagridview.Rows.Count; j++)
        {
            datagridview.Rows[j].Cells[i].Value="";
        }
    }
}

4 个答案:

答案 0 :(得分:1)

以下链接描述了如何实现您的目标,
请检查链接,我相信这对您有帮助,

http://msdn.microsoft.com/en-us/library/yc4fsbf5.aspx

答案 1 :(得分:1)

我不知道你为什么要删除CellValidating Event中的单元格值。

要删除CurrentCell中的值,您可以尝试

DataGridView1.CurrentCell.Value = ""

答案 2 :(得分:0)

我不是100%肯定,但您可以使用DataGridView.CurrentCell属性,如;

int yourcolumnIndex = datagridview.CurrentCell.ColumnIndex;
int yourrowIndex = datagridview.CurrentCell.RowIndex;

datagridview.Rows[yourrowIndex ].Cells[yourcolumnIndex].Value="";

甚至使用DataGrid.CurrentCellChanged事件可能是更好的方法。

答案 3 :(得分:0)

DataGridName.Rows.RemoveAt(DataGridName.CurrentCell.RowIndex);