2017-10-25 2 views
1

私はID列idを作成しました。自動インクリメントはtrueとなりますが、空です。なぜデータテーブル内の自動インクリメント列が空になるのですか?

なぜですか?

 Datatable dt= new datatable(); 

     DataColumn dc = new DataColumn("id", typeof(int)); 

     dc.AutoIncrement = true; 
     dc.AutoIncrementSeed = 1; 
     dc.AutoIncrementStep = 1; 
     dt.Columns.Add(dc); 

     dc = new DataColumn("NITNo", typeof(string)); 
     dc.DefaultValue = txtNitNo.Text.ToString(); 
     dt.Columns.Add(dc); 

     dc = new DataColumn("WorkNo", typeof(string)); 
     dc.DefaultValue = txtWorkNo.Text.ToString(); 
     dt.Columns.Add(dc); 
     //dt.Rows.rem 

     //Bind Data to GridView 
     gvBOQ.Caption = Path.GetFileName(FilePath); 
     gvBOQ.DataSource = dt; 
     gvBOQ.DataBind(); 

答えて

2

あなたがたDataTableにデータを追加する場合は、ID列の値が結果のテーブルには、ハローこの

id value 
1 Netherlands 
2 Japan 
99 Australia 
100 America 
+0

次のようになりますnull

//create a datatable DataTable table = new DataTable(); //auto increment column DataColumn column = new DataColumn("id", typeof(int)); column.AutoIncrement = true; column.AutoIncrementSeed = 1; column.AutoIncrementStep = 1; table.Columns.Add(column); //add a normal column column = new DataColumn("value", typeof(string)); table.Columns.Add(column); //add some data to the table table.Rows.Add(null, "Netherlands"); table.Rows.Add(null, "Japan"); table.Rows.Add(99, "Australia"); table.Rows.Add(null, "America"); 

であることを確認する必要があります申し訳ありませんが、元のデータを乱してクライアント側で問題を引き起こす可能性があるため、偽のデータを挿入することはできません。 –

+1

偽のデータはデモ用です。重要なのは、datatableを埋め込むときにidカラムが空のままであることです。 – VDWWD

+0

私は全く新しいので、コードを変更できますか? –

関連する問題