无法在Winforms中显示记录到DataGridView

发布于 2025-01-28 16:12:52 字数 1429 浏览 3 评论 0原文

这就是我的数据库中我的表“项目”的样子:

现在,我想在Winforms应用程序中显示它。我的应用程序包括看起来像这样的主页: ”在此处输入映像说明”

当我单击商店按钮时,我希望该程序将访问我创建的usercontrol1中的datagridview,并从我的创建并输入所有ID,名称和价格数据库中的数据库。 这就是我在主视图中将其制作的方式:

string connectionString = @"myconnectionString";     
private void storeBtn_Click(object sender, EventArgs e)
    {
        
        userControl21.Hide();
        userControl11.Show();
        userControl11.BringToFront();
        using (SqlConnection sqlCon = new SqlConnection(connectionString))
        {
            sqlCon.Open();
            SqlDataAdapter sqlData = new SqlDataAdapter("SELECT * FROM \"Item\"", sqlCon);
            DataTable mytable = new DataTable();
            sqlData.Fill(mytable);

            UserControl1 uctl1 = new UserControl1();
            uctl1.storeGridView.AutoGenerateColumns = false;
            uctl1.storeGridView.DataSource = mytable;

        }
    }

我也已经将datapropertyname放在我的usercontrol datagridview中,同一名称与我想要在数据库中获取记录的列。当我运行程序时,所有操作都会运行,没有发生错误,但是当我单击“存储”按钮时,什么都没有出现,没有显示记录。 我也尝试逐步调试,但仍然不知道为什么它没有收到任何记录。以这种速度,我真的很感谢任何帮助。

This is what my table "Item" look like in my database:
enter image description here

Now, I want to display it inside my winforms application. My application include the main page that look like this: enter image description here

When I click the Store button, I expect the program will access the DataGridView inside UserControl1 that I have created and input all id, name and price from my database into it.
This is how I make it inside the main view page:

string connectionString = @"myconnectionString";     
private void storeBtn_Click(object sender, EventArgs e)
    {
        
        userControl21.Hide();
        userControl11.Show();
        userControl11.BringToFront();
        using (SqlConnection sqlCon = new SqlConnection(connectionString))
        {
            sqlCon.Open();
            SqlDataAdapter sqlData = new SqlDataAdapter("SELECT * FROM \"Item\"", sqlCon);
            DataTable mytable = new DataTable();
            sqlData.Fill(mytable);

            UserControl1 uctl1 = new UserControl1();
            uctl1.storeGridView.AutoGenerateColumns = false;
            uctl1.storeGridView.DataSource = mytable;

        }
    }

I already also putting DataPropertyName inside my UserControl DataGridView the same name with the column i want to get record in my database. And when I run program, everything run and there's no bug occured, but when I click the "Store" button, nothing come out, there's no record displayed.
I also try to debug step by step but still have no idea why it doesn't received any record. At this rate, I would really appreciate any help coming.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文