Domanda

Come faccio ad aggiungere un nuovo DataColumn a un oggetto DataTable che contiene già dati?

pseudocodice

//call SQL helper class to get initial data 
DataTable dt = sql.ExecuteDataTable("sp_MyProc");

dt.Columns.Add("NewColumn", type(System.Int32));

foreach(DataRow row in dr.Rows)
{
    //need to set value to NewColumn column
}
È stato utile?

Soluzione

solo andare avanti con il tuo codice - siete sulla strada giusta:

//call SQL helper class to get initial data 
DataTable dt = sql.ExecuteDataTable("sp_MyProc");

dt.Columns.Add("NewColumn", typeof(System.Int32));

foreach(DataRow row in dt.Rows)
{
    //need to set value to NewColumn column
    row["NewColumn"] = 0;   // or set it to some other value
}

// possibly save your Dataset here, after setting all the new values

Altri suggerimenti

Nel caso non sia foreach invece che per!?

//call SQL helper class to get initial data  
DataTable dt = sql.ExecuteDataTable("sp_MyProc"); 

dt.Columns.Add("MyRow", **typeof**(System.Int32)); 

foreach(DataRow dr in dt.Rows) 
{ 
    //need to set value to MyRow column 
    dr["MyRow"] = 0;   // or set it to some other value 
} 

Ecco una soluzione alternativa per ridurre Per / ForEach looping, questo sarebbe ridurre i tempi e gli aggiornamenti in modo rapido loop :)

 dt.Columns.Add("MyRow", typeof(System.Int32));
 dt.Columns["MyRow"].Expression = "'0'";

Solo si desidera impostare il parametro valore di default. Questo chiamando il metodo terzo sovraccarico.

dt.Columns.Add("MyRow", type(System.Int32),0);

Prova questo

> dt.columns.Add("ColumnName", typeof(Give the type you want));
> dt.Rows[give the row no like  or  or any no]["Column name in which you want to add data"] = Value;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top