开发者

Help with converting bool to integer

I keep get开发者_如何学运维ting an error saying "Cannot implicitly covert type bool to int" I cannot figure out what to do with this. I get the error on: DisplayIndex = columns[i].Visible,

Code:

    private void SaveColumnOrder()
    {
        if (dgPortStatus.CanUserReorderColumns == true)
        {
            List<ColumnOrderItem> columnOrder = new List<ColumnOrderItem>();
            DataGridViewColumnCollection columns = this.Columns;
            for (int i = 0; i < columns.Count; i++)
            {
                columnOrder.Add(new ColumnOrderItem
                {
                    ColumnIndex = i,
                    DisplayIndex = columns[i].Visible,
                    Width = columns[i].Width
                });

            }

            portalDataGridViewSetting.Default.ColumnOrder[this.Name] = columnOrder;
            portalDataGridViewSetting.Default.Save();
        }
    }

Code:

 public sealed class ColumnOrderItem
    {
        public int DisplayIndex { get; set; }
        public int Width { get; set; }
        public bool Visible  { get; set; }
        public int ColumnIndex { get; set; }
    }


Try

DisplayIndex = columns[i].Visible ? 1 : 0

However, more likely, you mean

DisplayIndex = columns[i].DisplayIndex


The only possible interpretation I can make of this is:

        int displayIndex = 0;
        for (int i = 0; i < columns.Count; i++)
        {
            columnOrder.Add(new ColumnOrderItem
            {
                ColumnIndex = i,
                DisplayIndex = displayIndex;
                Width = columns[i].Width
            });

            if (columns[i].Visible) displayIndex++;
        }


It's telling you what's wrong. Visible is a bool and you're trying to assign it to DisplayIndex which is an int.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