2016-04-07 13 views
0

これらのコードに問題があります。私はデータセットに行を追加したいのですが、私はこのエラー "sampleDataSet.bar_of_4Rowの非静的フィールド、メソッド、またはプロパティにオブジェクト参照が必要です"があります。私は静的にしようとしましたが、うまくいきませんでした。私はこのチュートリアルのようにそれを実行しようとしました:https://msdn.microsoft.com/en-us/library/5ycd1034.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1C#エラーでテーブルに行を追加する(Visual Studio)

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace NaplnenieDB 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 

      sampleDataSet.bar_of_4Row newBarRow = sampleDataSet.bar_of_4.Newbar_of_4Row(); 
      newBarRow.bar = "RRRR"; 
      newBarRow.first = "R"; 
      sampleDataSet.bar_of_4.Addbar_of_4Row(newBarRow); 



     } 


    } 
} 

答えて

0

は、あなたがそれにメソッドNewbar_of_4Row()を呼び出すために、クラスsampleDataSet.bar_of_4のオブジェクトを作成する必要があります。

はこれを試してみてください:

var myDataSet= new sampleDataSet(); 
sampleDataSet.bar_of_4Row newBarRow = myDataSet.bar_of_4.Newbar_of_4Row(); 
newBarRow.bar = "RRRR"; 
newBarRow.first = "R"; 
myDataSet.bar_of_4.Addbar_of_4Row(newBarRow); 

は、そのクラス名は大文字と小文字の変数名で始まるで始まる大会に固執するようにしてください。それはこれをすべてあまり混乱させません。

+0

このサンプルデータセットは、他のプロジェクトからこのプロジェクトのデータソースにインポートされたDataSetであるため、このプロジェクトではいくつかの行を追加したいのですが、別のプロジェクトではテーブルからのみデータを選択します。 –

0

これにより、新しいDataTable、DataRow(s)、DataColumn(s)が作成され、値を入力できるようになります。

これが役に立ちます。

static void Main(string[] args) 
    { 

     // create table 
     DataTable sampleDataTable = new DataTable("Bar_of_4"); 
     // create row 
     DataRow sampleDataRow = sampleDataTable.NewRow(); 
     // create column 
     DataColumn column; 

     // set info about the first column 
     column = new DataColumn(); 
     column.DataType = System.Type.GetType("System.String"); 
     column.ColumnName = "bar"; 
     sampleDataTable.Columns.Add(column); 

     // set info about the second column 
     column = new DataColumn(); 
     column.DataType = System.Type.GetType("System.String"); 
     column.ColumnName = "first"; 
     sampleDataTable.Columns.Add(column); 

     // add column data to the first row 
     sampleDataRow["bar"] = "RRRR"; 
     sampleDataRow["first"] = "R"; 
     sampleDataTable.Rows.Add(sampleDataRow); 

     // add column data to the second row 
     sampleDataRow = sampleDataTable.NewRow(); 
     sampleDataRow["bar"] = "SSSS"; 
     sampleDataRow["first"] = "S"; 
     sampleDataTable.Rows.Add(sampleDataRow); 


     // loop through each row and display the column info 
     foreach (DataRow row in sampleDataTable.Rows) 
     { 
      Console.WriteLine(string.Format("{0} - {1}",row["bar"],row["first"])); 
     } 

     Console.WriteLine("\n\nPress any key to continue"); 
     Console.ReadKey(); 
    } 
関連する問題