简体   繁体   English

将数据表转换为列表,反之亦然

[英]Converting Data Table to List and vice versa

I don't know what's wrong. 我不知道怎么了 I have a SQL Statement and an adapter 我有一个SQL语句和一个适配器

 DataTable datTableCur = new DataTable();
 datTable = new DataTable();

 sqlCmd = new SqlCommand(@"SELECT DISTINCT [" + form1.getCol() + "] FROM [" + form1.getTableName3() + "]", connection);
 sqlDatAdapter = new SqlDataAdapter(sqlCmd.CommandText, connection);
 sqlDatAdapter.Fill(datTableCur);

Since the format is "float" in SQL I convert it to "Double" in C# and put every element of the column in a List 由于格式在SQL中是“浮点型”,因此我在C#中将其转换为“双精度”,并将列的每个元素都放在列表中

 List<Double> convertCol = new List<Double>();
 List<Double> convertedCol = new List<Double>();

 foreach (DataRow row in datTableCur.Rows)
 {
     convertCol.Add((double)row[0]);
 }

Now I want to check if the elements have "," and if that's the case I want to replace the "," with a ".", so I convert every single element into a String, check this case, replace the char, convert it back to Double and store it in another List 现在,我要检查元素是否具有“,”,如果是这种情况,我想用“。”替换“,”,因此我将每个元素都转换为字符串,检查这种情况,替换字符,转换将其返回给Double并将其存储在另一个列表中

  String convertToString;
  Double storeDouble;
  Double convertBackToDouble;

  for (int i = 0; i < convertCol.Count; i++)
  {
      storeDouble = convertCol[i];
      convertToString = storeDouble.ToString("0.######");

      if (convertToString.Contains(","))
      {
          convertToString.Replace(",", ".");
          convertBackToDouble = Convert.ToDouble(convertToString);
          convertedCol.Add(convertBackToDouble);
      }
      else 
      {
          convertedCol.Add(convertCol[i]);
      }
  }

and now here's my problem. 现在这是我的问题。 I want to put that back into a DataTable , and put that in a ListBoxbut that doesn't work. 我想将其放回DataTable ,并将其放到ListBox中,但这不起作用。 I get an ArgumentException and the error, that the input array is longer than the number of columns in the table. 我收到ArgumentException和错误,即输入数组的长度大于表中的列数。

datTable.Rows.Add(form1.getCol());

for (int j = 1; j < convertedCol.Count; j++)
{
    datTable.Rows.Add(convertedCol[j]);
}

form1.colList.DisplayMember = form1.getCol();
form1.colList.ValueMember = "Column";
form1.col.DataSource = datTable;

The error you are facing due to you need to add the column first in the datatable you are adding or to replace the precious orignal value column you need to add the column with same name 你正面临由于你需要添加错误column先在datatable要添加或更换珍贵的一部开拓创新的值列则需要添加与相同名称的列

DataColumn dc = new DataColumn("col2");
dc.DataType = typeof(double);
table.Columns.Remove("col2");
table.Columns.Add(dc);

and you can set the values then. 然后您可以设置值。

You can shorten your loop like below to get your list of double values.(Not tested may have syntax error). 您可以像下面那样缩短循环,以获取双精度值列表。(未经测试可能会出现语法错误)。

var result doubleValueList = dt.AsEnumerable()
         .Select(x=> new {double.Parse(x.ToString("0.######").Replace(",", "."))})
         .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM