Visual Studio 中的 C#:如何在 DataGridView 中显示列表?得到奇怪的东西
一段时间以来,我自愿在公司自学 Windows 编程。开始编写 vbs 脚本,突然意识到这个编程东西是多么有用;-)
无论如何,我是 C# 和 Visual Studio 的新手,我有点了解它是如何工作的,然后在设计端拖放界面部分将它们在背面/程序侧连接在一起。
我正在尝试编写一个程序,该程序将(最终)读取(非常具体的)csv 文件,并为用户提供比 Excel 更友好的编辑和排序方式。应该是简单的事情,我对此感到兴奋。
我今天早上开始,在互联网的帮助下,读入并解析了 CSV(实际上是一个 TSV,因为它们使用制表符而不是逗号,而是嘿)。
我一直在尝试找出显示信息的最佳方式,并且至少现在我正在使用 DataGridView。但数据没有显示。相反,我看到一长串值,其列标题为 Length、LongLength、Rank、SyncRoot、IsReadOnly、IsFixedSize 和 IsSynchronized。
我不知道这些意味着什么或它们来自哪里,不幸的是我也不知道如何改变它们。也许有人可以帮忙?
这是我的代码:
using System;
使用 System.Collections.Generic; 使用 System.ComponentModel; 使用系统数据; 使用系统绘图; 使用 System.Linq; 使用系统文本; 使用 System.Windows.Forms; 使用系统.IO;
命名空间 readInCSV { 公共部分类 readInCSV :表单 { 公共 readInCSV() { 初始化组件(); }
public List<string[]> parseCSV(string path)
{
List<string[]> parsedData = new List<string[]>();
try
{
using (StreamReader readfile = new StreamReader(path))
{
string line;
string[] row;
while ((line = readfile.ReadLine()) != null)
{
row = line.Split('\t');
parsedData.Add(row);
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return parsedData;
}
//PRIVATE METHODS FROM HERE ON DOWN
private void btnLoadIn_Click(object sender, EventArgs e)
{
int size = -1;
DialogResult csvResult = openCSVDialog.ShowDialog();
if (csvResult == DialogResult.OK)
{
string file = openCSVDialog.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
}
catch (IOException)
{
}
}
dgView.Dock = DockStyle.Top;
dgView.EditMode = DataGridViewEditMode.EditOnEnter;
dgView.AutoGenerateColumns = true;
dgView.DataSource = parseCSV(openCSVDialog.FileName);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void openCSVDialog_FileOk(object sender, CancelEventArgs e)
{
}
}
}
提前致谢!
Been a while and I've volunteered to teach myself Windows programming at my company. Started writing vbs scripts and suddenly realized how incredibly useful this programming thing is ;-)
Anyway, I'm a total newbie at C# AND Visual Studio, I kind of get how it works, you drag and drop interface pieces in the design side then wire them together in the back/program side.
I'm trying to write a program that will (ultimately) read in a (very specific kind of) csv file and give the user a friendlier way to edit and sort through it than Excel. Should be simple stuff, and I'm excited about it.
I started this morning and, with the help of the internet, got as far as reading in and parsing the CSV (which is actually a TSV, since they use tabs not commas but hey).
I've been trying to figure out the best way to display the information, and, for now at least, I'm using a DataGridView. But the data isn't displaying. Instead, I'm seeing a long grid of values with column headers of Length, LongLength, Rank, SyncRoot, IsReadOnly, IsFixedSize, and IsSynchronized.
I don't know what any of these mean or where they come from, and unfortunately I don't know how change them either. Maybe somebody can help?
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace readInCSV
{
public partial class readInCSV : Form
{
public readInCSV()
{
InitializeComponent();
}
public List<string[]> parseCSV(string path)
{
List<string[]> parsedData = new List<string[]>();
try
{
using (StreamReader readfile = new StreamReader(path))
{
string line;
string[] row;
while ((line = readfile.ReadLine()) != null)
{
row = line.Split('\t');
parsedData.Add(row);
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return parsedData;
}
//PRIVATE METHODS FROM HERE ON DOWN
private void btnLoadIn_Click(object sender, EventArgs e)
{
int size = -1;
DialogResult csvResult = openCSVDialog.ShowDialog();
if (csvResult == DialogResult.OK)
{
string file = openCSVDialog.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
}
catch (IOException)
{
}
}
dgView.Dock = DockStyle.Top;
dgView.EditMode = DataGridViewEditMode.EditOnEnter;
dgView.AutoGenerateColumns = true;
dgView.DataSource = parseCSV(openCSVDialog.FileName);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void openCSVDialog_FileOk(object sender, CancelEventArgs e)
{
}
}
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里发生的情况是,DataGridView 尝试显示
parsedData
列表中每个字符串数组的所有信息。当您将 DataGridView 的数据源设置为对象列表时,它会尝试将每个对象解释为要显示的行。
parsedData
是字符串数组对象的列表,因此网格向您显示数组对象的所有可显示属性。我们可以做的是将每个 TSV 行解析为一个自定义类(称为 TsvRow),该类公开了所有相关数据。然后,TsvRow 对象被放置在列表中并传递到 DataGridView。 本文解释了此方法的示例。
例如:
...
由于所有列数据都作为属性公开(即“Column1”和“Column2”),因此它们应该自动反映在 DataGridView 中。
希望有帮助!如果需要澄清,请告诉我。
What's happening here is that the DataGridView is trying to display all the information for each of the string arrays in your
parsedData
List.When you set a DataGridView's DataSource as a List of objects, it attempts to interpret each of the objects as a row to display.
parsedData
is a List of string array objects, so the grid is showing you all the displayable properties for an array object.What we can do is parse each TSV row into a custom class (call it TsvRow) which has all the relevant data exposed. The TsvRow objects are then placed in a List and passed to the DataGridView. An example of this approach is explained in this article.
For example:
...
Since all your column data is exposed as properties (i.e. "Column1" and "Column2"), they should be reflected in the DataGridView automatically.
Hope that helps! Please let me know if this needs clarification.
DataGridView
尝试显示字符串数组的属性。您应该设置AutoGenerateColumns = false
并自行创建列。CSV/TSV 的第一行是否包含列名称?是的,您不应该将它们作为
DataSource
传递。The
DataGridView
tries to display the Properties of your string-Array. You should setAutoGenerateColumns = false
and create the columns by yourself.Would the first line of the CSV/TSV contain the column names? Is so, you shouldn't pass them as
DataSource
.