数据存放在txt文件中,dataGridView1.DataSource = ds.Tables[0].DefaultView; 显示数据,然后把矩阵转化为特定格式:<label> <index1>:<value1> <index2>:<value2> ...SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "文档(*.txt)|*.txt";
            StreamWriter output = null;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                output = new StreamWriter(sfd.FileName);
                try
                {
                    if (dt == null)
                    {
                        MessageBox.Show("数据集中无数据");
                    }
                    else
                    {                        int row = dt.Rows.Count;
                        int col = dt.Columns.Count;                        for (int r = 0; r < row; r++)
                        {
                            output.Write(dt.Rows[r][0].ToString());
                           
                   
                            for (int c = 1; c < col; c++)
                            {
                                output.Write((c - 1).ToString());
                                output.Write(":");
                                output.Write(dt.Rows[r][c].ToString());                            }
                          output.WriteLine();
                        }
                    }
                    output.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("Failed save the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                             
            }结果和以前的矩阵一样 没有变?C#刚上手,求高手指点?

解决方案 »

  1.   

    这里改一下string st="";
    for (int r = 0; r < row; r++)
      {
        st=string.Format("<{0}>",dt.Rows[r][0]);
        
      for (int c = 1; c < col; c++)
      {
        st+=string.Format("<{0}>:<{1}>",c-1,dt.Rows[r][c]);
      }
      output.WriteLine(st);
      }
      

  2.   

    <494 1.25 0.2273 29 26448 587.5 1.3486 92.875 0.14>
    <498 1.27 0.2298 28.625 26368 588.5 1.3447 92.9063 0.15>
    <505 1.26 0.236 29 26416 589.5 1.3467 92.9688 0.14>
    ....................还是没达到转化格式的目的。
      

  3.   

    是不是和datatable设置 有关系,修改for循环 对输出结果没影响?
      

  4.   

    <label> <index1>:<value1> <index2>:<value2> ...
    1 1:456 2:88 3:444
    45 1:45 2:5456 3:545这种 自己有点琢磨到了 datatable 列设置好像有问题