Bit Operations in Enum

I am having some problems with the following:

  • I want to get the first visible and frozen column of a collection of columns.

I think this will do:

DataGridViewColumnCollection dgv = myDataGridView.Columns;
dgv.GetFirstColumn(
     DataGridViewElementStates.Visible | DataGridViewElementStates.Frozen);
  • Is it possible to create a bitmask to get the first frozen OR visible column?
+3
source share
2 answers

The implementation, AFAIK, “all this” - it uses:

((this.State & elementState) == elementState);

What is "everything." If you want to write "any", perhaps add a helper method: (add "this" to DataGridViewColumnCollectionto make it an extended C # 3.0 method)

    public static DataGridViewColumn GetFirstColumnWithAny(
        DataGridViewColumnCollection columns, // optional "this"
        DataGridViewElementStates states)
    {
        foreach (DataGridViewColumn column in columns)
        {
            if ((column.State & states) != 0) return column;
        }
        return null;
    }

Or with LINQ:

        return columns.Cast<DataGridViewColumn>()
            .FirstOrDefault(col => (col.State & states) != 0);
+5
source

Well, bitmasks usually work like this:

| . & - , . ^ ( , C/++).

GetFirstColumn, - (, GetFirstColumn , , ).

+1

Source: https://habr.com/ru/post/1705389/


All Articles