Hatena::ブログ(Diary)

理想のユーザ・インターフェイスを求めて このページをアンテナに追加 RSSフィード

2014-11-19

DataGridViewでの行の追加と削除

DataGridViewを使って動的なテーブルを作成する。

行の追加は、Rows.Add()した後、Cellの値を指定する。

具体的には下のように処理する。

    def Button1Click(self, sender, e):
        time = self._textBox1.Text
        valu = self._textBox2.Text
        idx = self._dataGridView1.Rows.Add()
        self._dataGridView1.Rows[idx].Cells[0].Value = time
        self._dataGridView1.Rows[idx].Cells[1].Value = valu

選択した行(1つのみ)を削除するには、Rows.RemoveAt(0)とすれば1行目が削除されるので、選択行の情報を取り出してから、Rows.RemoveAtすればよい。

具体的には下のように処理する。

    def Button2Click(self, sender, e):
        idx = self._dataGridView1.SelectedRows[0].Index
        self._dataGridView1.Rows.RemoveAt(idx)

これを使って、以下のような画面を作った。

f:id:Megumi221:20141119065021p:image

ソースコードは以下の通り。

import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class MainForm(Form):
    def __init__(self):
        self.InitializeComponent()
   
    def InitializeComponent(self):
        self._label1 = System.Windows.Forms.Label()
        self._textBox1 = System.Windows.Forms.TextBox()
        self._label2 = System.Windows.Forms.Label()
        self._textBox2 = System.Windows.Forms.TextBox()
        self._button1 = System.Windows.Forms.Button()
        self._dataGridView1 = System.Windows.Forms.DataGridView()
        self._button2 = System.Windows.Forms.Button()
        self._Column1 = System.Windows.Forms.DataGridViewTextBoxColumn()
        self._Column2 = System.Windows.Forms.DataGridViewTextBoxColumn()
        self._button3 = System.Windows.Forms.Button()
        self._dataGridView1.BeginInit()
        self.SuspendLayout()
        #
        # label1
        #
        self._label1.Location = System.Drawing.Point(25, 35)
        self._label1.Name = "label1"
        self._label1.Size = System.Drawing.Size(53, 23)
        self._label1.TabIndex = 0
        self._label1.Text = "時刻[s]"
        #
        # textBox1
        #
        self._textBox1.Location = System.Drawing.Point(73, 35)
        self._textBox1.Name = "textBox1"
        self._textBox1.Size = System.Drawing.Size(77, 19)
        self._textBox1.TabIndex = 1
        self._textBox1.Text = "0.0"
        #
        # label2
        #
        self._label2.Location = System.Drawing.Point(171, 35)
        self._label2.Name = "label2"
        self._label2.Size = System.Drawing.Size(100, 23)
        self._label2.TabIndex = 2
        self._label2.Text = ""
        #
        # textBox2
        #
        self._textBox2.Location = System.Drawing.Point(193, 35)
        self._textBox2.Name = "textBox2"
        self._textBox2.Size = System.Drawing.Size(78, 19)
        self._textBox2.TabIndex = 3
        self._textBox2.Text = "1.0"
        #
        # button1
        #
        self._button1.Location = System.Drawing.Point(292, 33)
        self._button1.Name = "button1"
        self._button1.Size = System.Drawing.Size(75, 23)
        self._button1.TabIndex = 4
        self._button1.Text = "追加"
        self._button1.UseVisualStyleBackColor = True
        self._button1.Click += self.Button1Click
        #
        # dataGridView1
        #
        self._dataGridView1.AllowUserToAddRows = False
        self._dataGridView1.AllowUserToDeleteRows = False
        self._dataGridView1.AllowUserToResizeColumns = False
        self._dataGridView1.AllowUserToResizeRows = False
        self._dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        self._dataGridView1.Columns.AddRange(System.Array[System.Windows.Forms.DataGridViewColumn](
            [self._Column1,
            self._Column2]))
        self._dataGridView1.Location = System.Drawing.Point(25, 81)
        self._dataGridView1.Name = "dataGridView1"
        self._dataGridView1.ReadOnly = True
        self._dataGridView1.RightToLeft = System.Windows.Forms.RightToLeft.No
        self._dataGridView1.RowTemplate.Height = 21
        self._dataGridView1.Size = System.Drawing.Size(246, 150)
        self._dataGridView1.TabIndex = 5
        #
        # button2
        #
        self._button2.Location = System.Drawing.Point(292, 81)
        self._button2.Name = "button2"
        self._button2.Size = System.Drawing.Size(75, 23)
        self._button2.TabIndex = 6
        self._button2.Text = "削除"
        self._button2.UseVisualStyleBackColor = True
        self._button2.Click += self.Button2Click
        #
        # Column1
        #
        self._Column1.HeaderText = "時刻"
        self._Column1.Name = "Column1"
        self._Column1.ReadOnly = True
        #
        # Column2
        #
        self._Column2.HeaderText = ""
        self._Column2.Name = "Column2"
        self._Column2.ReadOnly = True
        self._Column2.Width = 102
        #
        # button3
        #
        self._button3.Location = System.Drawing.Point(292, 111)
        self._button3.Name = "button3"
        self._button3.Size = System.Drawing.Size(75, 23)
        self._button3.TabIndex = 7
        self._button3.Text = "完了"
        self._button3.UseVisualStyleBackColor = True
        self._button3.Click += self.Button3Click
        #
        # MainForm
        #
        self.ClientSize = System.Drawing.Size(379, 253)
        self.Controls.Add(self._button3)
        self.Controls.Add(self._button2)
        self.Controls.Add(self._dataGridView1)
        self.Controls.Add(self._button1)
        self.Controls.Add(self._textBox2)
        self.Controls.Add(self._label2)
        self.Controls.Add(self._textBox1)
        self.Controls.Add(self._label1)
        self.Name = "MainForm"
        self.Text = "testTable"
        self._dataGridView1.EndInit()
        self.ResumeLayout(False)
        self.PerformLayout()

    def Button1Click(self, sender, e):
        time = self._textBox1.Text
        valu = self._textBox2.Text
        idx = self._dataGridView1.Rows.Add()
        self._dataGridView1.Rows[idx].Cells[0].Value = time
        self._dataGridView1.Rows[idx].Cells[1].Value = valu

    def Button2Click(self, sender, e):
        idx = self._dataGridView1.SelectedRows[0].Index
        self._dataGridView1.Rows.RemoveAt(idx)

    def Button3Click(self, sender, e):
        print self._dataGridView1.Rows.Count
       

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証

トラックバック - http://d.hatena.ne.jp/Megumi221/20141119/1416347501
リンク元