如何将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.
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
最满意答案
因为您需要配置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 ...