首页 \ 问答 \ 如何将datagridview中的值存储到集合类C#中(How to store the value in datagridview in to a collection class C#)

如何将datagridview中的值存储到集合类C#中(How to store the value in datagridview in to a collection class C#)

首先,我将csv文件导入datagridview并将其更改为我想要的格式。

我可以通过单击按钮过滤掉我不想要的数据,通过使用console.writeline获取我想要的数据。 (仅用于测试,所以我可以看到实际发生的事情)。 有相当数量的列不满足我想要计算的数据,所以我使用.contains().replace来过滤它。

现在,我想存储cell.value.toString(),它将值保存到数组但我不知道如何将数组植入其中。

这是代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace test2
{
    public partial class Form1 : Form
    {   
        OpenFileDialog openFile = new OpenFileDialog();

        public int colC { get; private set; }

        public Form1()
        {
            InitializeComponent();
        }


        public void Button1_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                List<string[]> rows = File.ReadLines(openFile.FileName).Select(x => x.Split(',')).ToList();
                DataTable dt = new DataTable();
                List<string> headerNames = rows[0].ToList();

                foreach (var headers in rows[0])
                {
                    dt.Columns.Add(headers);
                }
                foreach (var x in rows.Skip(1).OrderBy(r => r.First()))
                {
                    if (x[0] == "Lot ID")   //linq to check if 2 lists are have the same elements (perfect for strings)
                        continue;     //skip the row with repeated headers
                    if (x.All(val => string.IsNullOrWhiteSpace(val))) //if all columns of the row are whitespace / empty, skip this row
                        continue;
                    dt.Rows.Add(x);
                }

                dataGridView1.DataSource = dt;
                dataGridView1.ReadOnly = true;
               // dataGridView1.Columns["Lot ID"].ReadOnly = true;    
                //dataGridView1.Columns[" Magazine ID"].ReadOnly = true;
                //dataGridView1.Columns["Frame ID"].ReadOnly = true;

            }
        }

        public void Form1_Load_1(object sender, EventArgs e)
        {
            openFile.Filter = "CSV|*.csv";

        }

        public void Button2_Click(object sender, EventArgs e)
        {

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "txt files (*.txt)|*.txt|Csv files (*.csv)|*.csv";
            sfd.FilterIndex = 2;


            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    StreamWriter sw = new StreamWriter(sfd.FileName, false);
                    {
                        string columnHeaderText = "";

                        int countColumn = dataGridView1.ColumnCount - 1;

                        if (countColumn >= 0)
                        {
                            columnHeaderText = dataGridView1.Columns[0].HeaderText;
                        }

                        for (int i = 1; i <= countColumn; i++)
                        {
                            columnHeaderText = columnHeaderText + ',' + dataGridView1.Columns[i].HeaderText;
                        }


                        sw.WriteLine(columnHeaderText);

                        foreach (DataGridViewRow dataRowObject in dataGridView1.Rows)
                        {
                            if (!dataRowObject.IsNewRow)
                            {
                                string dataFromGrid = "";

                                dataFromGrid = dataRowObject.Cells[0].Value.ToString();

                                for (int i = 1; i <= countColumn; i++)
                                {
                                    dataFromGrid = dataFromGrid + ',' + dataRowObject.Cells[i].Value.ToString();

                                }
                                sw.WriteLine(dataFromGrid);
                            }
                        }

                        sw.Flush();
                        sw.Close();
                    }
                }
                catch (Exception exceptionObject)
                {
                    MessageBox.Show(exceptionObject.ToString());
                }
                }
            }

        public void Button3_Click(object sender, EventArgs e)
        {


            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    //prevent error occur coz , only execute the not null line
                    if(cell.Value != null)
                    {
                        string cellV = cell.Value.ToString();

                        //do operations with cell, filter out the unnecessary data 
                        if (!cellV.Contains("="))
                        {
                            continue;
                        }
                       else if (cellV.Contains("P = "))
                        {
                            cellV = cellV.Replace("P = ", "");
                        }
                        else if (cellV.Contains("F = n/a"))
                        {
                            cellV =cellV.Replace("F = n/a", "0");

                        }
                        else if (cellV.Contains("F = "))
                        {
                            cellV = cellV.Replace("F = ", "");

                        }

                        Console.WriteLine(cellV + "\n");
                    }

                }
            }

        }

    }
    }

请查看最后一个方法和图片。 在此处输入图像描述

这是输出。 在此处输入图像描述

我使用continue跳过所有与计算无关的数据

如何存储我在数组中过滤掉的数据???

如果您对此问题有任何疑问,请随时询问。


