1

我在设计时创建了一个 DataGridViewComboBoxColumn。但我想在运行时对其进行填充。但它没有发生。

这是整个代码

public partial class Form1 : Form
    {
        private List<FileInformation> FileInformationList;
        public Form1()
        {
            InitializeComponent();
            PrepareGrid();
            DisplayResult();
        }

        private void PrepareGrid()
        {

            var fileNameColumn = new DataGridViewTextBoxColumn
            {
                Name = @"FileName",
                HeaderText = "File Name",
                DataPropertyName = @"FileName",
                AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
                ReadOnly = false,
                Frozen = false
            };
            dataGridView1.Columns.Add(fileNameColumn);

            var downloadColumn = new DataGridViewLinkColumn

            {
                Name = @"Download",
                HeaderText = @"Download",
                DataPropertyName = @"Download",
                AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
                ReadOnly = true
            };
            dataGridView1.Columns.Add(downloadColumn);

            var dropdownColumn = new DataGridViewComboBoxColumn

            {
                Name = @"Test",
                HeaderText = @"Country",                
                AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
                ReadOnly = true
            };
            dataGridView1.Columns.Add(dropdownColumn);
        }

        private void DisplayResult()
        {
            FileInformationList = LoadItems();
            dataGridView1.DataSource = FileInformationList;

            ((DataGridViewComboBoxColumn)dataGridView1.Columns["Test"]).DataSource = GetCountryList();
            ((DataGridViewComboBoxColumn)dataGridView1.Columns["Test"]).ValueMember = "id";
            ((DataGridViewComboBoxColumn)dataGridView1.Columns["Test"]).DisplayMember = "Name";

        }

        private List<FileInformation> LoadItems()
        {
            var lstScriptInfo = new List<FileInformation>();
            for (int i = 1; i <= 5; i++)
            {
                lstScriptInfo.Add(new FileInformation { FileName = "File" + i.ToString() + ".txt" });
            }
            return lstScriptInfo;
        }

        private DataTable GetCountryList()
        {
            DataTable CountryDt = new DataTable();
            CountryDt.Columns.Add("id");
            CountryDt.Columns.Add("Name");
            CountryDt.Rows.Add("1", "Canada");
            CountryDt.Rows.Add("2", "USA");
            return CountryDt;
        }

    }

    public class FileInformation
    {
        public string FileName { get; set; }
        public string Download { get { return "Download File"; } }
    }
}

输出是

在此处输入图像描述

请帮我确定出了什么问题?

4

1 回答 1

1

实现这一目标的两种方法:

  1. 为列设置默认空值(跳过空检查):

    var cbColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns["Test"];
    var ds = GetCountryList();
    
    cbColumn.DataSource = ds;
    cbColumn.ValueMember = "id";
    cbColumn.DisplayMember = "Name";
    
    cbColumn.DefaultCellStyle.NullValue = ds.Rows[0][0];
    cbColumn.DefaultCellStyle.DataSourceNullValue  = ds.Rows[0][1];
    
  2. 其次是迭代后DataBinding手动设置:

    private void DataGridDataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            var hasValue = row.Cells["Test"].Value != null;
    
            if (!hasValue)
            {
                 row.Cells["Test"].Value = 1;
            }
        }
    }
    

我也会更改绑定顺序:

private void DisplayResult()
{
    var cbColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns["Test"];
    var ds = GetCountryList();

    cbColumn.DataSource = ds;
    cbColumn.ValueMember = "id";
    cbColumn.DisplayMember = "Name";

    cbColumn.DefaultCellStyle.NullValue = ds.Rows[0][0];
    cbColumn.DefaultCellStyle.DataSourceNullValue  = ds.Rows[0][1];

    FileInformationList = LoadItems();
    dataGridView1.DataSource = FileInformationList;
}
于 2013-06-21T08:36:12.620 回答