我想改组DataTable列值。我的桌子看起来像这样:
+------+------+------+------+------+
| col1 | col2 | col3 | col4 | col5 |
+------+------+------+------+------+
| 11 | 12 | 13 | 14 | 15 |
| 21 | 22 | 23 | 24 | 25 |
| 31 | 23 | 33 | 34 | 25 |
+------+------+------+------+------+
现在,我想发生的事情是随机地随机更改 col2 , col3 和 col4 的单元格值。示例:
+------+------+------+------+------+
| col1 | col2 | col3 | col4 | col5 |
+------+------+------+------+------+
| 11 | 14 | 12 | 13 | 15 |
| 21 | 23 | 24 | 22 | 25 |
| 31 | 32 | 34 | 33 | 35 |
+------+------+------+------+------+
到目前为止,这是我的代码。
private void shuffleDT()
{
DataTable sdt = new DataTable();
sdt.Columns.Add("col1", typeof(string));
sdt.Columns.Add("col2", typeof(string));
sdt.Columns.Add("col3", typeof(string));
sdt.Columns.Add("col4", typeof(string));
sdt.Columns.Add("col5", typeof(string));
sdt.Rows.Add(new object[] { "11", "12", "13", "14", "15" });
sdt.Rows.Add(new object[] { "21", "22", "23", "24", "25" });
sdt.Rows.Add(new object[] { "31", "32", "33", "34", "35"});
}
答案 0 :(得分:0)
这是Random
private DataTable sdt = new DataTable();
private void InitDt()
{
sdt = new DataTable();
sdt.Columns.Add("col1", typeof(string));
sdt.Columns.Add("col2", typeof(string));
sdt.Columns.Add("col3", typeof(string));
sdt.Columns.Add("col4", typeof(string));
sdt.Columns.Add("col5", typeof(string));
sdt.Rows.Add(new object[] { "11", "12", "13", "14", "15" });
sdt.Rows.Add(new object[] { "21", "22", "23", "24", "25" });
sdt.Rows.Add(new object[] { "31", "23", "33", "34", "25" });
}
private void ShuffleDT()
{
Random loRan = new Random(DateTime.Now.Millisecond);
int lnRan;
string[] loRowValues = new string[3];
List<int> loList;
foreach (DataRow loRow in sdt.Rows)
{
loList = new List<int>();
while (loList.Count < 3)
{
if (loList.Count == 2)
loList.Add(new List<int>() { 0, 1, 2 }.Except(loList).First());
else
{
lnRan = loRan.Next(0, 3);
if (!loList.Contains(lnRan))
loList.Add(lnRan);
}
}
loRowValues[0] = loRow.Field<string>(1);
loRowValues[1] = loRow.Field<string>(2);
loRowValues[2] = loRow.Field<string>(3);
loRow.SetField<string>(1, loRowValues[loList[0]]);
loRow.SetField<string>(2, loRowValues[loList[1]]);
loRow.SetField<string>(3, loRowValues[loList[2]]);
}
}