First, I import a csv file to the datagridview and changed it to the format that I want.

I'm able to filter out the data that i do not want by clicking a button, i get the data i want in the console by using console.writeline. (just for testing so i can see what's actually happen). There are quite numbers of column that do not content the data i want for calculation, so i use .contains() and .replace to filter it out.

Now, i want to store the cell.value.toString() which hold the value to an array but i have no idea how to implant array to it.

Here is the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace test2
{
    public partial class Form1 : Form
    {   
        OpenFileDialog openFile = new OpenFileDialog();

        public int colC { get; private set; }

        public Form1()
        {
            InitializeComponent();
        }


        public void Button1_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                List<string[]> rows = File.ReadLines(openFile.FileName).Select(x => x.Split(',')).ToList();
                DataTable dt = new DataTable();
                List<string> headerNames = rows[0].ToList();

                foreach (var headers in rows[0])
                {
                    dt.Columns.Add(headers);
                }
                foreach (var x in rows.Skip(1).OrderBy(r => r.First()))
                {
                    if (x[0] == "Lot ID")   //linq to check if 2 lists are have the same elements (perfect for strings)
                        continue;     //skip the row with repeated headers
                    if (x.All(val => string.IsNullOrWhiteSpace(val))) //if all columns of the row are whitespace / empty, skip this row
                        continue;
                    dt.Rows.Add(x);
                }

                dataGridView1.DataSource = dt;
                dataGridView1.ReadOnly = true;
               // dataGridView1.Columns["Lot ID"].ReadOnly = true;    
                //dataGridView1.Columns[" Magazine ID"].ReadOnly = true;
                //dataGridView1.Columns["Frame ID"].ReadOnly = true;

            }
        }

        public void Form1_Load_1(object sender, EventArgs e)
        {
            openFile.Filter = "CSV|*.csv";

        }

        public void Button2_Click(object sender, EventArgs e)
        {

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "txt files (*.txt)|*.txt|Csv files (*.csv)|*.csv";
            sfd.FilterIndex = 2;


            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    StreamWriter sw = new StreamWriter(sfd.FileName, false);
                    {
                        string columnHeaderText = "";

                        int countColumn = dataGridView1.ColumnCount - 1;

                        if (countColumn >= 0)
                        {
                            columnHeaderText = dataGridView1.Columns[0].HeaderText;
                        }

                        for (int i = 1; i <= countColumn; i++)
                        {
                            columnHeaderText = columnHeaderText + ',' + dataGridView1.Columns[i].HeaderText;
                        }


                        sw.WriteLine(columnHeaderText);

                        foreach (DataGridViewRow dataRowObject in dataGridView1.Rows)
                        {
                            if (!dataRowObject.IsNewRow)
                            {
                                string dataFromGrid = "";

                                dataFromGrid = dataRowObject.Cells[0].Value.ToString();

                                for (int i = 1; i <= countColumn; i++)
                                {
                                    dataFromGrid = dataFromGrid + ',' + dataRowObject.Cells[i].Value.ToString();

                                }
                                sw.WriteLine(dataFromGrid);
                            }
                        }

                        sw.Flush();
                        sw.Close();
                    }
                }
                catch (Exception exceptionObject)
                {
                    MessageBox.Show(exceptionObject.ToString());
                }
                }
            }

        public void Button3_Click(object sender, EventArgs e)
        {


            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    //prevent error occur coz , only execute the not null line
                    if(cell.Value != null)
                    {
                        string cellV = cell.Value.ToString();

                        //do operations with cell, filter out the unnecessary data 
                        if (!cellV.Contains("="))
                        {
                            continue;
                        }
                       else if (cellV.Contains("P = "))
                        {
                            cellV = cellV.Replace("P = ", "");
                        }
                        else if (cellV.Contains("F = n/a"))
                        {
                            cellV =cellV.Replace("F = n/a", "0");

                        }
                        else if (cellV.Contains("F = "))
                        {
                            cellV = cellV.Replace("F = ", "");

                        }

                        Console.WriteLine(cellV + "\n");
                    }

                }
            }

        }

    }
    }

Please have a look in the last method and the Picture. enter image description here

Here is the output. enter image description here

I skip all the data which is not related to calculation by using continue

How do i store the data that i filtered out in array???

Please do not hesitate to ask if you have any inquiry about the question.


原文:https://stackoverflow.com/questions/44939611
更新时间:2022-12-15 10:12

最满意答案

因为您需要配置proguard ..请在此处查看RN官方文档

Proguard是一种可以略微减小APK大小的工具。 它通过剥离应用程序未使用的部分React Native Java字节码(及其依赖项)来实现此目的。

