【问题标题】:Can't select row or control the scroll bar with DataGridVew无法使用 DataGridView 选择行或控制滚动条
【发布时间】:2018-01-29 16:17:20
【问题描述】:

也许这是一个新手问题。但我一直在寻找高低没有运气,因为我尝试过的任何方法都没有奏效。

我有一个名为dgvDeviceListDataGridView,它在FormMain 窗口中包含5 列:ID、设备名称、Mac 地址、RSSI 和广告类型。 当我使用一种方法扫描附近的蓝牙设备时,一旦找到,DataGridView 将添加一个新创建的行。

但是当扫描正在进行或停止时,我无法选择行或控制(向上/向下滚动)滚动条。看起来dgvDeviceList 没有滚动条。在某些论坛上,有描述作为multiple threading problem,但不幸的是他们提供的方法无法解决。代码如下:

    int rowIndex = 1;

    private void SetupScanResultHandler()
    {
        ScanCallBack.ScanResultHandler = (result) =>
        {
            if (result != null)
            {
                foreach (var item in result.ScanRecords)
                {
                  //the following InvokeRequired method is provided by internet
                    if (dgvDeviceList.InvokeRequired)
                    {
                        dgvDeviceList.Invoke((MethodInvoker)(() =>
                        {
                            SetupDeviceListDataGridView(item);

                        }));
                    }
                    else
                    {
                        SetupDeviceListDataGridView(item);

                    }

                    rowIndex++;
                }
            }
        };
    }

    private void SetupDeviceListDataGridView(CyScanRecord item)
    {
        DataGridViewRow row = dgvDeviceList.Rows[dgvDeviceList.Rows.Add()];
        row.Cells[0].Value = rowIndex;
        row.Cells[1].Value = Utility.GetDeviceName(item.AdvertisementData.RawData.Length, item.AdvertisementData.RawData);
        row.Cells[2].Value = Utility.InsertFormat(item.PeerDeviceAddress.Address.ToString("X12"), 2, ":");
        row.Cells[3].Value = item.RSSI.ToString() + "dBm";
        row.Cells[4].Value = item.AdvertisementType.ToString();

    }

如果您能发布一些代码或指导我到 我应该研究的正确方法。谢谢。

【问题讨论】:

    标签: c# multithreading datagridview scrollbar


    【解决方案1】:

    将值添加到 DataTable 而不是 DGV。确保添加新行:DataRow newRow = new dt1.Rows.Add();

         private void SetupDeviceListDataGridView(DataRow row)
         {
                    row[0] = 5;
                    row[1] = 25;
                    row[2] = 25;
                    row[3] = 10;
                    row[4] = 35;
    
                    dgvDeviceList.DataSource = null;
                    dgvDeviceList.DataSource = dataTabl;
    
          }
    

    【讨论】:

    • 5,25,25…是列宽的FillWeight,而不是列的值。我已经将设备信息值设置为dataTable row,然后让dataTable添加这一行。最后,我将dgvDeviceList的数据源与dataTable绑定在一起。上面的过程有什么问题吗?
    • 我将更新问题描述。我不会使用DataTable,而是使用DataGridViewRow 并将值设置为`row.Cells[i].Value`。使用结果,奇怪的现象依然存在!
    【解决方案2】:

    糟糕!!我在属性列表中将Enableproperty 设置为false。代码dgvDeviceList.enable = true;可以解决问题。应该没有错误。

    Listview and Datagridview are always disabled

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 2013-03-12
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多