【问题标题】:Not displaying continues data of networkstream in DataGridView在 DataGridView 中不显示网络流的连续数据
【发布时间】:2013-03-15 07:42:54
【问题描述】:

可以通过TcpClient类和方法从设备读取数据。

一旦我通过以太网连接使用 Ipaddress 和端口建立连接,就可以读取数据字节。我的读取方法读取特定字节数的网络流数据 并根据需要修剪字符串并更新到数据库并尝试在datagridview中显示更新的数据。它不会在datagridview中显示任何数据。

谁能建议我怎么做?

添加代码:

            private void ReceiveMethod()
           {
           try
          {
               string IP1 = textIP.Text.Trim();
               string port1 = textPort.Text.Trim(); 
               int port = Convert.ToInt32(port1);
               NetworkStream ns;
               int bytesRead = 0;
               byte[] buffer = new byte[9];

            try
            {

                IPAddress ipAddress = System.Net.IPAddress.Parse(Ip1);
                client = new TcpClient();
                client.Connect(IP1, port);
                while (true)
               {
                    ns = client.GetStream();
                    ns.Read(buffer, (int)bytesRead, buffer.Length - (int)bytesRead);

                      ASCIIEncoding encoder = new ASCIIEncoding();
                     msg = encoder.GetString(buffer);
                     GetData(msg);                        

                }
                client.Close();         

            }


    GetData(string data)
    {

       I'm trimming the data value according to my need
       and the updateing one filed of db and trying to populate table data in gridview

      //if i remove the while(true) in the Receivemethod am able to view the data in
         datagridview but i want to read continuous data from network stream and want 
         to show updated data in datagrid view
    }

【问题讨论】:

    标签: sockets c#-4.0 network-programming ethernet


    【解决方案1】:

    可能是您没有将网络流数据转换为 sring,所有数据都转换为 DataRow 类。

    1. 您使用什么数据库? (SQL Server/Oracle)
    2. 您将网络流数据放在什么类型的数据中?
    3. 显示您填写 DGV 的代码。

    【讨论】:

    • 尝试通过编译器查看您拥有的数据类型。如果没问题,那么你应该看到填写 DGV 的代码。
    • 我已经用代码编辑了上面的帖子。只有当它出现时,如果我想连续运行,则不会显示任何东西。
    【解决方案2】:
      foreach(NetworkStream s in ns)
       {
          GetData(string data);
       }
    

    【讨论】:

    • 如果这个答案有帮助,请投票。这是给予回答的动力:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 2011-07-31
    • 2016-10-12
    • 2014-01-07
    相关资源
    最近更新 更多