Python: correct format on input to datagridview

Hi,
I want to add rows to a datagridview “manually”. I tried converting the following code to python with reference to Manipulate Rows in the Windows Forms DataGridView Control and How to Build the User Interface.

However, I struggle with adding rows. The following doesn’t work:

for j in range(len(signals)):
    self._dataGridView1.Rows.Add(signals[j])

The following code does work, but is not dynamically enough as I don’t know how many elements there will be:

for j in range(len(signals)):
    self._dataGridView1.Rows.Add(signals[j][0], signals[j][1], signals[j][2], signals[j][3])
How should I fix this? I tried tuple, but the result were a tuple with all the info shown in the first cell instead of spread over the columns.

I would not like to add packages, as this is to be run within revid dynamo among several users, and I cannot convince everyone to install packages.

full code for context:

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
clr.AddReference('System.Data')
clr.AddReference('RevitAPIUI')


from Autodesk.Revit.UI import TaskDialog

from System.Windows.Forms import *

from System.Drawing import (
    Point, Size,
    Font, FontStyle,
    GraphicsUnit
)

from System.Data import DataSet
from System.Data.Odbc import OdbcConnection, OdbcDataAdapter

msgBox = TaskDialog

headers = IN[0]
signals = IN[1]



class DataGridViewQueryForm(Form):

        def __init__(self):
            self.Text = 'Signals'
            self.ClientSize = Size(942, 255)
            self.MinimumSize = Size(500, 200)

            self.setupDataGridView()


        def setupDataGridView(self):            
            self._dataGridView1 = DataGridView()
            self._dataGridView1.AllowUserToOrderColumns = True
            self._dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
            self._dataGridView1.Dock = DockStyle.Fill
            self._dataGridView1.Location = Point(0, 111)
            self._dataGridView1.Size = Size(506, 273)
            self._dataGridView1.TabIndex = 3
            self._dataGridView1.ColumnCount = len(headers)
            self._dataGridView1.ColumnHeadersVisible = True
            for i in range(len(headers)):
                self._dataGridView1.Columns[i].Name = headers[i]

            for j in range(len(signals)):
                self._dataGridView1.Rows.Add(signals[j][0], signals[j][1], signals[j][2], signals[j][3])

            self.Controls.Add(self._dataGridView1)

Application.Run(DataGridViewQueryForm())

@TejaswiniUL Can you show you graph as well?

Yeah, Figured it out.

Had to use System.Array.

from System import Array
code changes:

array_str = Array.CreateInstance(str, len(headers))

    for j in range(len(signals)):

        for k in range(len(headers)):
            array_str[k] = signals[j][k]
        self._dataGridView1.Rows.Add(array_str)
1 Like

just a hint, using range is not pytonic. Python has some very good functions to traverse lists.

ex. for item in headers: will do it. If you need an index, then try for idx, item in enumerate(headers): in your case where you have a double forloop and dont need the item, then try this.

for idx_col, _ in enumerate(signals):
    for idx_row, _ in enumerate(headers):
        array_str[idx_row] = signals[idxCol][idx_row]
    self._dataGridView1.Rows.Add(array_str)

It could be even more efficient, but let that be for now :slight_smile:

Sure, Thank you @erfajo