在DataGridView DataSource中显示数组中的格式化字符串

时间:2016-04-25 15:20:42

标签: c# winforms data-binding datagridview datasource

当列表绑定到DataGridView时,有没有办法从数组属性显示格式化字符串?

我目前正在使用以下代码:

var bindingList = new BindingList<Stage>(stageList);
var source = new BindingSource(bindingList, null);
dv.DataSource = source;
dv.AutoGenerateColumns = true;


internal class Stage
{
     .
     public bool isNew {get; protected internal set; }
     public int Id { get; protected internal set; }
     public short[] Level { get; protected internal set; } = new short[4];
     .
     .
}

&#34; isNew&#34;和&#34; Id&#34;属性正确显示。 我希望得到以下示例输出:

IsNew   |   Id  |   Stage
 [✓]        1       1/5/7/9
 [ ]        2       2/3/8/9
 [ ]        3       3/5/8/10

其中Stage是一个包含4个元素格式为

的数组
string.Format("{0}/{1}/{2}/{3}", Stage[0], Stage[1], Stage[2], Stage[3]);

1 个答案:

答案 0 :(得分:0)

试试这段代码:

private void dv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (this.dv.Columns[e.ColumnIndex].Name == "Stage")
    {
        formatting.Value = //your code to format data for this column;
    }
}