繁体   English   中英

DataGridView不显示DataTable C#中的列

[英]DataGridView not showing columns from DataTable C#

我的GUI在Form1中具有datagridview,应该由位于单独类中的数据表中的数据填充。 我已经设置了绑定源和数据源,但是当我运行GUI时,datagridview为空白,并且不显示数据表中的列。 此时,我不会为行存储任何数据,我只想确保显示我的列标题。

我查看了以下所有链接,但到目前为止没有任何帮助:

DataGridView不显示列/数据

DataGridView不显示我的列

如何在C#中将数据表绑定到datagridview

DataGridView不显示DataTable

这是我的代码

Form1.cs:

using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

public Form1()
{
    InitializeComponent();
    DataTable dataTable = LightSensor.dtOptical;
    DGV.DataSource = dataTable;
}

LightSensor.cs:

using System;
using System.Data;
using System.IO.Ports;
using System.Threading.Tasks;
using System.Windows.Forms;

public static DataTable dtOptical = new DataTable();
BindingSource bindingSource = new BindingSource();

private void OpticalDataSetup()
{
    dtOptical.Columns.Add("Sequence");
    dtOptical.Columns.Add("Date");
    dtOptical.Columns.Add("Time");
}
private void OpticalDataRows()
{
    DataRow row = dtOptical.NewRow();

    row = dtOptical.NewRow();
    row["Sequence"] = MeasSeq;
    row["Date"] = DateTime.Today.ToString("MM-dd-yyyy");
    row["Time"] = DateTime.Now.ToString("HH:mm:ss");

    dtOptical.Rows.Add(row);
    bindingSource.DataSource = dtOptical;
}

您在LightSensor类中需要一个构造函数,该构造函数调用OpticalDataSetupOpticalDataRows

public LightSensor(){
 OpticalDataSetup();
 OpticalDataRows();
}

然后在InitializeComponent()之后初始化LightSensor;

LightSensor ls = new LightSensor();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM