每隔毫秒将DataGridview导出为CSV

时间:2015-09-28 09:33:38

标签: c# wpf csv datagridview datagrid

我有一个将Windows.Form Application DataGridView导出为CSV的功能。 它工作正常,但问题是我的DataGridView有2行每毫秒更新一次,但输出到CSV没有,它只是在应用程序关闭后输出值......

    public void writeCSV(DataGridView gridIn, string outputFile)
    {
        //test to see if the DataGridView has any rows
        if (gridIn.RowCount > 0)
        {
            string value = "";
            DataGridViewRow dr = new DataGridViewRow();
            StreamWriter swOut = new StreamWriter(outputFile);

            //write header rows to csv
            for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
            {
                if (i > 0)
                {
                    swOut.Write(",");
                }
                swOut.Write(gridIn.Columns[i].HeaderText);
            }

            swOut.WriteLine();

            //write DataGridView rows to csv
            for (int j = 0; j <= gridIn.Rows.Count - 1; j++)
            {
                if (j > 0)
                {
                    swOut.WriteLine();
                }

                dr = gridIn.Rows[j];

                for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
                {
                    if (i > 0)
                    {
                        swOut.Write(",");
                    }

                    value = dr.Cells[i].Value.ToString();
                    //replace comma's with spaces
                    value = value.Replace(',', ' ');
                    //replace embedded newlines with spaces
                    value = value.Replace(Environment.NewLine, " ");

                    swOut.Write(value);
                }
            }
            swOut.Close();
        }
    }

我从SetDataGridView()函数中调用了该函数,我每隔几秒就使用它来更新DataGrids行,但它仍然没有像DataGrid那样每毫秒更新一次。

当DataGridView.Rows本身更新时,如何每毫秒更新一次CSV文件?

3 个答案:

答案 0 :(得分:0)

向DataGrid添加 TargetUpdated 事件:

<DataGrid TargetUpdated="DataGrid_TargetUpdated" ...

然后在每个DataGrid列定义中设置 NotifyOnTargetUpdated = True

<DataGridColumn Binding="{Binding Path=...,  NotifyOnTargetUpdated=True,
 UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

然后在TargetUpdated处理程序中调用writeCSV方法:

private void DataGrid_TargetUpdated(object sender, DataTransferEventArgs e)
{
      writeCSV(DataGridView,outputFile)
} 

答案 1 :(得分:0)

我通过调用来自writeCSV(DataGridView,outputFile)的{​​{1}}来解决它,只要将Timer类添加到C#应用程序中,就会自动创建private void timer_0_Tick(object sender, EventArgs e) ... 并且,每次Tick更新DataGridView

时,它都会开始更新CSV

答案 2 :(得分:0)

从更改代码 gridIn.Columns 至 gridIn.HeaderRow.Cells 由于gridIn.Columns不提供数据

替换后的最终代码是:

 public void writeCSV(DataGridView gridIn, string outputFile)
{
    //test to see if the DataGridView has any rows
    if (gridIn.RowCount > 0)
    {
        string value = "";
        DataGridViewRow dr = new DataGridViewRow();
        StreamWriter swOut = new StreamWriter(outputFile);

        //write header rows to csv
        for (int i = 0; i <= gridIn.HeaderRow.Cells.Count - 1; i++)
        {
            if (i > 0)
            {
                swOut.Write(",");
            }
            swOut.Write(gridIn.HeaderRow.Cells[i].HeaderText);
        }

        swOut.WriteLine();

        //write DataGridView rows to csv
        for (int j = 0; j <= gridIn.Rows.Count - 1; j++)
        {
            if (j > 0)
            {
                swOut.WriteLine();
            }

            dr = gridIn.Rows[j];

            for (int i = 0; i <= gridIn.HeaderRow.Cells.Count - 1; i++)
            {
                if (i > 0)
                {
                    swOut.Write(",");
                }

                value = dr.Cells[i].Value.ToString();
                //replace comma's with spaces
                value = value.Replace(',', ' ');
                //replace embedded newlines with spaces
                value = value.Replace(Environment.NewLine, " ");

                swOut.Write(value);
            }
        }
        swOut.Close();
    }
}