[英]C# - How can I check if it is Column 0 in the datatable?
I have a data table that I am creating from an excel file. 我有一个从Excel文件创建的数据表。 This works fine.
这很好。 But, I recently had a spec change and need to substitute a value in column 0 of the table.
但是,我最近进行了规格更改,需要替换表的第0列中的值。 I am having trouble getting my IF statement to fire.
我无法启动我的IF语句。 It is probably something simple I am overlooking.
我可能忽略了一些简单的事情。
Basically, if it is the first column in the row, I want to put a specific value I pulled elsewhere (I am pulling the color of the cell and putting in the RGB value). 基本上,如果它是该行的第一列,我想将我拉到的特定值放到其他地方(我将拉动单元格的颜色并放入RGB值)。 If it is any other column in the row, continue to read in the data as usual.
如果该行是其他任何列,请照常继续读取数据。 Please see the below code:
请看下面的代码:
DataTable tbl = new DataTable();
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
{
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
//var cellColor = ws.Cells[firstRowCell].Style.Fill.BackgroundColor.LookupColor();
}
//var startRow = hasHeader ? 2 : 1;
for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var currentRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
DataRow row = tbl.Rows.Add();
for (var i = 0; i < tbl.Columns.Count; i++)
{
//This is my problem. I am not seeing what needs to be in here.
if (tbl.Columns.????????)
{
row[i] = colorTable[rowNum];
}
else
{
row[i] = currentRow[rowNum, i + 1].Text;
}
}
}
You're in a for loop that starts at your first column to the last 您处于for循环中,该循环从第一列开始到最后一列
for (var i = 0; i < tbl.Columns.Count; i++)
So the value you can use to know at what column you're at would be the i
, you could have something like this 因此,您可以用来知道您在哪一列的值就是
i
,您可能会有类似这样的信息
for (var i = 0; i < tbl.Columns.Count; i++)
{
//If you're at the first column
if (i == 0)
{
row[i] = colorTable[rowNum];
}
else
{
row[i] = currentRow[rowNum, i + 1].Text;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.