1

I keep getting 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; }
    }

3 Answers 3

11

Try

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

However, more likely, you mean

DisplayIndex = columns[i].DisplayIndex
Sign up to request clarification or add additional context in comments.

Comments

1

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++;
        }

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.