我有一个datagridview,它使用switch语句通过双击事件进行过滤。数据源最初是基于用户输入的LINQ to SQL查询设置的,然后他们可以选择双击单元格来过滤类似项目,如模型,计算机名称或操作系统。
这里似乎有太多重复的代码。有关更好的方法的任何想法吗?
private void gridInventory_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex > 0 && e.RowIndex > 0)
{
var cell = this.gridInventory[e.ColumnIndex, e.RowIndex];
var clickedValue = (cell.Value != null) ? cell.Value.ToString() : string.Empty;
if (!string.IsNullOrEmpty(clickedValue))
{
switch (this.gridInventory.Columns[e.ColumnIndex].Name.ToUpper())
{
case @"MODEL":
CurrentList = CurrentList.Where(r => r.Model != null && r.Model.ToUpper() == clickedValue.ToUpper()).ToList();
break;
case @"COMPUTERNAME":
CurrentList = CurrentList.Where(r => r.ComputerName != null && r.ComputerName.ToUpper() == clickedValue.ToUpper()).ToList();
break;
case @"SERIALNUMBER":
CurrentList = CurrentList.Where(r => r.SerialNumber != null && r.SerialNumber.ToUpper() == clickedValue.ToUpper()).ToList();
break;
case @"COMPUTERID":
CurrentList = CurrentList.Where(r => r.ComputerID.ToString().ToUpper() == clickedValue.ToUpper()).ToList();
break;
case @"MANUFACTURER":
CurrentList = CurrentList.Where(r => r.Manufacturer != null && r.Manufacturer.ToUpper() == clickedValue.ToUpper()).ToList();
break;
case @"OSVERSION":
CurrentList = CurrentList.Where(r => r.OSVersion != null && r.OSVersion.ToUpper() == clickedValue.ToUpper()).ToList();
break;
case @"AUDITDATE":
CurrentList = CurrentList.Where(r => r.AuditDate != null && r.AuditDate.ToString().ToUpper() == clickedValue.ToUpper()).ToList();
break;
case @"AUDITGUID":
CurrentList = CurrentList.Where(r => r.AuditGUID != null && r.AuditGUID.ToString().ToUpper() == clickedValue.ToUpper()).ToList();
break;
default:
break;
}
if (this.CurrentList != null)
{
gridInventory.DataSource = this.CurrentList;
this.lblRecords.Text = string.Format(@"Total Records: {0}", CurrentList.Count(c => c.ComputerID > 0));
}
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, @"error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
答案 0 :(得分:1)
使用LINQ和反射这个函数可以这样重写:
private void gridInventory_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex > 0 && e.RowIndex > 0)
{
var cell = this.gridInventory[e.ColumnIndex, e.RowIndex];
var clickedValue = (cell.Value != null) ? cell.Value.ToString() : string.Empty;
if (!string.IsNullOrEmpty(clickedValue))
{
CurrentList = CurrentList.Where(r => typeof(r_type_here).GetProperty(this.gridInventory.Columns[e.ColumnIndex].Name).GetValue(r, null) != null &&
typeof(r_type_here).GetProperty(this.gridInventory.Columns[e.ColumnIndex].Name).GetValue(r, null).ToString().ToUpper() == clickedValue.ToUpper()).ToList();
if (this.CurrentList != null)
{
gridInventory.DataSource = this.CurrentList;
this.lblRecords.Text = string.Format(@"Total Records: {0}", CurrentList.Count(c => c.ComputerID > 0));
}
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, @"error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
将r_type_here
替换为CurrentList
中实际的元素类型,您应该得到相同的结果。
<强>优点:强>
<强>缺点:强>
gridInventory.Columns
同样应该在相同的LINQ where
声明中实现对exrty exsitence的检查。