我正在尝试读取文本文件并将内容显示到datagridview。我的问题是我不知道如何阅读文本文件的所有内容并在datagridview中显示特定的行。
我的文本文件内容的格式如下:
A1 = SUPERSTAR RETRO'
N1 =檀'
C1 = CHUCK TAYLOR'
A1 = CLOUDFOAM RACE'
N1 =雅诺斯基'
C1 = HIGH CUT'
我有3个名为ADIDAS,NIKE和CONVERSE的列的datagridview。现在我想要显示所有AI到ADIDAS,所有N1到NIKE和所有C1到CONVERSE就像这样:
ADIDAS
NIKE
CONVERSE
SUPERSTAR RETRO
檀
HIGH CUT
CLOUDFOAM RACE JANOSKI CHUCK TAYLOR
答案 0 :(得分:0)
所以我会将文本文件传递给字符串并使用它:
Dim SearchWithinThis As String = "A1=SUPERSTAR RETRO'
N1=TANJUN'
C1=CHUCK TAYLOR'
A1=CLOUDFOAM RACE'
N1=JANOSKI'
C1=HIGH CUT'"
Dim SearchForThisBegining As String = "A1" 'You can put whatever you need to find
Dim SearchForThisEnd As String = " " 'to find the end of the line
Dim FirstCharacter As Integer = SearchWithinThis.IndexOf(SearchForThisBegining)
Dim LastCharacter As Integer = SearchWithinThis.IndexOf(SearchForThisEnd)
现在,您可以遍历循环并将找到的值存储到GridView。
答案 1 :(得分:0)
你的文字文件在每一行的末尾是否真的有'
?
我会创建三个集合:
Dim nikeItems as New List(Of String)()
'etc
然后拆分文件内容:
For Each line As String In file.Split(Environment.NewLine)
Dim lineContent as String() = line.Split("="c)
Select Case (lineContent(0))
Case "N1": nikeItems.Add(lineContent(1))
' etc.
Next
然后从这些列表中填充数据网格。
答案 2 :(得分:0)
这将做你想要的!我包括一些额外的示例,以将数据从GridView发送到SQL Server数据库。此外,还有一些代码可以在您完成项目时关闭项目。
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;
using System.Globalization;
using System.Configuration;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
string delimiter = ",";
string tablename = "medTable";
DataSet dataset = new DataSet();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (MessageBox.Show("Are you sure you want to import the data from \n " + openFileDialog1.FileName + "?", "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
filename = openFileDialog1.FileName;
StreamReader sr = new StreamReader(filename);
string csv = File.ReadAllText(openFileDialog1.FileName);
dataset.Tables.Add(tablename);
dataset.Tables[tablename].Columns.Add("Order ID");
dataset.Tables[tablename].Columns.Add("Product");
dataset.Tables[tablename].Columns.Add("Unit Price");
dataset.Tables[tablename].Columns.Add("Quantity");
dataset.Tables[tablename].Columns.Add("Discount");
string allData = sr.ReadToEnd();
string[] rows = allData.Split("\r".ToCharArray());
foreach (string r in rows)
{
string[] items = r.Split(delimiter.ToCharArray());
dataset.Tables[tablename].Rows.Add(items);
}
this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;
MessageBox.Show(filename + " was successfully imported. \n Please review all data before sending it to the database.", "Success!", MessageBoxButtons.OK);
}
else
{
this.Close();
}
}
}
public string filename { get; set; }
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void Import_Load(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
//remove the semicolon, and add brackets below after line
{
//create the connection string
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Ryan\\Desktop\\Coding\\Microsoft Access\\Northwind_2012.mdb";
//create the database query
string query = "SELECT * FROM [OrderDetailsTest]";
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
//fill the DataTable
dAdapter.Fill(dTable);
//the DataGridView
DataGridView dataGridView1 = new DataGridView();
//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dTable;
//set the DataGridView DataSource
dataGridView1.DataSource = bSource;
// An update function to get the changes back into the database.
dAdapter.Update(dTable);
}
}
}