将表的数据加载到运行时创建的 DataGridView 中

发布于 2025-01-15 09:35:24 字数 1385 浏览 3 评论 0原文

每当我按下按钮时,我都会尝试在运行时创建一个 DataGridView。
我还想显示数据库中表中的数据。

当我按下按钮时,应该创建一个 DataGridView 并显示数据,但它什么也没做。它不会创建 DataGridView。

这是我的代码(该表称为“供应”):

public partial class managerpage : Form
{
    int idWorkersDB = 0;
    int idAccountsDB = 1;
    int idSuppliersDB = 2;
    int idOtherDB = 3;
    public static DataGridView workersView = new DataGridView();
    public static DataGridView suppliesView = new DataGridView();
    public static DataGridView accountsView = new DataGridView();
    int clickedID;

    private void picBoxSuppliers_MouseUp(object sender, MouseEventArgs e)
    {
        suppliesView.Location = new Point(59, 51);
        suppliesView.Size = new Size(749, 399);
        
        clickedID = idSuppliersDB;
        dataDBPanel.Visible = true;
        dataDBPanel.Dock = DockStyle.Fill;
        
        titleDataLbl.Text = "Suppliers Data";
        titleDataLbl.Left = (this.Width - titleDataLbl.Width) / 2;
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\project\FUCKINGVISUALSTUDIOISSOSHIT\loginData.mdb");
        OleDbDataAdapter cmd = new OleDbDataAdapter("SELECT * FROM supplies", con);
        con.Open();
        DataTable tbl = new DataTable();
        cmd.Fill(tbl);
        con.Close();

        suppliesView.DataSource = tbl;
        suppliesView.Show();
    }
}

I'm trying to create a DataGridView at runtime whenever I press a button.
I also want to present the data from a table in my database.

When I press the Button that should create a DataGridView and show the data, but it just does nothing. It doesn't create the DataGridView.

Here is my code (the table is called "supplies"):

public partial class managerpage : Form
{
    int idWorkersDB = 0;
    int idAccountsDB = 1;
    int idSuppliersDB = 2;
    int idOtherDB = 3;
    public static DataGridView workersView = new DataGridView();
    public static DataGridView suppliesView = new DataGridView();
    public static DataGridView accountsView = new DataGridView();
    int clickedID;

    private void picBoxSuppliers_MouseUp(object sender, MouseEventArgs e)
    {
        suppliesView.Location = new Point(59, 51);
        suppliesView.Size = new Size(749, 399);
        
        clickedID = idSuppliersDB;
        dataDBPanel.Visible = true;
        dataDBPanel.Dock = DockStyle.Fill;
        
        titleDataLbl.Text = "Suppliers Data";
        titleDataLbl.Left = (this.Width - titleDataLbl.Width) / 2;
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\project\FUCKINGVISUALSTUDIOISSOSHIT\loginData.mdb");
        OleDbDataAdapter cmd = new OleDbDataAdapter("SELECT * FROM supplies", con);
        con.Open();
        DataTable tbl = new DataTable();
        cmd.Fill(tbl);
        con.Close();

        suppliesView.DataSource = tbl;
        suppliesView.Show();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2025-01-22 09:35:24

您隐藏了大部分代码,但我怀疑您从未将 SupplyView (数据网格视图?)添加到表单中。下面是一个运行良好的示例代码:

void Main()
{
    DataTable t = new DataTable();
    using (var con = new SqlConnection(@"server=.;Database=Northwind;Trusted_Connection=yes"))
    using (var cmd = new SqlCommand(@"select * from Customers", con))
    {
        con.Open();
        t.Load(cmd.ExecuteReader());
        con.Close();
    }

    
    var f = new Form();
    var dgv = new DataGridView { Dock = DockStyle.Fill, DataSource=t};
    f.Controls.Add(dgv);
    f.Show();
}

You are hiding most of the code, but I suspect you never add that suppliesView (a datagridview?) to the form. Here is a sample code that works wonderfully well:

void Main()
{
    DataTable t = new DataTable();
    using (var con = new SqlConnection(@"server=.;Database=Northwind;Trusted_Connection=yes"))
    using (var cmd = new SqlCommand(@"select * from Customers", con))
    {
        con.Open();
        t.Load(cmd.ExecuteReader());
        con.Close();
    }

    
    var f = new Form();
    var dgv = new DataGridView { Dock = DockStyle.Fill, DataSource=t};
    f.Controls.Add(dgv);
    f.Show();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文