c#中的数据网格视图 [英] Datagrid view in c#
问题描述
嗨我正在编写一个代码,其中我从文本框中的串口获取数据。现在我希望数据在datagrid中。输出如下:
------------------------------ JL206F ID:VDE40050
2015/05/29 11:02:19
JBatch_No.:067
JOperator ID:15
-------------------- ------------
混合计数
W --- -----------------------------存款金额:0.00
- -------------------------------
denom count value
UVK(| ???? DW50D 1 50.00
JUVK(| ???? DW20D 1 20.00
JUVK(| ???? DW10ND 1 10.00
J ----- ---------------------------
总计:3 80.00 >
--------------------------------
硬币:0.00
-------------------- ------------
余额:0.00
我希望我的网格看起来像这样
名称ID日期时间Batch_No运营商ID存款金额denom
计算总金币余额
和我的当我分割数据时,代码如下,它给出了spli [1]数组超出范围的错误。我怎么能这样做我是c#的新手。这是我的代码
hi I am writing a code in which i got a data from serial port in a textbox. Now I want that data to be in datagrid. The output is as follows:
------------------------------JL206F ID:VDE40050
2015/05/29 11:02:19
JBatch_No.:067
JOperator ID:15
--------------------------------
Mixed Counting
W--------------------------------Deposit Amount: 0.00
--------------------------------
denom count value
UVK(|????DW50D 1 50.00
JUVK(|????DW20D 1 20.00
JUVK(|????DW10ND 1 10.00
J--------------------------------
Total: 3 80.00
--------------------------------
Coin: 0.00
--------------------------------
Balance: 0.00
I want my grid to look like this
Name ID Date Time Batch_No Operator ID Deposit amount denom
Count value total coin balance
and my code is as follows when i am splitting data it is giving me error on spli[1] array out of bound. How Can I do it I am new to c#. 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 SerialPortListener.Serial;
using System.IO;
namespace SerialPortListener
{
public partial class MainForm : Form
{
SerialPortManager _spManager;
public MainForm()
{
InitializeComponent();
UserInitialization();
}
private void UserInitialization()
{
_spManager = new SerialPortManager();
SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;
serialSettingsBindingSource.DataSource = mySerialSettings;
portNameComboBox.DataSource = mySerialSettings.PortNameCollection;
baudRateComboBox.DataSource = mySerialSettings.BaudRateCollection;
dataBitsComboBox.DataSource = mySerialSettings.DataBitsCollection;
parityComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.Parity));
stopBitsComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));
_spManager.NewSerialDataRecieved += new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
_spManager.Dispose();
}
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
{
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}
int maxTextLength = 100000; // maximum text length in text box
if (tbData.TextLength > maxTextLength)
tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength);
DataTable dt = new DataTable();
DataColumn colName = dt.Columns.Add("Name", typeof(string));
DataColumn colID = dt.Columns.Add("ID", typeof(string));
DataColumn colDate = dt.Columns.Add("Date", typeof(string));
DataColumn colTime = dt.Columns.Add("Time", typeof(string));
DataColumn colBatch_No = dt.Columns.Add("Batch_No.", typeof(string));
DataColumn colOperatorID = dt.Columns.Add("Operator ID", typeof(string));
DataColumn colDepAmt = dt.Columns.Add("Deposit Amount", typeof(string));
DataColumn coldenom =dt.Columns.Add("Denom", typeof(string));
DataColumn colCount = dt.Columns.Add("Count", typeof(string));
DataColumn colValue = dt.Columns.Add("Value", typeof(string));
DataColumn colTotal = dt.Columns.Add("Total", typeof(string));
DataColumn colCoin = dt.Columns.Add("Coin", typeof(string));
DataColumn colBal = dt.Columns.Add("Balance", typeof(string));
string str = Encoding.ASCII.GetString(e.Data);
string[] stringSeparators = new string[] { "\n\n\r" };
string[] lines = str.Split(stringSeparators,StringSplitOptions.None );
foreach (var line in lines)
{
string[] split = line.Split(stringSeparators, StringSplitOptions.None);
DataRow row = dt.NewRow();
row.SetField(colName,split[0]);
row.SetField(colID,split[1]);
row.SetField(colDate,split[2]);
row.SetField(colTime,split[3]);
row.SetField(colBatch_No,split[4]);
row.SetField(colOperatorID,split[5]);
row.SetField(colDepAmt,split[6]);
row.SetField(coldenom,split[7]);
row.SetField(colCount,split[8]);
row.SetField(colValue,split[9]);
row.SetField(colTotal,split[10]);
row.SetField(colCoin,split[11]);
row.SetField(colBal,split[12]);
dt.Rows.Add(row);
tbData.AppendText(str);
tbData.ScrollToCaret();
}
}
// Handles the "Start Listening"-button click event
private void btnStart_Click(object sender, EventArgs e)
{
_spManager.StartListening();
}
// Handles the "Stop Listening"-button click event
private void btnStop_Click(object sender, EventArgs e)
{
_spManager.StopListening();
}
private void serialSettingsBindingSource_CurrentChanged(object sender, EventArgs e)
{
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void tbData_TextChanged(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
推荐答案
您正在尝试将字符串与相同的分隔符分开两次!
首先 str 分隔为行:
You are trying to separate your string with the same separators twice!
First str is separated to the lines:
string[] stringSeparators = new string[] { "\n\n\r" };
string[] lines = str.Split(stringSeparators,StringSplitOptions.None );
...然后你再次尝试将行的项目分开:
...then you try to separate the items of lines again:
foreach (var line in lines)
{
string[] split = line.Split(stringSeparators,StringSplitOptions.None);
...
这是矛盾的,因为在第一次拆分中删除了分隔符。您应该为第二次分离选择不同的分隔符。
this is contradiction, because the separator characters are removed in the first split. You should choose different separator for the second separation.
这篇关于c#中的数据网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!