【发布时间】:2018-01-29 16:17:20
【问题描述】:
也许这是一个新手问题。但我一直在寻找高低没有运气,因为我尝试过的任何方法都没有奏效。
我有一个名为dgvDeviceList 的DataGridView,它在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