C# 删除DataGridView C中的行#
我尝试循环查看我的C# 删除DataGridView C中的行#,c#,.net,vb.net,datagridview,row,C#,.net,Vb.net,Datagridview,Row,我尝试循环查看我的dataGridView1,并删除不满足以下条件的行: foreach (DataGridViewRow row in dataGridView1.Rows) { if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0) { dataGridView1.Rows.Remove(row); //error: Uncommitted new row can
dataGridView1
,并删除不满足以下条件的行:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
{
dataGridView1.Rows.Remove(row); //error: Uncommitted new row cannot be deleted.
}
}
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
如果(!(Convert.ToDateTime(row.Cells[7].Value)-DateTime.Today)。天尝试放置以下条件:
foreach(DataGridViewRow row in dataGridView1.Rows)
{
if(!row.IsNewRow)
{
if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
{
dataGridView1.Rows.Remove(row);
}
}
}
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
如果(!row.IsNewRow)
{
如果(!(Convert.ToDateTime(row.Cells[7].Value)-DateTime.Today)。天尝试放置以下条件:
foreach(DataGridViewRow row in dataGridView1.Rows)
{
if(!row.IsNewRow)
{
if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
{
dataGridView1.Rows.Remove(row);
}
}
}
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
如果(!row.IsNewRow)
{
如果(!(Convert.ToDateTime(row.Cells[7].Value)-DateTime.Today)。天尝试
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
if(!(row.Cells.OfType().All(c=>c.Value==null))
{
如果(!(Convert.ToDateTime(row.Cells[7].Value)-DateTime.Today)。天尝试
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
if(!(row.Cells.OfType().All(c=>c.Value==null))
{
如果(!(Convert.ToDateTime(row.Cells[7].Value)-DateTime.Today).Days不使用foreach
在这种情况下,循环集合可能会被修改并导致不可预测的结果,有时会抛出异常,比如集合被修改(主要在LINQ
中遇到),请改用for
:
for(int i = dataGridView1.RowCount-1; i >= 0; i--){
var row = dataGridView1.Rows[i];
if (!row.IsNewRow&&!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0){
dataGridView1.Rows.Remove(row);
}
}
for(int i=dataGridView1.RowCount-1;i>=0;i--){
var row=dataGridView1.Rows[i];
如果(!row.IsNewRow&!(Convert.ToDateTime(row.Cells[7].Value)-DateTime.Today).Days不使用foreach
在这种情况下,可能会修改循环集合并导致不可预测的结果,有时会引发异常,如集合被修改(主要在LINQ
中遇到),请改用for
:
for(int i = dataGridView1.RowCount-1; i >= 0; i--){
var row = dataGridView1.Rows[i];
if (!row.IsNewRow&&!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0){
dataGridView1.Rows.Remove(row);
}
}
for(int i=dataGridView1.RowCount-1;i>=0;i--){
var row=dataGridView1.Rows[i];
if(!row.IsNewRow&!(Convert.ToDateTime(row.Cells[7].Value)-DateTime.Today).Days从数据源中删除记录要比从datagridview中删除记录好。从数据源中删除记录要比从datagridview中删除记录好。我已经尝试了您的代码,但不是所有行都被删除,即使它们满足条件。我试过您的代码,但不是所有行都被删除行被删除,即使它们满足条件。