IronPython DataGridView:从一键获取价值按但没有按'Enter'并移动到下一个单元格

时间:2015-10-28 13:19:22

标签: datagridview ironpython

我几天来一直在努力解决这个困难。我已经阅读了http://www.ironpython.info/并预订了Michael和Christian的'IronPython In Action',以及https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview(v=vs.110).aspx上的DataGridView类,但我仍然缺乏一些知识和技能。 我的问题是如何从一键按下获得价值,但没有按下'Enter'并移动到下一个单元格,这意味着当我在键盘上按一个数字但没有按下'Enter'时,数字将被传递到第一个单元格,如3,然后下一个单元格是活动的。序列是曲折的,如第1行第1列 - >第1行第2行 - > ... - >第1行第12行 - >第2行第12行 - >第2行第11行。 我尝试过很多DataGridView事件,比如'CellEnter'和'KeyPress',但都失败了。 如果你能给出一些提示,我会非常感激。 以下是我的代码:

import clr

clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

from System.Windows.Forms import Form
from System.Windows.Forms import DataGridView
from System.Windows.Forms import DataGridViewContentAlignment
from System.Windows.Forms import Application
from System.Windows.Forms import Control
from System.Windows.Forms import Clipboard
from System.Windows.Forms import DataFormats
from System.Windows.Forms import DataObject

from System.Drawing import Point
from System.Drawing import Size
from System.Drawing import Font
from System.Drawing import FontStyle
from System.Drawing import Color

from System import Text

from System.IO import MemoryStream

NUMROWS = 6
NUMCOLS = 12

data = [[-1 for x in range(NUMCOLS)] for x in range(NUMROWS)]


class DataGridForm(Form):
    def __init__(self, numcols, numrows):
        self.Text = 'DataGridView Cell Format Test'
        self.ClientSize = Size(98 * (numcols + 1), 25 * (numrows + 1))
        self.dgv = DataGridView()
        self.numcols = numcols
        self.numrows = numrows
        self.setupdatagridview()
        self.adddata()
        self.formatheaders()

    def setupdatagridview(self):
        self.dgv.Location = Point(0, 0)
        self.dgv.Size = Size(98 * (NUMCOLS + 1), 25 * (NUMROWS + 1))
        self.Controls.Add(self.dgv)
        # have to have columns defined before inserting rows
        self.dgv.ColumnCount = self.numcols
        # center all text in all data cells by default
        self.dgv.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        #self.dgv.CellEnter += self.move
        #self.dgv.CellValueChanged +=self.move
        #self.dgv.Enter += self.move
        #self.dgv.CellStateChanged += self.move
        #self.dgv.ChangeUICues += self.move
        #self.dgv.EditingControlShowing += self.move
        #self.dgv.EditModeChanged += self.move
        #self.dgv.GotFocus += self.move
        #self.dgv.HandleCreated += self.move
        #self.dgv.KeyPress += self.move
        #self.dgv.KeyDown += self.move
        #self.dgv.KeyUp += self.move
        #self.dgv.MouseEnter += self.move
        #self.dgv.Move += self.move
        #self.dgv.TextChanged += self.move

        # add empty rows first
        for num in xrange(self.numrows):
            self.dgv.Rows.Add()
        # format empty cells
        self.dgv.AllowUserToAddRows = False
        self.dgv.AllowUserToDeleteRows = False
        self.dgv.ReadOnly = False
        self.dgv.ClearSelection()

    def formatheaders(self):
        for num in xrange(self.numcols):
            col = self.dgv.Columns[num]
            col.HeaderCell.Value = str(num + 1)
            # slightly left of center on headers
            col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            # sets font and font style
            col.HeaderCell.Style.Font = Font(Control.DefaultFont, FontStyle.Bold) 
            col.HeaderCell.Style.ForeColor = Color.MidnightBlue
        # put numbers on rows
        for num in xrange(self.numrows):
            row = self.dgv.Rows[num]
            # get sequential numeric label on side of row
            row.HeaderCell.Value = str(num + 1)
            # sets font and font style
            row.HeaderCell.Style.Font = Font(Control.DefaultFont, FontStyle.Bold)
            row.HeaderCell.Style.ForeColor = Color.Blue
        self.dgv.TopLeftHeaderCell.Value = 'Score'
        self.dgv.TopLeftHeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        self.dgv.TopLeftHeaderCell.Style.Font = Font(Control.DefaultFont, FontStyle.Bold)
        self.dgv.TopLeftHeaderCell.Style.ForeColor = Color.Blue
        self.dgv.RowHeadersWidth = 60

    def adddata(self):
        for num in xrange(self.numrows):
            row = self.dgv.Rows[num]
            dat = (datax for datax in data[num]) 
            for cell in row.Cells:
                cell.Value = dat.next()
    """            
    def move(self, sender, event):
        print 'move'
    """
dataGridForm = DataGridForm(NUMCOLS, NUMROWS)
Application.Run(dataGridForm)

3 个答案:

答案 0 :(得分:1)

我终于自己解决了这个问题。使用KeyPress事件并将ReadOnly属性设置为true。

答案 1 :(得分:0)

我发现EditingControlShowing唤起一个事件,而self.dgv.CurrentCell = self.dgv.Rows [currentRow] .Cells [currentCol + 1]移动单元格。但是,我无法获取输入数字,表格为无。我的这部分代码如下。

android:fillViewport="true

答案 2 :(得分:0)

我找到了一种方法:首先通过“self.dgv.EditingControlShowing + = self.keyPress”唤起EditingControlShowing事件;然后唤起KeyPress def keyPress(self,sender,event):         event.Control.KeyPress + = self.move 现在我可以通过“self.dgv.CurrentCell.Value = event.KeyChar”获得价值。 但是,单元格值和移动并不总是正确的,需要解决。