Iterate through a DataGrid (not a DataGridView)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peted

    #1

    Iterate through a DataGrid (not a DataGridView)

    Hi

    I need to iterate through a winforms visible datagrid, in a legacy
    application, to update it a little bit to change the background
    colours of the grid.

    This seems to be more dificult than i expected as it does not appear
    the DataGridRows[] collection is exposed with DataGrid.

    Is there a way to/ or best way to iterate through the rows of a
    datagrid to change the background colour.

    Note that i cannot update the winforms app to use a DataGridView


    Any advice or examples appreciated

    Peted
  • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

    #2
    RE: Iterate through a DataGrid (not a DataGridView)


    "Peted" wrote:
    Hi
    >
    I need to iterate through a winforms visible datagrid, in a legacy
    application, to update it a little bit to change the background
    colours of the grid.
    >
    This seems to be more dificult than i expected as it does not appear
    the DataGridRows[] collection is exposed with DataGrid.
    >
    Is there a way to/ or best way to iterate through the rows of a
    datagrid to change the background colour.
    >
    Note that i cannot update the winforms app to use a DataGridView
    >
    >
    Any advice or examples appreciated
    >
    Peted
    >
    Hi Peter,

    I'm afraid you can't change the background color if an individual row, even
    if you can extract the DataGridRows collection using reflection. The
    DataGridRow does not contain any color properties. If you want a different
    background color on the DataGridRows you need to use the color properties on
    the DataGrid. BackColor, AlternatingBack Color, SelectionBackCo lor etc.

    --
    Happy Coding!
    Morten Wennevik [C# MVP]

    Comment

    • Peted

      #3
      Re: Iterate through a DataGrid (not a DataGridView)



      Hi thanks for that.
      Suppose however i do want to just iterate through a DataGrid , lets
      say through the rows using a "foreach" statement. Is this possible
      with a datagrid, seeing as it does not expose a datagridrows
      collection.

      Is there anyway to do this at all ?

      thanks

      Peter
      >
      >Hi Peter,
      >
      >I'm afraid you can't change the background color if an individual row, even
      >if you can extract the DataGridRows collection using reflection. The
      >DataGridRow does not contain any color properties. If you want a different
      >background color on the DataGridRows you need to use the color properties on
      >the DataGrid. BackColor, AlternatingBack Color, SelectionBackCo lor etc.

      Comment

      • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

        #4
        Re: Iterate through a DataGrid (not a DataGridView)


        "Peted" wrote:
        >
        >
        Hi thanks for that.
        Suppose however i do want to just iterate through a DataGrid , lets
        say through the rows using a "foreach" statement. Is this possible
        with a datagrid, seeing as it does not expose a datagridrows
        collection.
        >
        Is there anyway to do this at all ?
        >
        thanks
        >
        Peter
        >
        Well, using reflection you can get to the DataGridRow using the following:


        Type gridType = Type.GetType(@"
        System.Windows. Forms.DataGrid,
        System.Windows. Forms,
        Version=2.0.0.0 ,
        Culture=neutral ,
        PublicKeyToken= b77a5c561934e08 9");

        PropertyInfo piRows = gridType.GetPro perty("DataGrid Rows",
        BindingFlags.In stance | BindingFlags.No nPublic);

        Type rowType = piRows.Property Type.GetElement Type();
        PropertyInfo piValue = rowType.GetProp erty("RowNumber ");

        object[] rows = piRows.GetValue (myDataGrid, null) as object[];
        foreach (object row in rows)
        {
        int rowNumber = (int)piValue.Ge tValue(row, null);
        }

        However, there are no Cells property or Value so the DataGridRow is only
        meant for display and only contains the row index to the actual row, which
        would explain why DataGridRows is null before display time, even though
        DataSource is set.

        You can instead use the indexer on the DataGrid itself to obtain the values

        int numRows = dg.BindingConte xt[myDataGrid.Data Source].Count;
        object[] data = new object[numRows];
        for (int i = 0; i < numRows; i++)
        {
        data[i] = myDataGrid[i, 0];
        }

        However, the number of columns is a bit trickier as this is the number of
        displayed columns, not the number of potential columns from the DataSource.

        A third method is parsing through the DataSource itself, which according to
        the documentations can be a DataTable, DataView, DataSet, DataViewManager ,
        IListSource or IList.

        Once you determine which type the DataGrid.DataSo urce contains you can use
        properties and methods on each of the six types to parse the content.


        if (dg.DataSource is DataTable)
        ParseTable(dg.D ataSource as DataTable);
        else if (dg.DataSource is IList)
        ParseList(dg.Da taSource as IList);
        ....
        etc


        --
        Happy Coding!
        Morten Wennevik [C# MVP]

        Comment

        • Peted

          #5
          Re: Iterate through a DataGrid (not a DataGridView)


          thanks for that dude
          i will see how it goes

          cheers

          Peter
          >>
          >
          >Well, using reflection you can get to the DataGridRow using the following:
          >
          >
          >Type gridType = Type.GetType(@"
          System.Windows. Forms.DataGrid,
          System.Windows. Forms,
          Version=2.0.0.0 ,
          Culture=neutral ,
          PublicKeyToken= b77a5c561934e08 9");
          >
          >PropertyInfo piRows = gridType.GetPro perty("DataGrid Rows",
          BindingFlags.In stance | BindingFlags.No nPublic);
          >
          >Type rowType = piRows.Property Type.GetElement Type();
          >PropertyInfo piValue = rowType.GetProp erty("RowNumber ");
          >
          >object[] rows = piRows.GetValue (myDataGrid, null) as object[];
          >foreach (object row in rows)
          >{
          int rowNumber = (int)piValue.Ge tValue(row, null);
          >}
          >
          >However, there are no Cells property or Value so the DataGridRow is only
          >meant for display and only contains the row index to the actual row, which
          >would explain why DataGridRows is null before display time, even though
          >DataSource is set.
          >
          >You can instead use the indexer on the DataGrid itself to obtain the values
          >
          >int numRows = dg.BindingConte xt[myDataGrid.Data Source].Count;
          >object[] data = new object[numRows];
          >for (int i = 0; i < numRows; i++)
          >{
          data[i] = myDataGrid[i, 0];
          >}
          >
          >However, the number of columns is a bit trickier as this is the number of
          >displayed columns, not the number of potential columns from the DataSource.
          >
          >A third method is parsing through the DataSource itself, which according to
          >the documentations can be a DataTable, DataView, DataSet, DataViewManager ,
          >IListSource or IList.
          >
          >Once you determine which type the DataGrid.DataSo urce contains you can use
          >properties and methods on each of the six types to parse the content.
          >
          >
          >if (dg.DataSource is DataTable)
          ParseTable(dg.D ataSource as DataTable);
          >else if (dg.DataSource is IList)
          ParseList(dg.Da taSource as IList);
          >...
          >etc

          Comment

          Working...