Python:输入到datagridview的格式正确

时间:2018-05-23 11:27:47

标签: python datagridview

我想将行添加到datagridview"手动"。我尝试将以下代码转换为python:https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-manipulate-rows-in-the-windows-forms-datagridview-control

但是,我努力添加行。以下不起作用:

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

以下代码确实有效,但动态不够,因为我不知道会有多少元素:

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

我该如何解决这个问题?我尝试了元组,但结果是一个元组,所有信息显示在第一个单元格中,而不是遍布列。

我不想添加软件包,因为这是在几个用户的revid dynamo中运行的,我无法说服每个人安装软件包。

上下文的完整代码:

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())

1 个答案:

答案 0 :(得分:0)

想出来。不得不使用System.Array。

from System import Array

代码更改:

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)