简体   繁体   English

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

[英]How to delete the current cell in DataGridView?

How can I delete a current cell in a DataGridView.如何删除 DataGridView 中的当前单元格。 I am trying the code below, but it is deleting all cells except the current one.我正在尝试下面的代码,但它正在删除除当前单元格之外的所有单元格。

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="";
        }
    }
}

The following link describe how to achive what you want ,以下链接描述了如何实现您想要的,
Please check the link , I am sure that will help you ,请检查链接,我相信这会对您有所帮助,

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

I don't know why you do deleting cell value in CellValidating Event ..我不知道你为什么要删除 CellValidating Event 中的单元格值..

To delete value in CurrentCell .. you may try要删除 CurrentCell 中的值 .. 你可以尝试

DataGridView1.CurrentCell.Value = ""

I'm not %100 sure but you can use DataGridView.CurrentCell property like;我不确定 %100,但您可以使用DataGridView.CurrentCell属性,例如;

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

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

Or even using DataGrid.CurrentCellChanged event could be better approach.或者甚至使用DataGrid.CurrentCellChanged事件可能是更好的方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM