将数据从datagridview保存在c#

时间:2018-11-19 08:28:17

标签: c# datagridview save

1我在存储数据时遇到问题。 在此之前,我已经在DataGridview中显示了数据,并且我想以* txt的形式存储来自DataGridview的数据。我试图保存数据,但仅存储了一个数据,而不是全部。

在这里,我向您展示我的代码。希望可以有人帮帮我?非常感谢

row.Cells["colProjection"].Value = string.Join("\t", feature.Select(f => Math.Abs(f / 30).ToString("N3")));
            TextWriter writer = new StreamWriter(@"C:\Users\LENOVO\Desktop\OKE.txt");
            writer.Write(row.Cells["colProjection"].Value);
            writer.WriteLine("");
            writer.Close();

2 个答案:

答案 0 :(得分:0)

您应该获取所有行数据,可以使用for循环来执行此操作,如下所示:

  StringBuilder sr=new StringBuilder();
  for (int i=0 ;i>=mygrid.Rows.Count;i++)
  {
     sr.AppendLine(mygrid.Rows[i].Cells["colProjection"].Text);
  }

  System.IO.File.WriteAllText("D:\output.txt",sr.ToString());

更新:

private void btnFeature_Click(object sender, EventArgs e)
{
    if (pca == null)
    {
        MessageBox.Show("Please compute the analysis first!");
        return;
    }

    ImageToArray converter = new ImageToArray(min: 0, max: 1);

    int rows = dataGridView3.Rows.Count;
    double[][] inputs = new double[rows][];
    double[][] features = new double[rows][];
    int[] outputs = new int[rows];

    int index = 0;
    StringBuilder sr=new StringBuilder();
    foreach (DataGridViewRow row in dataGridView3.Rows)
    {
        Bitmap image = row.Cells["colFace2"].Value as Bitmap;
        int label = (int)row.Cells["colLabel2"].Value;

        double[] input;
        converter.Convert(image, out input);

        double[] feature = pca.Transform(input);

        row.Cells["colProjection"].Value = string.Join("\t", feature.Select(f => Math.Abs(f / 30).ToString("N3")));
       // TextWriter writer = new StreamWriter(@"C:\Users\LENOVO\Desktop\OKE.txt");
        /*for (int i = 0; i < dataGridView3.RowCount; i++)
        {
            writer.Write(); 
        }*/

        sr.Append(row.Cells["colProjection"].Text);// Or .Value


        row.Tag = feature;
        inputs[index] = input;
        features[index] = feature;
        outputs[index] = label;
        index++;
    }
  System.IO.File.WriteAllText("C:\Users\LENOVO\Desktop\OKE.txt",sr.ToString());
}

答案 1 :(得分:0)

这是我用来存储数据的代码。 但是存储的数据仍然有错误。因为存储的数据是最后一行数据,而不是第一行到最后一行的数据。

private void btnFeature_Click(object sender, EventArgs e)
    {
        if (pca == null)
        {
            MessageBox.Show("Please compute the analysis first!");
            return;
        }

        ImageToArray converter = new ImageToArray(min: 0, max: 1);

        int rows = dataGridView3.Rows.Count;
        double[][] inputs = new double[rows][];
        double[][] features = new double[rows][];
        int[] outputs = new int[rows];

        int index = 0;
        foreach (DataGridViewRow row in dataGridView3.Rows)
        {
            Bitmap image = row.Cells["colFace2"].Value as Bitmap;
            int label = (int)row.Cells["colLabel2"].Value;

            double[] input;
            converter.Convert(image, out input);

            double[] feature = pca.Transform(input);

            row.Cells["colProjection"].Value = string.Join("\t", feature.Select(f => Math.Abs(f / 30).ToString("N3")));
            TextWriter writer = new StreamWriter(@"C:\Users\LENOVO\Desktop\OKE.txt");
            for (int i = 0; i < dataGridView3.RowCount; i++)
            {
                writer.Write(row.Cells["colProjection"].Value + "\t"); 
            }
            writer.WriteLine("");
            writer.Close();

            row.Tag = feature;
            inputs[index] = input;
            features[index] = feature;
            outputs[index] = label;
            index++;
        }
    }