c# - How do I DataBind a DataGrid after reading serial port -


i trying read serial port , put data grid. , data doesn't appear in data grid.

i used notify changer per martin comment below.

only wanted show data on datagrid view when serial port updated.

here xaml:

<window x:class="wpfapplication2.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="mainwindow" height="689.7" width="655">  <grid margin="0,0,2,9">     <datagrid x:name="datagriddata" margin="50,25,19,10" autogeneratecolumns="false" itemssource="{binding source=firstname}">                 </datagrid>               </grid> </window> 

and here cs:

  using system;   using system.collections.generic;   using system.linq;   using system.text;   using system.threading.tasks;   using system.windows;   using system.windows.controls;   using system.windows.data;   using system.windows.documents;   using system.windows.input;   using system.windows.media;   using system.windows.media.imaging;   using system.windows.navigation;   using system.windows.shapes;   using system.io.ports;   using system.threading;   using system.collections.objectmodel;   using system.componentmodel;    namespace wpfapplication2   {  /// <summary>  /// interaction logic mainwindow.xaml  /// </summary> public partial class mainwindow : window, inotifypropertychanged {     public event propertychangedeventhandler propertychanged = delegate { };       /// <summary>     /// class contain row data show in datagridrecieved     /// </summary>     public class datafortable : inotifypropertychanged     {          public event propertychangedeventhandler propertychanged = delegate { };          private string time; // contain time of data has been sent or recieved.           /// <summary>         /// or sets time has been recieved.         /// </summary>         public string time         {             { return time; }             set { time = value;             propertychanged(this, new propertychangedeventargs("time"));             }         }           private string data; // contain line of data has been recieved.                                  /// <summary>         /// or set line of data has been send recieved         /// </summary>         public string data         {             { return data; }             set { data = value;             propertychanged(this, new propertychangedeventargs("data"));             }         }         }         private system.threading.thread threadserial;     private system.io.ports.serialport _sp;     private queue<byte[]> _qbytesfromserialport1;     private queue<datetime> _qdatetimefromserialport1;           private byte[] _realbytesfromserialport;     private delegate void emptydelagte(); // delegate pass on play option.     private emptydelagte updatetableguidelgate;                     public datafortable _datafortable;      private thread guithread;     object lockmessage;        private observablecollection  <datafortable> _firstname;     public observablecollection<datafortable> firstname     {         { return _firstname; }         set { _firstname = value;         propertychanged(this, new propertychangedeventargs("items"));         }     }        public mainwindow()     {         initializecomponent();                                              _qbytesfromserialport1 = new queue<byte[]>();         _qdatetimefromserialport1 = new queue<datetime>();          _firstname = new observablecollection<datafortable>();          _datafortable = new datafortable();         // datagriddata.itemssource = _firstname.select(datafortable => new { datafortable.data, datafortable.time }).tolist();           //create serial port         _sp = new serialport("com1", 38400, parity.none, 8, stopbits.one);         _sp.open();         threadserial = new thread(new threadstart(readserialport)); //reads serial port.         threadserial.isbackground = true;         threadserial.start();          //updatetableguidelgate = new emptydelagte(updatetableguidelgate);         guithread = new thread(new threadstart(guithread));         guithread.isbackground = true;         guithread.start();          }       private void readserialport()     {         _realbytesfromserialport = new byte[100];         datetime _dtread = new datetime();         lockmessage = 0;         while (true)         {                             if (_sp.isopen)             {                  if (_sp.bytestoread > 0)                 {                      lock (lockmessage)                     {                          _realbytesfromserialport = new byte[_sp.bytestoread];                         _sp.read(_realbytesfromserialport, 0, _sp.bytestoread); //read data                         if (_realbytesfromserialport.length != 0)                         {                             _dtread = datetime.now;                               _qbytesfromserialport1.enqueue(_realbytesfromserialport); // q serial porta                                                                                             _qdatetimefromserialport1.enqueue(_dtread); // q date time now.                         }                     }                  }             }         }     }      private byte[] tmp;      private void guithread()     {          _datafortable = new datafortable();         while (true)         {                  if (_qbytesfromserialport1.count > 0)                 {                     _datafortable.data = bytetostring(_qbytesfromserialport1.dequeue());                     _datafortable.time = datetime.now.tolongdatestring();                                   _firstname.add(_datafortable);                                         //   this.dispatcher.begininvoke(updatetableguidelgate);                 }          }        }       private string bytetostring(byte [] arr)     {         string tmps = "";         (int = 0; < arr.length; i++)         {             tmps += arr[i].tostring()  + " ";         }          return tmps;      }     }  } 

here example of how add data thread:

<window x:class="datagridbinding.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="mainwindow" height="350" width="525"> <grid>     <datagrid itemssource="{binding items}"/> </grid> 

public partial class mainwindow : window, inotifypropertychanged {     private observablecollection<datatableitem> _items;      public observablecollection<datatableitem> items     {         { return _items; }         set         {             _items = value;             propertychanged(this, new propertychangedeventargs("items"));         }     }       public mainwindow()     {         initializecomponent();         this.datacontext = this;          items = new observablecollection<datatableitem>();          task.factory.startnew(() =>         {             dispatcher.invoke(() =>             {                 (int = 0; < 10; i++)                     items.add(new datatableitem() { data = "data " + });             }         );         });     }      public event propertychangedeventhandler propertychanged = delegate { }; } 

and datatableitem class:

public class datatableitem : inotifypropertychanged {     private string _data;      public string data     {         { return _data; }         set         {             _data = value;             propertychanged(this, new propertychangedeventargs("data"));         }     }      public event propertychangedeventhandler propertychanged = delegate { }; } 

in order ui informed of newly items added, need use observablecollection instead of simple list.

one more thing, serialport class has datareceived event, wouldn't more suited here instead of polling ?

further more, try simplify way going gui thread, it's bit difficult follow , shouldn't be. in example have used lambda expression on invoke. check out!


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -