locked
Change DefaultCellStyle when using a DataTable in DataGridView RRS feed

  • Question

  • Here is the context: 

    I was previously using:

    $datagridviewInfo.Rows.Add("123", "456")

    To populate a DataGridView, but I noticed afterwards I couldn't export the DataGridView to CSV if it didn't use a "DataTable" as it's DataSource.

    So now i'm creating a DataTable and adding my rows to this object. I can now export sucessfully to CSV.

    HOWEVER, I can't manage to style my cells like I was able to before.

    If I do this to test :

    $button1_Click={
     $RowNomPoste = $datagridviewInfo.Rows.Add("Poste", "$poste est inaccessible.")
     $datagridviewInfo.Rows.Item($RowNomPoste).DefaultCellStyle.BackColor = "Red"
    }

    It works and gives me that row highlighted in red. However that method isn't using a DataTable so it's no good since later on I can't export to CSV.

    Now if I try this : 

    $button2_Click={

     $tableInfoPoste = New-Object system.Data.DataTable "TableInfoPoste"

     $tableInfoPoste.Columns.Add("Propriété")
     $tableInfoPoste.Columns.Add("Valeur")

     $datagridviewInfo.DataSource = $tableInfoPoste

     $RowNomPoste = $tableInfoPoste.Rows.Add("Poste", "$poste est inaccessible.")

     $tableInfoPoste.Rows.Item($RowNomPoste).DefaultCellStyle.BackColor = "Red"

    }

    It adds the row but won't allow me to change it's DefaultCellStyle.

    It throws the error : Cannont convert argument «index» («System.Data.DataRow»)  «get_Item» to type «System.Int32»: «
    ERROR: Cannot convert the value «System.Data.DataRow» of type «System.Data.DataRow» to type «System.Int32».»»

    Why is it working when using the first method of adding rows directly to DataGridView but NOT when using a DataTable?

    How can I use a DataTable but still style my row properly?

    Much appreciated.

           

    Monday, November 19, 2018 3:36 PM

Answers

  • You are trying to change the color of the table and not the grid row.  Tables have no color.  You need to get the added rows.  Color the row in the paint and do it by conditionals that tell you why you are coloring that particular row.

    Here is how to do this:

    $datagridview1_RowPrePaint=[System.Windows.Forms.DataGridViewRowPrePaintEventHandler]{
    	#Event Argument: $_ = [System.Windows.Forms.DataGridViewRowPrePaintEventArgs]
            if(([int]$datagridview1.Rows[$_.RowIndex].Cells[2].Value) -gt 10){
    		    $datagridview1.Rows[$_.RowIndex].DefaultCellStyle.ForeColor = 'red'
            }
    }
    	


    \_(ツ)_/




    • Edited by jrv Monday, November 19, 2018 5:27 PM
    • Marked as answer by Raphael GJ Monday, November 19, 2018 6:14 PM
    Monday, November 19, 2018 5:24 PM

All replies

  • You have to get the associated datagrid row.  The best way to color rows is in the "paint" event as it gives you each cell one at a time.  You can also change the row in the row added event.  You cannot change the datatable row as it is not part of the grid.

    Also you can access the row by its index:

    $datagridviewInfo.Rows[$index].DefaultCellStyle.BackColor = "Red"


    \_(ツ)_/


    • Edited by jrv Monday, November 19, 2018 4:52 PM
    Monday, November 19, 2018 4:50 PM
  • Hmm, this is giving me a headache. I would like it to be simple like the first method. It was working perfectly then, my problem was only getting all that out to CSV.

    I'm thinking of doing the dirty method of clipboard. Do you know why when I do for example : 

     $csv1 = $datagridviewInfo.SelectAll()
     $csv2 = $datagridviewInfo.GetClipboardContent()
     Set-Clipboard $csv2

    The clipboard just contains the string : System.Windows.Forms.DataObject

    Instead of the actual DataGridView data? 

    If i could manage to copy the datagridview to clipboard, all my problems would go away :) Thanks a lot

    Monday, November 19, 2018 5:15 PM
  • You are trying to change the color of the table and not the grid row.  Tables have no color.  You need to get the added rows.  Color the row in the paint and do it by conditionals that tell you why you are coloring that particular row.

    Here is how to do this:

    $datagridview1_RowPrePaint=[System.Windows.Forms.DataGridViewRowPrePaintEventHandler]{
    	#Event Argument: $_ = [System.Windows.Forms.DataGridViewRowPrePaintEventArgs]
            if(([int]$datagridview1.Rows[$_.RowIndex].Cells[2].Value) -gt 10){
    		    $datagridview1.Rows[$_.RowIndex].DefaultCellStyle.ForeColor = 'red'
            }
    }
    	


    \_(ツ)_/




    • Edited by jrv Monday, November 19, 2018 5:27 PM
    • Marked as answer by Raphael GJ Monday, November 19, 2018 6:14 PM
    Monday, November 19, 2018 5:24 PM
  • Hmm, this is giving me a headache. I would like it to be simple like the first method. It was working perfectly then, my problem was only getting all that out to CSV.

    I'm thinking of doing the dirty method of clipboard. Do you know why when I do for example : 

     $csv1 = $datagridviewInfo.SelectAll()
     $csv2 = $datagridviewInfo.GetClipboardContent()
     Set-Clipboard $csv2

    The clipboard just contains the string : System.Windows.Forms.DataObject

    Instead of the actual DataGridView data? 

    If i could manage to copy the datagridview to clipboard, all my problems would go away :) Thanks a lot


    You cannot copy an object collection to the clipboard. 

    \_(ツ)_/

    Monday, November 19, 2018 5:27 PM
  • To copy the contents of a DGV you would do it like this:

    $buttonCopyToClipboard_Click={
    	$datagridview1.ClipboardCopyMode = 'EnableAlwaysIncludeHeaderText'
        $datagridview1.SelectAll()
        $obj = $datagridview1.GetClipboardContent()
        [System.Windows.Forms.Clipboard]::SetDataObject($obj)
    }
    


    \_(ツ)_/

    Monday, November 19, 2018 5:40 PM
  • Yes, finally got it working! Thank you so much. This is all very confusing at first.

    Got it working with : 


    $datagridviewInfo_RowPrePaint=[System.Windows.Forms.DataGridViewRowPrePaintEventHandler]{
     if (($datagridviewInfo.Rows[$_.RowIndex].Cells[1].Value) -like "*inaccessible*")
     {
      $datagridviewInfo.Rows[$_.RowIndex].DefaultCellStyle.BackColor = 'red'
     }
     
     
    }

    Monday, November 19, 2018 6:15 PM