删除datagridview的单元格值 [英] deleting cell values of datagridview
问题描述
我有一个datagridview.我想选择一些散布在不同列和行中的单元格.当我按Delete键时,此单元格应该很清楚.我可以按以下方式为文本类型的单元格实现此目的(我使用MS Access作为数据库,并且所有字段均为数字)
I have a datagridview. i want to select some cells which spreaded in different columns and rows. when i hit Delete key this cells should be clear. i could achieve this for cells of text type as follows (I use MS Access as the database and all fields are numbers)
if (e.KeyData == Keys.Delete)
{
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
// if keep column in text type in MS access
string i = "";
cell.Value = (object)i;
}
这确实清除了所有已删除的单元格.但是如果细胞是数字我该怎么办.删除时如何为这些数字分配空值.
this really clear all deleted cells. but what shall i do if the cells are numbers . how i can assign null value to these numbers while deleting.
if (e.KeyData == Keys.Delete)
{
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
// if keep column in text type in MS access
int i =0;//here i want some null value
cell.Value = (object)i;
}
上面的代码使所有已删除的单元格都为零.我想避免这种情况.我不想将零放置为null.
我做的另一种方法是
the above code make all deleted cell with zeros . I want to avoid that situation. I dont want to place a zero for null.
another approach I did is
if (e.KeyData == Keys.Delete)
{
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
cell.Value = cell.DefaultNewRowValue;
}
但这在我选择多于一行的单元格时不起作用.我该怎么解决;
请有人帮忙......
but this do not work when i select cells of more than one row . how i can solve it;
please somebody help please......
推荐答案
让单元格值作为字符串
例如:
int i = 2
cell.value =(i).tostring()
我认为您可以将cell用作字符串进行使用解析
读取值....
If(cell.value.tosting().trim()!=")
Int i = int.Parse(cell.value.tostring())
Let cell value as string
exmple:
int i=2
cell.value= (i).tostring()
i think you may use cell as string a use parsing
to read value ....
If(cell.value.tosting().trim()!="")
Int i = int.Parse(cell.value.tostring())
简单尝试cell.value = null;
必须工作
SIMPLY try cell.value = null;
must work
这篇关于删除datagridview的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!