清除每个单元格结束编辑事件列表

时间:2014-02-02 14:49:04

标签: c#-4.0 datagridview

此处,我按comboboxselected值搜索搜索设置和项id 问题仅在更改combobox项目并按其他设置搜索时才会显示'item not found',但请确保其存在但不在当前列表中。

if (e.ColumnIndex == 0)
{
    List<ComboUnit> ItemUnitsList = new List<ComboUnit>();
    ComboUnit ItemUnitsObj = new ComboUnit();
    long x = Convert.ToInt64(dataGridView1.Rows[i].Cells[0].Value);
    string r = ddlInvSearchSet1.SelectedValue.ToString();
    TechTouch.Methods o = new TechTouch.Methods();
    List<db.Vitems> itemDetails = o.SearchItem(x, r);

    if (itemDetails.Count == 0)
    {
        {
            MessageBox.Show("NO Items Found");
            dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
            //   dataGridView1.Rows[i].Cells [2]= dataGridView1.Columns[2].Index + 1;
            dataGridView1.BeginEdit(true);
            dataGridView1.AllowUserToAddRows = false;
        }
        itemDetails.Clear();
        dataGridView1.Refresh();
    }
    else
    {
        foreach (var item in itemDetails)
        {
            ItemUnitsObj.UnitName = item.UnitNameArabic;
            ItemUnitsObj.UnitId = item.UnitId;
            ItemUnitsList.Add(ItemUnitsObj);
            ItemUnitsObj = new ComboUnit();
            dataGridView1.Rows[i].Cells[1].Value = itemDetails.First().NameArabic;
            if (itemDetails.Count > 1)
            {
                DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();
                comboBoxCell.DataSource = ItemUnitsList;
                comboBoxCell.DisplayMember = "UnitName";
                comboBoxCell.ValueMember = "UnitId";
                dataGridView1.Rows[i].Cells[2] = comboBoxCell;
            }
            else
            {
                DataGridViewTextBoxCell textBoxCell = new DataGridViewTextBoxCell();
                textBoxCell.Value = itemDetails.First().UnitNameArabic;
                dataGridView1.Rows[i].Cells[4].Value = itemDetails.First().SalePrice;
                dataGridView1.Rows[i].Cells[2] = textBoxCell;
                dataGridView1.Rows[i].Cells[7].Value = itemDetails.First().UnitId;
                dataGridView1.Refresh();
            }
        }
    }
    itemDetails.Clear();
}

1 个答案:

答案 0 :(得分:0)

在IF语句中添加itemDetails.Clear();

if (itemDetails.Count == 0)
{
MessageBox.Show("NO Items Found");
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
dataGridView1.Rows[i].Cells[0].Value = "";
dataGridView1.BeginEdit(true);
itemDetails.Clear();
}
else
{
}