datatableの特定の列の値をループしたいですか?誰かがC#コーディングを教えてもらえますか?
3 に答える
DataTable tbl = new DataTable();
foreach (DataRow row in tbl.Rows)
{
object cellData = row["colName"];
}
以下のLinqベースのソリューションを試してください
var values = (from t in dt.AsEnumarable() // dt is your data table
select t[ColumnName]).ToList().ForEach(your expression );
or try normal way
foreach(DataRow drow in dt.Rows)
{
string value = drow[columnname].ToString();
}
こんにちは、以下のコード スニペットを試してください
システムを使用する; System.Collections.Generic の使用; System.Linq を使用します。 System.Text を使用します。 System.Data の使用;名前空間 ConsoleApplication1 { クラス プログラム { public static DataTable GetDataTable() { // 新しい DataTable オブジェクトを作成します DataTable objDataTable = new DataTable(); // string 型の 3 つの列を作成します objDataTable.Columns.Add("列 1", typeof(string)); objDataTable.Columns.Add("列 2", typeof(string)); objDataTable.Columns.Add("列 3", typeof(string)); // この DataTable の行にデータを追加します objDataTable.Rows.Add(new string[] { "Row1 - Column1", "Row1 - Column2", "Row1 - Column3" }); objDataTable.Rows.Add(new string[] { "Row2 - Column1", "Row2 - Column2", "Row2 - Column3" }); objDataTable.Rows.Add(new string[] { "Row3 - Column1", "Row3 - Column2", "Row3 - Column3" }); objDataTable を返します。
} static void Main(string[] args) { foreach (DataRow row in GetDataTable().Rows) { object cellData = row["Column 1"]; Console.WriteLine(cellData); } } }
}