我在绑定到 BindingList 的 Win Forms 应用程序中使用 DataGridView,我想改进业务逻辑和表示的“分离”。
在我的 Form_Load 事件中,我调用了一个例程来构建一个 BindingList,然后我将 DGV 的 .DataSource 设置为这个 BindingList:
private void initializeFileList(string rootFolder) // populate grid with .xml filenames to be processed
{
String root = rootFolder;
var result = Directory.GetFiles(root, "*.xml", SearchOption.AllDirectories)
.Select(name => new InputFileInfo(name))
.ToList();
_filesToParse = new BindingList<InputFileInfo>(result.ToList());
dataGridView1.DataSource = _filesToParse;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.Columns["Rows"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridView1.Columns["Message"].DefaultCellStyle.ForeColor = Color.Red;
困扰我的是最后两个陈述;如您所见,我希望对从“行”和“消息”属性创建的列进行细微的修饰。我应该在最后两行中硬编码我的自定义对象的属性,这感觉很糟糕。
是否有更优雅的方式来自定义 DGV 的这两列 - 目标是充分利用以下提供的绑定:dataGridView1.DataSource = _filesToParse; 换句话说,仍然自定义列,但是从“业务”对象中的某些内容而不是当前技术中进行自定义。
这是我的InputFileInfo类(来自同一解决方案中的另一个项目):
namespace CBMI.Common
{
public class InputFileInfo : INotifyPropertyChanged
{
private bool processThisFile;
public bool Process
{
get { return processThisFile; }
set
{
Utilities.Set(this, "Process", ref processThisFile, value, PropertyChanged);
}
}
public string FileName { get; set; }
private long rowsReturned;
public long Rows
{
get { return rowsReturned; }
set
{
Utilities.Set(this, "Rows", ref rowsReturned, value, PropertyChanged);
}
}
private string message;
public string Message
{
get { return message; }
set
{
Utilities.Set(this, "Message", ref message, value, PropertyChanged);
}
}
// constructor
public InputFileInfo(string fName)
{
Process = true;
FileName = fName;
Rows = 0;
Message = String.Empty;
}
public event PropertyChangedEventHandler PropertyChanged;
}
public static class Utilities
{
public static void Set<T>(object owner, string propName,
ref T oldValue, T newValue, PropertyChangedEventHandler eventHandler)
{
// make sure the property name really exists
if (owner.GetType().GetProperty(propName) == null)
{
throw new ArgumentException("No property named '" + propName + "' on " + owner.GetType().FullName);
}
// we only raise an event if the value has changed
if (!Equals(oldValue, newValue))
{
oldValue = newValue;
if (eventHandler != null)
{
eventHandler(owner, new PropertyChangedEventArgs(propName));
}
}
}
}
}