「受注画面」のコード
Public Class Form1 'デザインで作成した明細にアクセスするためのコレクションアダプタです Private DetailCollection As G1FreeGrid1DetailAdapterCollection1 Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click DetailCollection = New G1FreeGrid1DetailAdapterCollection1(G1FreeGrid1) Dim control As Control control = sender Dim CurrentRowNo As Integer CurrentRowNo = G1FreeGrid1.GetPositionFromControl(control) CzsampleDataSet.prDetail.商品コードColumn.AllowDBNull = True CzsampleDataSet.prDetail.商品名Column.AllowDBNull = True CzsampleDataSet.prDetail.数量Column.AllowDBNull = True CzsampleDataSet.prDetail.定価Column.AllowDBNull = True CzsampleDataSet.prDetail.単価Column.AllowDBNull = True CzsampleDataSet.prDetail.小計Column.AllowDBNull = True CzsampleDataSet.prDetail.備考Column.AllowDBNull = True CzsampleDataSet.prDetail.受注コードColumn.AllowDBNull = True Dim f3 As Form3 f3 = New Form3() f3.mode_obj_id = DetailCollection(CurrentRowNo).TextBox1 f3.mode_obj_name = DetailCollection(CurrentRowNo).TextBox2 f3.ShowDialog() DetailCollection(CurrentRowNo).TextBox1.Refresh() DetailCollection(CurrentRowNo).TextBox2.Refresh() End Sub Private Sub txtJuchuNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtJuchuNo.TextChanged Dim juchuNo As Integer juchuNo = 0 If Not txtJuchuNo.Text = "" Then juchuNo = Integer.Parse(txtJuchuNo.Text) End If PrHeaderTableAdapter.Fill(CzsampleDataSet.prHeader, juchuNo) PrDetailTableAdapter.Fill(CzsampleDataSet.prDetail, juchuNo) '小計の合計を表示する Dim shokei As Integer shokei = 0 Dim i As Integer If 0 < CzsampleDataSet.prDetail.Rows.Count Then For i = 0 To CzsampleDataSet.prDetail.Rows.Count - 1 shokei += CzsampleDataSet.prDetail.Rows(i)("小計") Next End If labSumShokei.Text = shokei.ToString() '新規の場合は空白行を追加する If juchuNo = 0 Then CzsampleDataSet.prDetail.商品コードColumn.AllowDBNull = True CzsampleDataSet.prDetail.商品名Column.AllowDBNull = True CzsampleDataSet.prDetail.数量Column.AllowDBNull = True CzsampleDataSet.prDetail.定価Column.AllowDBNull = True CzsampleDataSet.prDetail.単価Column.AllowDBNull = True CzsampleDataSet.prDetail.小計Column.AllowDBNull = True CzsampleDataSet.prDetail.備考Column.AllowDBNull = True CzsampleDataSet.prDetail.受注コードColumn.AllowDBNull = True CzsampleDataSet.prDetail.AddprDetailRow(CzsampleDataSet.prDetail.NewprDetailRow()) End If End Sub Private Sub button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button5.Click 'Insertストアドを実行する Dim detailIndex As Integer detailIndex = CzsampleDataSet.prDetail.Count - 1 Dim newdetail As czsampleDataSet.prDetailRow newdetail = CzsampleDataSet.prDetail(detailIndex) Dim dat1 As Integer Dim dat2 As String Dim dat3 As Integer Dim dat4 As Integer Dim dat5 As Integer Dim dat6 As Integer Dim dat7 As String Dim dat8 As Integer dat1 = newdetail.商品コード dat2 = newdetail.商品名 dat3 = newdetail.数量 dat4 = newdetail.定価 dat5 = newdetail.単価 dat6 = newdetail.小計 dat7 = newdetail.備考 dat8 = Integer.Parse(txtJuchuNo.Text) PrDetailTableAdapter.Insert(dat1, dat2, dat3, dat4, dat5, dat6, dat7, dat8) 'Return Valueを取得する Dim rev As Integer rev = PrDetailTableAdapter.Adapter.InsertCommand.Parameters(0).Value If rev = 0 Then MessageBox.Show("登録されました") Else MessageBox.Show("正しく入力されていない箇所があります。") End If End Sub Private Sub txtNohin_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNohin.TextChanged Dim tokuisakiNo As Integer If Not txtNohin.Text = "" Then tokuisakiNo = Integer.Parse(txtNohin.Text) End If Pr得意先取得TableAdapter.Fill(CzsampleDataSet.pr得意先取得, tokuisakiNo) End Sub Private Sub txtSeikyu_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSeikyu.TextChanged Dim tokuisakiNo As Integer If Not txtSeikyu.Text = "" Then tokuisakiNo = Integer.Parse(txtSeikyu.Text) End If Pr得意先取得2TableAdapter.Fill(CzsampleDataSet.pr得意先取得2, tokuisakiNo) End Sub Private Sub txtShukka_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtShukka.TextChanged Dim tokuisakiNo As Integer If Not txtShukka.Text = "" Then tokuisakiNo = Integer.Parse(txtShukka.Text) End If Pr得意先取得3TableAdapter.Fill(CzsampleDataSet.pr得意先取得3, tokuisakiNo) End Sub Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click Dim f2 As Form2 f2 = New Form2() f2.mode_obj = txtNohin f2.Show() End Sub Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click Dim f2 As Form2 f2 = New Form2() f2.mode_obj = txtSeikyu f2.Show() End Sub Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click Dim f2 As Form2 f2 = New Form2() f2.mode_obj = txtShukka f2.Show() End Sub End Class
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace gridcz2_create { public partial class Form1 : Form { // デザインで作成した明細にアクセスするためのコレクションアダプタです private G1FreeGrid1DetailAdapterCollection1 DetailCollection; public Form1() { InitializeComponent(); } private void txtJuchuNo_TextChanged(object sender, EventArgs e) { int juchuNo = 0; if (txtJuchuNo.Text != "") { juchuNo = int.Parse(txtJuchuNo.Text); } prHeaderTableAdapter.Fill(czsampleDataSet.prHeader, juchuNo); prDetailTableAdapter.Fill(czsampleDataSet.prDetail, juchuNo); // 小計の合計を表示する int shokei = 0; for (int i = 0; i < czsampleDataSet.prDetail.Rows.Count; i++) { shokei += (int)czsampleDataSet.prDetail.Rows[i]["小計"]; } labSumShokei.Text = shokei.ToString(); // 新規の場合は空白行を追加する if (juchuNo == 0) { czsampleDataSet.prDetail.商品コードColumn.AllowDBNull = true; czsampleDataSet.prDetail.商品名Column.AllowDBNull = true; czsampleDataSet.prDetail.数量Column.AllowDBNull = true; czsampleDataSet.prDetail.定価Column.AllowDBNull = true; czsampleDataSet.prDetail.単価Column.AllowDBNull = true; czsampleDataSet.prDetail.小計Column.AllowDBNull = true; czsampleDataSet.prDetail.備考Column.AllowDBNull = true; czsampleDataSet.prDetail.受注コードColumn.AllowDBNull = true; czsampleDataSet.prDetail.AddprDetailRow(czsampleDataSet.prDetail.NewprDetailRow()); } } private void button5_Click(object sender, EventArgs e) { // Insertストアドを実行する int detailIndex = czsampleDataSet.prDetail.Count - 1; czsampleDataSet.prDetailRow newdetail = czsampleDataSet.prDetail[detailIndex]; int dat1 = newdetail.商品コード; string dat2 = newdetail.商品名; int dat3 = newdetail.数量; int dat4 = newdetail.定価; int dat5 = newdetail.単価; int dat6 = newdetail.小計; string dat7 = newdetail.備考; int dat8 = int.Parse(txtJuchuNo.Text); prDetailTableAdapter.Insert(dat1, dat2, dat3, dat4, dat5, dat6, dat7, dat8); // Return Valueを取得する //int rev = (int)prDetailTableAdapter.Adapter.InsertCommand.Parameters[0].Value; //★保護エラーが発生する場合は以下の処理を使用してください。 int rev = (int)prDetailTableAdapter.ReturnValue; if (rev == 0) { MessageBox.Show("登録されました。"); } else { MessageBox.Show("正しく入力されていない箇所があります。"); } } private void button1_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.mode_obj = txtNohin; f2.Show(); } private void button2_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.mode_obj = txtSeikyu; f2.Show(); } private void button3_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.mode_obj = txtShukka; f2.Show(); } private void txtNohin_TextChanged(object sender, EventArgs e) { int tokuisakiNo = 0; if (txtNohin.Text != "") { tokuisakiNo = int.Parse(txtNohin.Text); } pr得意先取得TableAdapter.Fill(czsampleDataSet.pr得意先取得, tokuisakiNo); } private void txtSeikyu_TextChanged(object sender, EventArgs e) { int tokuisakiNo = 0; if (txtSeikyu.Text != "") { tokuisakiNo = int.Parse(txtSeikyu.Text); } pr得意先取得2TableAdapter.Fill(czsampleDataSet.pr得意先取得2, tokuisakiNo); } private void txtShukka_TextChanged(object sender, EventArgs e) { int tokuisakiNo = 0; if (txtShukka.Text != "") { tokuisakiNo = int.Parse(txtShukka.Text); } pr得意先取得3TableAdapter.Fill(czsampleDataSet.pr得意先取得3, tokuisakiNo); } private void button7_Click(object sender, EventArgs e) { DetailCollection = new G1FreeGrid1DetailAdapterCollection1(g1FreeGrid1); Control control = sender as Control; int CurrentRowNo = g1FreeGrid1.GetPositionFromControl(control); czsampleDataSet.prDetail.商品コードColumn.AllowDBNull = true; czsampleDataSet.prDetail.商品名Column.AllowDBNull = true; czsampleDataSet.prDetail.数量Column.AllowDBNull = true; czsampleDataSet.prDetail.定価Column.AllowDBNull = true; czsampleDataSet.prDetail.単価Column.AllowDBNull = true; czsampleDataSet.prDetail.小計Column.AllowDBNull = true; czsampleDataSet.prDetail.備考Column.AllowDBNull = true; czsampleDataSet.prDetail.受注コードColumn.AllowDBNull = true; Form3 f3 = new Form3(); f3.mode_obj_id = DetailCollection[CurrentRowNo].TextBox1; f3.mode_obj_name = DetailCollection[CurrentRowNo].TextBox2; f3.ShowDialog(); DetailCollection[CurrentRowNo].TextBox1.Refresh(); DetailCollection[CurrentRowNo].TextBox2.Refresh(); } } }
「商品検索画面」のコード
Public Class Form3 Public mode_obj_id As TextBox Public mode_obj_name As TextBox Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click Me.Close() End Sub Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click Dim pos As Integer pos = G1FreeGrid1.GetSelectedDetailPositions(0) mode_obj_id.Text = CzsampleDataSet2.pr商品検索(pos).商品コード.ToString() mode_obj_name.Text = CzsampleDataSet2.pr商品検索(pos).商品名.ToString() Me.Close() End Sub Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Dim opt As Integer If チェックボックス.Checked Then opt = 1 End If Pr商品検索TableAdapter.Fill(CzsampleDataSet2.pr商品検索, テキストボックス.Text, opt) End Sub End Class
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace gridcz2_create { public partial class Form3 : Form { public Form3() { InitializeComponent(); } public TextBox mode_obj_id = null; public TextBox mode_obj_name = null; private void button2_Click(object sender, EventArgs e) { this.Close(); } private void btnSearch_Click(object sender, EventArgs e) { int opt = 0; if (チェックボックス.Checked) { opt = 1; } pr商品検索TableAdapter.Fill(czsampleDataSet2.pr商品検索, テキストボックス.Text, opt); } private void button1_Click(object sender, EventArgs e) { int pos = g1FreeGrid1.GetSelectedDetailPositions()[0]; mode_obj_id.Text = czsampleDataSet2.pr商品検索[pos].商品コード.ToString(); mode_obj_name.Text = czsampleDataSet2.pr商品検索[pos].商品名.ToString(); this.Close(); } } }
まとめ
今回は受注画面の作成を行いました。フリーレイアウトグリッドを使用すれば、これまでの応用により、どんな画面でも簡単に作成できることがお分かりいただけたかと思います。次回は、検索画面で仕様変更が発生したときの対処方法について説明します。