0

我在绑定到 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));
        }
    }
}

}

}

4

1 回答 1

0

我真的没有看到比您提供的更容易的替代解决方案。您不应该将某种 CellStyle 与您的数据绑定对象混合,因为这不是一个好习惯。

我唯一建议的是禁用 AutoGenerateColumns 并定义自己的样式:

private static void AdditionalInitialization(DataGridView dgv, object dataSource) {

 dgv.AutoGenerateColumns = false;
 dgv.DataSource = dataSource; // Needs to occur after the auto generate columns is set to off
 DataGridViewTextColumn msgCol = new DataGridViewTextColumn();
 msgCol.HeaderText = "Message";
 msgCol.DataPropertyName = "Message";
 msgCol.DefaultCellStyle.ForeColor = Color.Red;
 dgv.Columns.Add(msgCol);
}

此方法可以放置在您的任何类中,因为它是静态的,即您的 InputFileInfo.cs

然后,当您的表单加载时,请执行以下操作:

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());
        InputFileInfo.AdditionalInitialization(datagridview1,_filesToParse);

}

于 2011-08-09T18:27:38.263 回答