2017-03-02 7 views
-1

(私の悪い英語のために申し訳ありません)私は私のお父さんのためにこのプログラムを作ったので、助けてください。C#OleDb dataGridViewテキストボックスで埋めてください

public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     connetionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Csaba\\Desktop\\Teszt.accdb ;"; 
     connection = new OleDbConnection(connetionString); 
     Sql = "select * from 2017"; 
     try 
     { 
      connection.Open(); 
      oledbAdapter = new OleDbDataAdapter(Sql, connection); 
      oledbAdapter.Fill(ds); 
      dataGridView1.DataSource = ds.Tables[0]; 
     } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     string[] rowS = { NumeleO, AdresaO, NrMatricolO, VJO, NrMO, DataO, TaurO, CodS, }; 


    int row = 0; 
     row = dataGridView1.Rows.Count - 1; 
     dataGridView1.Rows.Add(new Object[] { NumeleTxT.Text, AdresaTxT, NrMatricolTxT, VJTxT, NrMTxT, DataTxT, TaurTxt, CodTxT }); 
     dataGridView1.Refresh(); 
    } 
} 

Error: System.InvalidOperationException occurred

HResult=0x80131509 
    Source=System.Windows.Forms 
    StackTrace: 
    at System.Windows.Forms.DataGridViewRowCollection.Add(Object[] values) 
    at Apu_Proba_3.Form1.button3_Click(Object sender, EventArgs e) in C:\Users\Csaba\documents\visual studio 2017\Projects\Apu Proba 3\Apu Proba 3\Form1.cs:line 79 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
    at System.Windows.Forms.Button.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
at Apu_Proba_3.Program.Main() in C:\Users\Csaba\documents\visual studio 2017\Projects\Apu Proba 3\Apu Proba 3\Program.cs:line 19 

私が問題だかわかりません。助けてください。私はあなたのすべての質問に答えます。

+0

あなたのお父さんのためのプログラムを作る前に、自分でC#を学ぶべきです。 –

答えて

0

InvalidOperationExceptionsの場合、例外のスローされたケースを確認するために、例外(System.Windows.Forms.DataGridViewRowCollection.Add(Object[] values))の常にcheck the documentationが例外を投げるのが最善です。

あなたが値を手動で追加しているので、私が最初にあなたが最初にこのケースに違反しているかどうかを確認します:

The row returned by the DataGridView.RowTemplate property has more cells than there are columns in the control.

そして、あなたはこのケースに違反しているかどうかのチェック:

The DataGridView has no columns.

これらのいずれのケースでもない場合は、残りの例外のケースを調べて違反しているかどうかを確認してください。

関連する問題