2013-12-10 89 views
0

我正在使用vb.net設計器連接到訪問數據庫。 在我的Form1上我有一個DataGridView和兩個按鈕添加和編輯 我使Form2將數據添加到數據庫和工作好.. Imake Form3與相同的form2內容 現在我需要當我在DataGridView和Clic編輯按鈕所選行的數據顯示在窗體3上以供編輯它打開datagridview中的選定行以編輯另一種形式

Public Class Form1 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Me.SalesTableAdapter.Fill(Me.OrdersDataSet.sales) 

    Me.DateTimePicker1.Value = Date.Today 
End Sub 

Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged 
    SalesBindingSource.Filter = String.Format("date = '{0}'", DateTimePicker1.Value.ToShortDateString()) 
End Sub 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Form2.Show() 
End Sub 

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    Form3.Show() 
End Sub 

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click 

End Sub 

Private Sub SalesDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles SalesDataGridView.CellContentClick 
End Sub 
End Class 
+0

什麼是你的問題?請定義一個明確的問題,以便可以回答。謝謝。 –

+0

我需要當我在DataGridView行選擇並單擊編輯按鈕所選行的數據顯示在form3上編輯它 – user3077945

+0

@ user3077945 ..您可以使用Dgv currentrow和單元格索引。 – matzone

回答

2

您需要以模態/對話方式訪問它。您只需要編輯一個表格。

添加/編輯形式

添加一個參數的構造函數的形式。

Public Sub New(row As DataRowView) 
    Me.InitializeComponent() 
    'Me.ctlAge: NumericUpDown control. 
    'Me.ctlBirthday: DateTimePicker control. 
    'Me.ctlName: TextBox control. 
    If (row Is Nothing) Then 
     'Add mode, set default values: 
     Me.ctlAge.Value = 0 
     Me.ctlBirthday.Value = Date.Now 
     Me.ctlName.Text = String.Empty 
    Else 
     'Edit mode, set current values: 
     Me.ctlAge.Value = CDec(row.Item("AGE")) 
     Me.ctlBirthday.Value = CDate(row.Item("BIRTHDAY")) 
     Me.ctlName.Text = CStr(row.Item("NAME")) 
    End If 
End Sub 

您還需要接受按鈕和取消按鈕。

Friend Sub btnAcceptClicked(sender As Object, e As EventArgs) Handles btnAccept.Click 
    Me.DialogResult = Windows.Forms.DialogResult.OK 
    Me.Close() 
End Sub 

Friend Sub btnCancelClicked(sender As Object, e As EventArgs) Handles btnCancel.Click 
    Me.DialogResult = Windows.Forms.DialogResult.Cancel 
    Me.Close() 
End Sub 

主要形式

添加方法:

Private Sub btnAddClicked(sender As Object, e As EventArgs) Handles btnAdd.Click 
    Try 
     Using f As New AddOrEditForm(CType(Nothing, DataRowView)) 
      If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then 

       Dim view As DataView = TryCast(Me.SalesDataGridView.DataSource, DataView) 

       If (view Is Nothing) Then 
        Throw New InvalidCastException() 
       End If 

       Dim viewRow As DataRowView = view.AddNew() 

       viewRow.EndEdit() 
       viewRow.Item("AGE") = f.ctlAge.Value 
       viewRow.Item("BIRTHDAY") = f.ctlBirthday.Value 
       viewRow.Item("NAME") = f.ctlName.Text 
       viewRow.EndEdit() 

      End If 
     End Using 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 
End Sub 

編輯方法:

Private Sub btnEditClicked(sender As Object, e As EventArgs) Handles btnEdit.Click 
    Try 

     Me.SalesDataGridView.EndEdit() 

     If (Me.SalesDataGridView.SelectedRows.Count > 0) Then 

      Dim gridRow As DataGridViewRow = Me.SalesDataGridView.SelectedRows(0) 
      Dim viewRow As DataRowView = TryCast(gridRow.DataBoundItem, DataRowView) 

      If (viewRow Is Nothing) Then 
       Throw New InvalidCastException() 
      End If 

      Using f As New AddOrEditForm(viewRow) 
       If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then 
        viewRow.BeginEdit() 
        Try 
         viewRow.Item("AGE") = f.ctlAge.Value 
         viewRow.Item("BIRTHDAY") = f.ctlBirthday.Value 
         viewRow.Item("NAME") = f.ctlName.Text 
         viewRow.EndEdit() 
        Catch ex As Exception 
         viewRow.CancelEdit() 
         Throw ex 
        End Try 
       End If 
      End Using 

     End If 

    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 
End Sub 
相關問題