12
Answers

Datagrid C#

Photo of Gunjan Manan

Gunjan Manan

9y
745
1
Hi I am working with a note sorter machine that is connected to a serial port. I am able to retrieve a data into  text box like 

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

Now i want this information to come in datagrid Like Batch no, Balance , Operator id etc in columns. o far my code is as follows. I am able to retrieve the info but bit by bit and only in one column. Please help
 
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);      string str = Encoding.ASCII.GetString(e.Data);     tbData.AppendText(str);     string[] myArray = {str};      int k=0;      for (int i = 0; i < str.Length;i++ )     {         k = str.IndexOf("10", i);         if (k!=-1)         {             dataGridView1.Rows.Add(str.Substring(i,k-1));             i=k+2;         }         else         {             dataGridView1.Rows.Add(str.Substring(i, str.Length - i));             i = str.Length;         }     }     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) {  }
 

Answers (12)

0
Photo of ramya bharath
NA 922 0 9y
Your application continuously get info from the another application right so once the batch no appears that time it would have worked put other conditions in switch case to process other inputs too it will surely work the error would have come when other inputs enter where batch_no would not be there so put it in condition and exception check
0
Photo of Gunjan Manan
NA 46 4.4k 9y
but my string is coming dynamically after i pressprint on the machine in str in my code. however i did few changes and my application worked only once after that same error string str = Encoding.UTF8.GetString(e.Data); tbData.AppendText(str); TextReader read = new System.IO.StringReader(tbData.Text); str = read.ReadToEnd(); string toFind1 = "Batch_No.:"; string toFind2 = "Operator ID:"; int start = str.IndexOf(toFind1) + toFind1.Length; int end = str.IndexOf(toFind2, start); string str1 = str.Substring(start, end - start); dataGridView1.Rows[0].Cells[0].Value = str1;
0
Photo of ramya bharath
NA 922 0 9y
Try using regexp as like this

string input=" JL206F ID:VDE40050

2015/05/29 11:02:19

JBatch_No.:067

JOperator ID:15";
// Here we call Regex.Match.
Match match = Regex.Match(input, @"/(\w)\ W(\d {4}/\d {2}/\d [2} \d {2}:\d {2}:\d {2})\WJBATCH_NO.:([A-Za-z0-9])\WJOPERATION_ID:([A-Za-z0-9])",
RegexOptions.IgnoreCase);

// Here we check the Match instance.
if (match.Success)
{
// Finally, we get the Group value and display it.
string key = match.Groups[1].Value;

String time = match.Groups[2].Value;

String batchno = match.Groups[3].Value;

String operid = match.Groups[4].Value;


}
0
Photo of Gunjan Manan
NA 46 4.4k 9y
Hi I tried as per your instructions to find the labels in the text box however i am gettiing error in this also length cannot be zeero. Here is my coe
int maxTextLength = 100000; // maximum text length in text box
if (tbData.TextLength > maxTextLength)
tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength);
string str = Encoding.ASCII.GetString(e.Data);
tbData.AppendText(str);
string[] myArray = { str };
string toFind1 = "Batch_No.:";
string toFind2 = "Operator ID:";
int start = str.IndexOf(toFind1) + toFind1.Length;
int end = str.IndexOf(toFind2, start);
string str1 = str.Substring(start, end - start);
dataGridView1.Rows[0].Cells[0].Value = str1;
 
0
Photo of Gunjan Manan
NA 46 4.4k 9y
can u just give me some example via code
0
Photo of Gunjan Manan
NA 46 4.4k 9y
Yes it comes through when i press print on the machine.
0
Photo of ramya bharath
NA 922 0 9y
If this entire msg comes in through single event trigger?
JL206F ID:VDE40050

2015/05/29 11:02:19

JBatch_No.:067

JOperator ID:15

If so then using regex or string . contains we can check if Batch_No string exusts in thar output and then allocate cell accordingly.

Similarly we can set keyword check for others
0
Photo of Gunjan Manan
NA 46 4.4k 9y
As you can see above evertime batch no changes in the output file there is no fixed batch no.
0
Photo of Gunjan Manan
NA 46 4.4k 9y
I don't think so. However when I checked the machine there it is written Batch setup as 100
0
Photo of ramya bharath
NA 922 0 9y
Is there any possibility to know from the event result that this string is a batch no or this s a balance like that?

If so you can do like this
1. If the string is a batch no add a new row and add cell 0 with value

2. If balance get the last row of datagrid view and check cell 1 as empty then add the value to it.

Similarly you can process others for which to appropriately add to the cells you have know the type of string we get if that is possible you can do other's easily
0
Photo of Gunjan Manan
NA 46 4.4k 9y
How will I do that? Can you just give me an example and this 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);
string str = Encoding.ASCII.GetString(e.Data);
string[] myArray = { str };
int k = 0;
for (int i = 0; i < str.Length; i++)
{
k = str.IndexOf("100", i);
if (k != -1)
{
dataGridView1.Rows.Add(str.Substring(i, k - 1));
i = k + 2;
}
else
{
dataGridView1.Rows.Add(str.Substring(i, str.Length - i));
i = str.Length;
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)
{
}
}
}
 
 
0
Photo of ramya bharath
NA 922 0 9y
In your code you are adding rows to datagrid dynamically similarly before adding row add datagridviewcell to the row and add it . Hope this is what you have asked for

note:
Your code is tough to read please update that In your question.