Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Content from Textbox to Dtagridview

c# datagridview

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 gmanan

gmanan

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 03 June 2015 - 12:42 AM

Hi, 

I am working on a note sorter machine and I am fetching the data out of it through serial port. My Output in the textbox 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

 

Now I want this information to present in grid view say for an example Batchno., operator etc in a colums of data grid. I am new to c# and my code is as follows. Please help

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);
            tbData.AppendText(str);
            string[] myArray = {str};

            dataGridView1.Columns.Add("Column1", "Name");
            int k=0;
            for (int i = 0; i < str.Length;i++ )
            {
                k = str.IndexOf("\n",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)
        {

        }

        
    }
}






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download