C#怎么取到datatable中数据的每一列的标题部分?
2013-04-04
展开全部
我动手给你写的一个代码示例,希望对你有帮助: Code: DataTable dt = new DataTable();
dt.Columns.Add("姓名");
dt.Columns.Add("年龄");
dt.Columns.Add("性别"); DataRow dr; dr = dt.NewRow();
dr["姓名"] = "张三";
dr["年龄"] = "21";
dr["性别"] = "男";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["姓名"] = "李四";
dr["年龄"] = "22";
dr["性别"] = "男";
dt.Rows.Add(dr); foreach (DataColumn c in dt.Columns)
{
Console.WriteLine(c.ColumnName);//输出列名
} foreach (DataRow r in dt.Rows)
{
Console.WriteLine(r["姓名"].ToString() + r["年龄"].ToString() + r["性别"].ToString());//输出各行记录
}附截图:
dt.Columns.Add("姓名");
dt.Columns.Add("年龄");
dt.Columns.Add("性别"); DataRow dr; dr = dt.NewRow();
dr["姓名"] = "张三";
dr["年龄"] = "21";
dr["性别"] = "男";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["姓名"] = "李四";
dr["年龄"] = "22";
dr["性别"] = "男";
dt.Rows.Add(dr); foreach (DataColumn c in dt.Columns)
{
Console.WriteLine(c.ColumnName);//输出列名
} foreach (DataRow r in dt.Rows)
{
Console.WriteLine(r["姓名"].ToString() + r["年龄"].ToString() + r["性别"].ToString());//输出各行记录
}附截图:
展开全部
#region SJF 算法下的功能分析
//创建行索引
int rowIndexSJF=0;
int MinNeedTime = 100;
//遍历查询最短服务时间的进程
for (int i = 1; i < dtReadyP.Rows.Count; i++)
{
if (MinNeedTime > Int32.Parse(dtReadyP.Rows[i]["needTime"].ToString()))//查询条件
{
MinNeedTime = Int32.Parse(dtReadyP.Rows[i]["needTime"].ToString());
}
}
//由列值得到对应的行索引
for (int i1 = 1; i1 < dtReadyP.Rows.Count; i1++)
{
if (Int32.Parse(dtReadyP.Rows[i1]["needTime"].ToString())== MinNeedTime)//查询条件
{
//进行操作
rowIndexSJF = i1;
}
}
int tag2 = Convert.ToInt32(dtReadyP.Rows[rowIndexSJF][5].ToString());
Label2.Text = dtReadyP.Rows[rowIndexSJF][0].ToString();
Label3.Text = "运行中...";
Label4.Text = dtReadyP.Rows[rowIndexSJF][2].ToString();
Label5.Text = dtReadyP.Rows[rowIndexSJF][3].ToString();
Label6.Text = DateTime.Now.ToString("hh:mm:ss");
Label7.Text = dtReadyP.Rows[rowIndexSJF][4].ToString();
Label8.Text = tag2.ToString();
//删除一行数据,并重新绑定
dtReadyP.Rows[rowIndexSJF].Delete();
break;
#endregion
//创建行索引
int rowIndexSJF=0;
int MinNeedTime = 100;
//遍历查询最短服务时间的进程
for (int i = 1; i < dtReadyP.Rows.Count; i++)
{
if (MinNeedTime > Int32.Parse(dtReadyP.Rows[i]["needTime"].ToString()))//查询条件
{
MinNeedTime = Int32.Parse(dtReadyP.Rows[i]["needTime"].ToString());
}
}
//由列值得到对应的行索引
for (int i1 = 1; i1 < dtReadyP.Rows.Count; i1++)
{
if (Int32.Parse(dtReadyP.Rows[i1]["needTime"].ToString())== MinNeedTime)//查询条件
{
//进行操作
rowIndexSJF = i1;
}
}
int tag2 = Convert.ToInt32(dtReadyP.Rows[rowIndexSJF][5].ToString());
Label2.Text = dtReadyP.Rows[rowIndexSJF][0].ToString();
Label3.Text = "运行中...";
Label4.Text = dtReadyP.Rows[rowIndexSJF][2].ToString();
Label5.Text = dtReadyP.Rows[rowIndexSJF][3].ToString();
Label6.Text = DateTime.Now.ToString("hh:mm:ss");
Label7.Text = dtReadyP.Rows[rowIndexSJF][4].ToString();
Label8.Text = tag2.ToString();
//删除一行数据,并重新绑定
dtReadyP.Rows[rowIndexSJF].Delete();
break;
#endregion
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
for (int i = 0; i < dt.Columns.Count; i++)
{
string 列标题 = dt.Columns[i].ColumnName;
//你自己的代码
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-04-03
展开全部
foreach(dataColumn dc in dt.Columns)
{
string cn = dc.ColumnName;
}
{
string cn = dc.ColumnName;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询