我在一个类中创建了一个DataTable对象并将其返回到主程序时遇到了一个问题。我调用一个method(1),该方法从数据表中检索一些信息,然后删除相同的行。然后,我运行另一个方法(2),该方法从列中检索数据,但是我可以看到在另一个方法(1)中删除的行没有生效。我已经计算了method(1)中的行数,并且行数确实减少了,这表明它们已被删除。
这与引用对象有关吗?
对于在问这个问题时遇到的任何错误,我们深表歉意。这是我对堆栈的第一个问题,我对编码还是很陌生。
谢谢!
主程序:
filepath = @"E:\1802370 11.05.18 qCML 6x10E6 ERM_analysed.txt";
//Create a datatable from _analysed file and delete unwated columns
DataTable dataTable = csv.convertToDataTable(filepath);
//Assign slope and r2 values for both BCR and ABL
//Delete rows after
plate.getSlopeandR2(dataTable);
plate.getSampleList(dataTable);
DataTable方法:
//Converts text file into a datatable to be manipluated.
public DataTable convertToDataTable(string filepath)
{
StreamReader sr = new StreamReader(filepath);
string[] headers = sr.ReadLine().Split('\t');
DataTable dt = new DataTable();
foreach (string header in headers)
{
dt.Columns.Add(header);
}
while (!sr.EndOfStream)
{
string[] rows = Regex.Split(sr.ReadLine(), "\t");
DataRow dr = dt.NewRow();
for (int i = 0; i < headers.Length; i++)
{
dr[i] = rows[i];
}
dt.Rows.Add(dr);
}
sr.Close();
return dt;
方法(1):获取值删除行
//Get slope and r2 values
//Delete rows after info is retrieved
public void getSlopeandR2(DataTable dataTable)
{
bool isABL = true;
List<int> rowsToDelete = new List<int>();
Console.WriteLine(dataTable.Rows.Count);
foreach (DataRow row in dataTable.Rows)
{
int rowIndex;
if (row["Well"].ToString() == "Slope" && isABL == true)
{
rowIndex = dataTable.Rows.IndexOf(row);
ablSlope = Convert.ToDouble(dataTable.Rows[rowIndex][1]);
ablR2 = Convert.ToDouble(dataTable.Rows[rowIndex + 2][2]);
isABL = false;
rowsToDelete.Add(rowIndex);
rowsToDelete.Add(rowIndex + 1);
rowsToDelete.Add(rowIndex + 2);
rowsToDelete.Add(rowIndex + 3);
rowsToDelete.Add(rowIndex + 4);
rowsToDelete.Add(rowIndex + 5);
}
else if (row["Well"].ToString() == "Slope" && isABL == false)
{
rowIndex = dataTable.Rows.IndexOf(row);
biomedSlope = Convert.ToDouble(dataTable.Rows[rowIndex][1]);
biomedR2 = Convert.ToDouble(dataTable.Rows[rowIndex + 2][2]);
rowsToDelete.Add(rowIndex);
rowsToDelete.Add(rowIndex + 1);
rowsToDelete.Add(rowIndex + 2);
rowsToDelete.Add(rowIndex + 3);
rowsToDelete.Add(rowIndex + 4);
rowsToDelete.Add(rowIndex + 5);
rowsToDelete.Add(rowIndex + 6);
}
}
for (int i = 0; i < rowsToDelete.Count; i++)
{
dataTable.Rows.RemoveAt(i);
}
Console.WriteLine(dataTable.Rows.Count);
}
方法(2):检索数据(仍包括已删除的行)
//Get unique list of sample ID's on plate
public void getSampleList(DataTable dataTable)
{
string tempValue;
foreach (DataRow row in dataTable.Rows)
{
if (!Samples.Contains(row["Sample Name"].ToString()))
{
tempValue = Convert.ToString(row["Sample Name"]);
Samples.Add(tempValue);
}
}
foreach (string id in Samples)
{
Console.WriteLine(id);
}
}
答案 0 :(得分:1)
有两种方法可以从DataTable中删除DataRow。读取文件后,调用AcceptChanges()在内部提交DataTable。
//Created new table sample
DataTable dt1 = new DataTable("dt1");
dt1.Columns.Add("Id", typeof(int));
dt1.Columns.Add("Name");
//Added dummy data
dt1.Rows.Add(1, "Abc");
dt1.Rows.Add(2, "Def");
//You must accept changes after reading file, so you can track changes
dt1.AcceptChanges();
//Below line will remove row permanently.
dt1.Rows.RemoveAt(0);
//Below line will mark row as deleted, still row count will be same till AcceptChanges() called.
dt1.Rows[0].Delete();
dt1.AcceptChanges();
答案 1 :(得分:0)
从表中删除行后,接受更改
dataTable.AcceptChanges()
for (int i = 0; i < rowsToDelete.Count; i++)
{
dataTable.Rows.RemoveAt(i);
dataTable.AcceptChanges();
}