重要提示 :如果您启用了Proguard,请务必彻底测试您的应用。 Proguard通常需要特定于您正在使用的每个本机库的配置。 请参阅app / proguard-rules.pro。


Because you need to configure proguard.. Check the RN official docs here.

Proguard is a tool that can slightly reduce the size of the APK. It does this by stripping parts of the React Native Java bytecode (and its dependencies) that your app is not using.

IMPORTANT: Make sure to thoroughly test your app if you've enabled Proguard. Proguard often requires configuration specific to each native library you're using. See app/proguard-rules.pro.

相关问答

更多
  • 我会假设你有先决条件 ,但在继续之前要仔细检查。 Homebrew,Watchman,Node 4+和Flow(可选)。 当您在Xcode中打开项目并按下构建按钮(播放?)时,它应该自动启动打包服务器。 但在你的情况下,它似乎没有。 在这种情况下,尝试在构建项目之前从项目的根目录中运行终端中的npm start 。 I'm going to assume you have the pre-requisites, but double check before moving on. Homebrew, Wat ...
  • 我总是使用* .pro作为proguard文件。 尝试将* .txt更改为* .pro。 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' Looking at the logs pointed me to the right direction. The problem was a silent error in XML layout, in fact I had a button wit ...
  • 它位于设置选项下的开发菜单中。 关键是你必须重新加载JS。 您可以在打包器输出中验证dev=false 。 It's in the dev menu under the settings option. The key is that you then have to reload the JS. You can verify in your packager output that dev=false.
  • 这里有两件不同的事情。 运行react-native run-ios将在您的模拟器上运行您的应用程序。 要在您的设备中运行它,您需要从xCode运行该应用程序。 您可以在此处查看RN在设备上运行的官方文档。 There are two different things here. Running react-native run-ios will run your application on your simulator. To run it in your device, you'll need to ...
  • 因为您需要配置proguard ..请在此处查看RN官方文档。 Proguard是一种可以略微减小APK大小的工具。 它通过剥离应用程序未使用的部分React Native Java字节码(及其依赖项)来实现此目的。 重要提示 :如果您启用了Proguard,请务必彻底测试您的应用。 Proguard通常需要特定于您正在使用的每个本机库的配置。 请参阅app / proguard-rules.pro。 Because you need to configure proguard.. Check the RN ...
  • 你可以在Modal中添加它 _onNavigationStateChange (webViewState) { this.hide() } show () { this.setState({ modalVisible: true }) } hide () { this.setState({ modalVisible: false }) } render () { const { clientId, redirectUrl, scopes } = this.props return ( ...
  • 它与其他平台的功能相同,请参阅常见问题解答以获取详细信息。 在Andorid上,您必须保留清单中定义的活动,服务和其他组件的名称,因为操作系统按名称引用它们。 生命周期方法也是如此,例如onCreate() 。 不会以任何方式修改资源。 将重命名非组件类(POJO),其方法和字段也将重命名。 它最终取决于您使用的配置文件的内容。 正如您所注意到的那样,这并不完全隐藏您的代码,没有任何内容。 它只是让反编译时更难阅读。 您可以尝试更多的DexGuard ,但最终如果有人确定,他们可以反转您的代码(本机代码也是 ...
  • 好吧,这是一个使用端口8081的相机应用程序导致了这个问题。 如果我杀了那个过程就行了。 我检查了使用的端口 netstat -tulpn 然后在那里找8081端口 kill -9 假设你很高兴没有运行,并改变它的配置。 如果对正在使用的端口进行反应本机检查并且如果是,则报告它,以便保存具有相同问题的其他端口,将会很方便。 Ok, it was a camera application using port 8081 that was causing the issue. If I kill ...
  • 在build.gradle文件中,您可以尝试更改以下行: compile "com.facebook.react:react-native:+" // From node_modules 对此: compile("com.facebook.react:react-native:+") { exclude module: 'javax.inject' } 该错误基本上意味着@Inject注释被多次包含在您的最终APK中,它来自不同的地方。 很难确切地指出究竟在哪里,但这种来自本地反应的传递依赖很可能是其 ...
  • 使用官方Babel预设 为React Native应用程序安装官方的Babel预设 : npm i babel-preset-react-native --save-dev 编辑你的.babelrc : { "presets": ["react-native"] } 以前的RN v21.0:从react-native扩展原始的.babelrc 为避免操纵node_modules目录中的文件,应该扩展原始的.babelrc 。 通过保持您的依赖关系清洁,升级或共享项目不会有任何问题。 { "e ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)