c#获取datatable中的某列数据并改变

2024-11-08 08:53:33
推荐回答(2个)
回答1:

for(int i=0;i{
string times=dataset.table[0].rows[i][0].tostring()+":"+dataset.tables[0].rows[i][1].tostring();
//这里写输出times或者你可以装载到list中
}

回答2:

private void Merger()
{
//模拟数据
DataTable sourceData = new DataTable();
sourceData.Columns.Add("Minute", typeof(int));
sourceData.Columns.Add("Seconds", typeof(int));
DataRow row1 = sourceData.NewRow();
row1[0] = 0;
row1[1] = 1;
sourceData.Rows.Add(row1);
DataRow row2 = sourceData.NewRow();
row2[0] = 1;
row2[1] = 5;
sourceData.Rows.Add(row2);
DataRow row3 = sourceData.NewRow();
row3[0] = 2;
row3[1] = 10;
sourceData.Rows.Add(row3);
DataRow row4 = sourceData.NewRow();
row4[0] = 3;
row4[1] = 20;
sourceData.Rows.Add(row4);
//return DataTable
DataTable mergerTable = new DataTable();
mergerTable.Columns.Add("Time", typeof(string));
//return List
List mergerList = new List();
foreach (DataRow row in sourceData.Rows)
{
// 合并操作 主要使用PadLeft方法 指定字符长度 不足时在前面补零
string mergerString = string.Format("{0}:{1}",
row[0].ToString().PadLeft(2, '0'),
row[1].ToString().PadLeft(2, '0'));
// DataTable add
DataRow tmpRow = mergerTable.NewRow();
tmpRow[0] = mergerString;
mergerTable.Rows.Add(tmpRow);
// List add
mergerList.Add(mergerString);
}
}