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], signals[j], signals[j], signals[j]) 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 signals = IN 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], signals[j], signals[j], signals[j]) self.Controls.Add(self._dataGridView1) Application.Run(DataGridViewQueryForm())